#include <bits/stdc++.h>
#define int long long
//#define set unordered_set
using namespace std;
int INF = 1LL<<30;
vector<int> people;
vector<int> setmoves;
vector<int> ans;
class DSU {
private:
int n;
vector<int> parent;
vector<int> rank;
vector<int> rank2;
vector<set<int>> sets;
public:
DSU(int sz) {
n = sz;
parent=vector<int>(n); iota(parent.begin(), parent.end(), (int)0);
rank=vector<int>(n,1);
for (int i=0; i<n; i++) rank[i] = people[i];
rank2=vector<int>(n,1);
sets=vector<set<int>>(n);
for (int i=0; i<n; i++) sets[i] = {i};
}
int find(int node) {
if (node==parent[node]) return node;
return parent[node] = find(parent[node]);
}
void link(int u, int v) {
u=find(u); v=find(v);
if (u==v) {
return;
}
if (rank2[u]<rank2[v]) {
swap(u,v); //swap(sets[u],sets[v]);
}
rank2[u]+=rank2[v]; rank2[v] = 0;
rank[u]+=rank[v]; rank[v] = 0;
for (int j:sets[v]) {
sets[u].insert(j);
setmoves[j]++;
}
sets[v].clear();
parent[v] = u;
}
int getrank(int u) {
return rank[find(u)];
}
void destroy(int u) {
sets[u].clear();
}
vector<set<int>> getsets() {
return sets;
}
};
signed main() {
int local = 0;
if (local) {
// for getting input from input.txt
freopen("input.txt", "r", stdin);
// for writing output to output.txt
freopen("output.txt", "w", stdout);
}
/*#ifdef ONLINE_JUDGE
ios_base::sync_with_stdio(false);
cin.tie(NULL);
#endif*/ //fast IO
int n,m; cin >> n >> m;
//want to calculate for each node,
//the smallest w such that if we remove all nodes of weight > w, the connected component containing this node
//has total weight at most w
//use DSU
//make w take on the weight of each node + 1, in increasing order
//any set that has sum at most the current w is not allowable
//actually it's easier to merge the sets of indices from small to large
//so a set is marked as bad when it is merged into a larger set
ans=vector<int>(n,0);
people=vector<int>(n); for (int i=0; i<n; i++) cin >> people[i];
vector<int> order(n); iota(order.begin(), order.end(), (int)0);
sort(order.begin(), order.end(), [&](const int i1, const int i2) {
return people[i1]<people[i2];
});
vector<vector<int>> adjlist(n);
for (int i=0; i<m; i++) {
int u,v; cin >> u >> v; u--; v--;
adjlist[u].push_back(v); adjlist[v].push_back(u);
}
DSU dsu(n);
setmoves=vector<int>(n,0);
vector<int> active(n,0);
for (int oi=0; oi<n; oi++) {
int k = order[oi];
//cout << "adding node " << oi <<" " << k <<" " << people[k] << endl;
for (int j:adjlist[k]) {
if (active[j]) {
int r1 = dsu.getrank(j);
// cout << "adding edge " << k <<" " << j <<" " << r1 << endl;
if (r1<people[k]) {
dsu.destroy(dsu.find(j));
}
dsu.link(k,j);
}
else {
//cout << "inactive edge " << k <<" " << j << endl;
}
}
active[k] = 1;
}
vector<set<int>> settings = dsu.getsets();
for (auto i:settings) {
for (auto j:i) {
ans[j] = 1;
}
}
int mamove = *max_element(setmoves.begin(), setmoves.end());
assert(mamove<30);
for (int i=0; i<n; i++) {
cout << ans[i];
}
cout << endl;
}
Compilation message
island.cpp: In function 'int main()':
island.cpp:67:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
67 | freopen("input.txt", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
island.cpp:69:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
69 | freopen("output.txt", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
2 ms |
860 KB |
Output is correct |
5 |
Correct |
2 ms |
860 KB |
Output is correct |
6 |
Correct |
2 ms |
860 KB |
Output is correct |
7 |
Correct |
2 ms |
860 KB |
Output is correct |
8 |
Correct |
2 ms |
600 KB |
Output is correct |
9 |
Correct |
2 ms |
860 KB |
Output is correct |
10 |
Correct |
2 ms |
860 KB |
Output is correct |
11 |
Correct |
2 ms |
860 KB |
Output is correct |
12 |
Correct |
2 ms |
860 KB |
Output is correct |
13 |
Correct |
2 ms |
860 KB |
Output is correct |
14 |
Correct |
2 ms |
860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
239 ms |
63196 KB |
Output is correct |
4 |
Correct |
258 ms |
58716 KB |
Output is correct |
5 |
Correct |
288 ms |
61264 KB |
Output is correct |
6 |
Correct |
318 ms |
63056 KB |
Output is correct |
7 |
Correct |
329 ms |
63316 KB |
Output is correct |
8 |
Correct |
300 ms |
76372 KB |
Output is correct |
9 |
Correct |
233 ms |
61796 KB |
Output is correct |
10 |
Correct |
184 ms |
57284 KB |
Output is correct |
11 |
Correct |
295 ms |
75864 KB |
Output is correct |
12 |
Correct |
222 ms |
56144 KB |
Output is correct |
13 |
Correct |
229 ms |
48820 KB |
Output is correct |
14 |
Correct |
228 ms |
57688 KB |
Output is correct |
15 |
Correct |
246 ms |
75348 KB |
Output is correct |
16 |
Correct |
202 ms |
74068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
320 ms |
57556 KB |
Output is correct |
3 |
Correct |
339 ms |
57872 KB |
Output is correct |
4 |
Correct |
265 ms |
70996 KB |
Output is correct |
5 |
Correct |
230 ms |
58204 KB |
Output is correct |
6 |
Correct |
321 ms |
62292 KB |
Output is correct |
7 |
Correct |
267 ms |
75352 KB |
Output is correct |
8 |
Correct |
274 ms |
75528 KB |
Output is correct |
9 |
Correct |
204 ms |
74020 KB |
Output is correct |
10 |
Correct |
213 ms |
60752 KB |
Output is correct |
11 |
Correct |
253 ms |
55120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
326 ms |
59220 KB |
Output is correct |
3 |
Correct |
339 ms |
63572 KB |
Output is correct |
4 |
Correct |
348 ms |
66128 KB |
Output is correct |
5 |
Correct |
378 ms |
76120 KB |
Output is correct |
6 |
Correct |
322 ms |
72784 KB |
Output is correct |
7 |
Correct |
291 ms |
76992 KB |
Output is correct |
8 |
Correct |
192 ms |
61772 KB |
Output is correct |
9 |
Correct |
169 ms |
31304 KB |
Output is correct |
10 |
Correct |
325 ms |
58332 KB |
Output is correct |
11 |
Correct |
255 ms |
55120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
2 ms |
860 KB |
Output is correct |
5 |
Correct |
2 ms |
860 KB |
Output is correct |
6 |
Correct |
2 ms |
860 KB |
Output is correct |
7 |
Correct |
2 ms |
860 KB |
Output is correct |
8 |
Correct |
2 ms |
600 KB |
Output is correct |
9 |
Correct |
2 ms |
860 KB |
Output is correct |
10 |
Correct |
2 ms |
860 KB |
Output is correct |
11 |
Correct |
2 ms |
860 KB |
Output is correct |
12 |
Correct |
2 ms |
860 KB |
Output is correct |
13 |
Correct |
2 ms |
860 KB |
Output is correct |
14 |
Correct |
2 ms |
860 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
239 ms |
63196 KB |
Output is correct |
18 |
Correct |
258 ms |
58716 KB |
Output is correct |
19 |
Correct |
288 ms |
61264 KB |
Output is correct |
20 |
Correct |
318 ms |
63056 KB |
Output is correct |
21 |
Correct |
329 ms |
63316 KB |
Output is correct |
22 |
Correct |
300 ms |
76372 KB |
Output is correct |
23 |
Correct |
233 ms |
61796 KB |
Output is correct |
24 |
Correct |
184 ms |
57284 KB |
Output is correct |
25 |
Correct |
295 ms |
75864 KB |
Output is correct |
26 |
Correct |
222 ms |
56144 KB |
Output is correct |
27 |
Correct |
229 ms |
48820 KB |
Output is correct |
28 |
Correct |
228 ms |
57688 KB |
Output is correct |
29 |
Correct |
246 ms |
75348 KB |
Output is correct |
30 |
Correct |
202 ms |
74068 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
320 ms |
57556 KB |
Output is correct |
33 |
Correct |
339 ms |
57872 KB |
Output is correct |
34 |
Correct |
265 ms |
70996 KB |
Output is correct |
35 |
Correct |
230 ms |
58204 KB |
Output is correct |
36 |
Correct |
321 ms |
62292 KB |
Output is correct |
37 |
Correct |
267 ms |
75352 KB |
Output is correct |
38 |
Correct |
274 ms |
75528 KB |
Output is correct |
39 |
Correct |
204 ms |
74020 KB |
Output is correct |
40 |
Correct |
213 ms |
60752 KB |
Output is correct |
41 |
Correct |
253 ms |
55120 KB |
Output is correct |
42 |
Correct |
1 ms |
344 KB |
Output is correct |
43 |
Correct |
326 ms |
59220 KB |
Output is correct |
44 |
Correct |
339 ms |
63572 KB |
Output is correct |
45 |
Correct |
348 ms |
66128 KB |
Output is correct |
46 |
Correct |
378 ms |
76120 KB |
Output is correct |
47 |
Correct |
322 ms |
72784 KB |
Output is correct |
48 |
Correct |
291 ms |
76992 KB |
Output is correct |
49 |
Correct |
192 ms |
61772 KB |
Output is correct |
50 |
Correct |
169 ms |
31304 KB |
Output is correct |
51 |
Correct |
325 ms |
58332 KB |
Output is correct |
52 |
Correct |
255 ms |
55120 KB |
Output is correct |
53 |
Correct |
0 ms |
344 KB |
Output is correct |
54 |
Correct |
0 ms |
348 KB |
Output is correct |
55 |
Correct |
0 ms |
348 KB |
Output is correct |
56 |
Correct |
2 ms |
960 KB |
Output is correct |
57 |
Correct |
3 ms |
860 KB |
Output is correct |
58 |
Correct |
3 ms |
796 KB |
Output is correct |
59 |
Correct |
2 ms |
800 KB |
Output is correct |
60 |
Correct |
2 ms |
860 KB |
Output is correct |
61 |
Correct |
2 ms |
856 KB |
Output is correct |
62 |
Correct |
2 ms |
860 KB |
Output is correct |
63 |
Correct |
2 ms |
860 KB |
Output is correct |
64 |
Correct |
2 ms |
960 KB |
Output is correct |
65 |
Correct |
2 ms |
856 KB |
Output is correct |
66 |
Correct |
2 ms |
860 KB |
Output is correct |
67 |
Correct |
238 ms |
67664 KB |
Output is correct |
68 |
Correct |
236 ms |
58720 KB |
Output is correct |
69 |
Correct |
273 ms |
61184 KB |
Output is correct |
70 |
Correct |
311 ms |
63192 KB |
Output is correct |
71 |
Correct |
303 ms |
63316 KB |
Output is correct |
72 |
Correct |
361 ms |
76284 KB |
Output is correct |
73 |
Correct |
236 ms |
61664 KB |
Output is correct |
74 |
Correct |
190 ms |
57276 KB |
Output is correct |
75 |
Correct |
264 ms |
75996 KB |
Output is correct |
76 |
Correct |
220 ms |
56144 KB |
Output is correct |
77 |
Correct |
229 ms |
48724 KB |
Output is correct |
78 |
Correct |
217 ms |
57684 KB |
Output is correct |
79 |
Correct |
241 ms |
75344 KB |
Output is correct |
80 |
Correct |
208 ms |
74056 KB |
Output is correct |
81 |
Correct |
321 ms |
61788 KB |
Output is correct |
82 |
Correct |
325 ms |
62288 KB |
Output is correct |
83 |
Correct |
278 ms |
75680 KB |
Output is correct |
84 |
Correct |
224 ms |
58196 KB |
Output is correct |
85 |
Correct |
295 ms |
62308 KB |
Output is correct |
86 |
Correct |
271 ms |
75348 KB |
Output is correct |
87 |
Correct |
269 ms |
75340 KB |
Output is correct |
88 |
Correct |
223 ms |
60756 KB |
Output is correct |
89 |
Correct |
254 ms |
55172 KB |
Output is correct |
90 |
Correct |
350 ms |
63572 KB |
Output is correct |
91 |
Correct |
306 ms |
63572 KB |
Output is correct |
92 |
Correct |
344 ms |
66132 KB |
Output is correct |
93 |
Correct |
370 ms |
76216 KB |
Output is correct |
94 |
Correct |
325 ms |
72788 KB |
Output is correct |
95 |
Correct |
294 ms |
76996 KB |
Output is correct |
96 |
Correct |
206 ms |
61780 KB |
Output is correct |
97 |
Correct |
152 ms |
31312 KB |
Output is correct |
98 |
Correct |
327 ms |
58192 KB |
Output is correct |
99 |
Correct |
291 ms |
55124 KB |
Output is correct |
100 |
Correct |
67 ms |
7652 KB |
Output is correct |
101 |
Correct |
320 ms |
63056 KB |
Output is correct |
102 |
Correct |
275 ms |
61268 KB |
Output is correct |
103 |
Correct |
272 ms |
55372 KB |
Output is correct |
104 |
Correct |
364 ms |
62212 KB |
Output is correct |