-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpercapta.cpp
More file actions
90 lines (81 loc) · 2.26 KB
/
Copy pathpercapta.cpp
File metadata and controls
90 lines (81 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Solution to: https://www.codechef.com/problems/PERCAPTA
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int tt;
cin >> tt;
while (tt--) {
int n, m;
cin >> n >> m;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
vector<int> b(n);
for (int i = 0; i < n; i++) {
cin >> b[i];
}
vector<vector<int>> g(n);
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v; u--; v--;
g[u].push_back(v);
g[v].push_back(u);
}
vector<int> order(n);
iota(order.begin(), order.end(), 0); // numeric
// for (int i = 0; i < n; i++) { order[i] = i; }
sort(order.begin(), order.end(), [&](const int &i, const int &j) -> bool {
return 1.0 * a[i] / b[i] > 1.0 * a[j] / b[j];
});
vector<bool> vis(n);
vector<int> comp;
long cap, pop;
function<void(int)> dfs = [&](int u) -> void {
vis[u] = true;
if (pop == 0) {
cap += a[u];
pop += b[u];
} else {
double cur = 1.0 * (cap + a[u]) / (pop + b[u]);
double prev = 1.0 * cap / pop;
if (cur < prev) {
return;
}
cap += a[u];
pop += b[u];
}
for (int v : g[u]) {
if (!vis[v]) {
dfs(v);
}
}
comp.push_back(u);
};
double opt = 0;
vector<int> res;
for (int i : order) {
if (!vis[i]) {
cap = pop = 0;
comp.clear();
dfs(i);
if (1.0 * cap / pop < opt) {
break;
}
opt = 1.0 * cap / pop;
if (comp.size() > res.size()) {
res.assign(comp.begin(), comp.end());
}
}
}
cout << res.size() << '\n';
for (int v : res) {
cout << v + 1 << ' ';
}
cout << '\n';
}
return 0;
}