#include <bits/stdc++.h>
using namespace std;
int fnd(int u, vector<int> &p){ // performs path compression
if(p[u] < 0) return u;
return p[u] = fnd(p[u], p);
}
void mrg(int u, int v, vector<int> &p, vector<long long> &strength){
u = fnd(u,p), v = fnd(v,p);
if(u==v) return;
if(p[u] > p[v]) swap(u,v); // performs size optimization
p[u] += p[v];
p[v] = u;
strength[u] += strength[v];
}
void dfs(int u, int flag, vector<int> &canwin, vector<vector<int>> &mark){
canwin[u] &= flag;
for(int v : mark[u]) dfs(v, canwin[u], canwin, mark);
}
int main(){
cin.tie(0);
ios::sync_with_stdio(0);
int N, M;
cin >> N >> M;
vector<int> s(N);
for(int &x : s) cin >> x;
vector<vector<int>> g(N);
for(int i = 0, u, v; i < M; i++){
cin >> u >> v;
u--, v--;
g[u].push_back(v), g[v].push_back(u);
}
vector<int> canwin(N,1), p(N,-1), vis(N), inserted(N), rep(N);
vector<long long> strength(N);
vector<vector<int>> mark(N);
vector<pair<int,int>> order(N);
for(int i = 0; i < N; i++){
strength[i] = s[i];
order[i] = {s[i],i};
}
sort(order.begin(), order.end());
for(auto t : order){ // insert nodes in increasing order of weight
int u = t.second;
for(int v : g[u]) if(inserted[v]){
int w = rep[fnd(v,p)]; // components representant
if(strength[fnd(v,p)] < s[u]){ // this component cannot win
canwin[w] = 0;
}
if(!vis[w]){ // build structure for marking
mark[u].push_back(w);
vis[w] = 1;
}
}
for(int v : g[u]) if(inserted[v]) vis[rep[fnd(v,p)]] = 0;
for(int v : g[u]) if(inserted[v]) mrg(u,v,p,strength);
rep[fnd(u,p)] = u;
inserted[u] = 1;
}
dfs(order[N-1].second,1,canwin,mark); // propagate marks
for(int i = 0; i < N; i++) cout << canwin[i];
cout << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
3 ms |
468 KB |
Output is correct |
5 |
Correct |
2 ms |
596 KB |
Output is correct |
6 |
Correct |
2 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
476 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
2 ms |
596 KB |
Output is correct |
10 |
Correct |
3 ms |
596 KB |
Output is correct |
11 |
Correct |
3 ms |
468 KB |
Output is correct |
12 |
Correct |
3 ms |
468 KB |
Output is correct |
13 |
Correct |
2 ms |
596 KB |
Output is correct |
14 |
Correct |
3 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
190 ms |
32048 KB |
Output is correct |
4 |
Correct |
206 ms |
38628 KB |
Output is correct |
5 |
Correct |
286 ms |
27080 KB |
Output is correct |
6 |
Correct |
292 ms |
27916 KB |
Output is correct |
7 |
Correct |
269 ms |
27976 KB |
Output is correct |
8 |
Correct |
250 ms |
39652 KB |
Output is correct |
9 |
Correct |
205 ms |
27076 KB |
Output is correct |
10 |
Correct |
159 ms |
26064 KB |
Output is correct |
11 |
Correct |
189 ms |
40452 KB |
Output is correct |
12 |
Correct |
303 ms |
30604 KB |
Output is correct |
13 |
Correct |
224 ms |
39652 KB |
Output is correct |
14 |
Correct |
215 ms |
38672 KB |
Output is correct |
15 |
Correct |
226 ms |
39608 KB |
Output is correct |
16 |
Correct |
138 ms |
39128 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
311 ms |
28004 KB |
Output is correct |
3 |
Correct |
282 ms |
28084 KB |
Output is correct |
4 |
Correct |
258 ms |
39568 KB |
Output is correct |
5 |
Correct |
182 ms |
36656 KB |
Output is correct |
6 |
Correct |
291 ms |
28088 KB |
Output is correct |
7 |
Correct |
212 ms |
39608 KB |
Output is correct |
8 |
Correct |
236 ms |
39556 KB |
Output is correct |
9 |
Correct |
176 ms |
39248 KB |
Output is correct |
10 |
Correct |
183 ms |
31740 KB |
Output is correct |
11 |
Correct |
208 ms |
29916 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
274 ms |
28456 KB |
Output is correct |
3 |
Correct |
282 ms |
28064 KB |
Output is correct |
4 |
Correct |
323 ms |
28024 KB |
Output is correct |
5 |
Correct |
255 ms |
28008 KB |
Output is correct |
6 |
Correct |
266 ms |
27868 KB |
Output is correct |
7 |
Correct |
166 ms |
33788 KB |
Output is correct |
8 |
Correct |
144 ms |
39656 KB |
Output is correct |
9 |
Correct |
164 ms |
15860 KB |
Output is correct |
10 |
Correct |
264 ms |
27996 KB |
Output is correct |
11 |
Correct |
227 ms |
29932 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
3 ms |
468 KB |
Output is correct |
5 |
Correct |
2 ms |
596 KB |
Output is correct |
6 |
Correct |
2 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
476 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
2 ms |
596 KB |
Output is correct |
10 |
Correct |
3 ms |
596 KB |
Output is correct |
11 |
Correct |
3 ms |
468 KB |
Output is correct |
12 |
Correct |
3 ms |
468 KB |
Output is correct |
13 |
Correct |
2 ms |
596 KB |
Output is correct |
14 |
Correct |
3 ms |
596 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
190 ms |
32048 KB |
Output is correct |
18 |
Correct |
206 ms |
38628 KB |
Output is correct |
19 |
Correct |
286 ms |
27080 KB |
Output is correct |
20 |
Correct |
292 ms |
27916 KB |
Output is correct |
21 |
Correct |
269 ms |
27976 KB |
Output is correct |
22 |
Correct |
250 ms |
39652 KB |
Output is correct |
23 |
Correct |
205 ms |
27076 KB |
Output is correct |
24 |
Correct |
159 ms |
26064 KB |
Output is correct |
25 |
Correct |
189 ms |
40452 KB |
Output is correct |
26 |
Correct |
303 ms |
30604 KB |
Output is correct |
27 |
Correct |
224 ms |
39652 KB |
Output is correct |
28 |
Correct |
215 ms |
38672 KB |
Output is correct |
29 |
Correct |
226 ms |
39608 KB |
Output is correct |
30 |
Correct |
138 ms |
39128 KB |
Output is correct |
31 |
Correct |
1 ms |
212 KB |
Output is correct |
32 |
Correct |
311 ms |
28004 KB |
Output is correct |
33 |
Correct |
282 ms |
28084 KB |
Output is correct |
34 |
Correct |
258 ms |
39568 KB |
Output is correct |
35 |
Correct |
182 ms |
36656 KB |
Output is correct |
36 |
Correct |
291 ms |
28088 KB |
Output is correct |
37 |
Correct |
212 ms |
39608 KB |
Output is correct |
38 |
Correct |
236 ms |
39556 KB |
Output is correct |
39 |
Correct |
176 ms |
39248 KB |
Output is correct |
40 |
Correct |
183 ms |
31740 KB |
Output is correct |
41 |
Correct |
208 ms |
29916 KB |
Output is correct |
42 |
Correct |
0 ms |
212 KB |
Output is correct |
43 |
Correct |
274 ms |
28456 KB |
Output is correct |
44 |
Correct |
282 ms |
28064 KB |
Output is correct |
45 |
Correct |
323 ms |
28024 KB |
Output is correct |
46 |
Correct |
255 ms |
28008 KB |
Output is correct |
47 |
Correct |
266 ms |
27868 KB |
Output is correct |
48 |
Correct |
166 ms |
33788 KB |
Output is correct |
49 |
Correct |
144 ms |
39656 KB |
Output is correct |
50 |
Correct |
164 ms |
15860 KB |
Output is correct |
51 |
Correct |
264 ms |
27996 KB |
Output is correct |
52 |
Correct |
227 ms |
29932 KB |
Output is correct |
53 |
Correct |
0 ms |
212 KB |
Output is correct |
54 |
Correct |
0 ms |
212 KB |
Output is correct |
55 |
Correct |
1 ms |
212 KB |
Output is correct |
56 |
Correct |
2 ms |
468 KB |
Output is correct |
57 |
Correct |
3 ms |
596 KB |
Output is correct |
58 |
Correct |
2 ms |
468 KB |
Output is correct |
59 |
Correct |
2 ms |
468 KB |
Output is correct |
60 |
Correct |
3 ms |
468 KB |
Output is correct |
61 |
Correct |
2 ms |
596 KB |
Output is correct |
62 |
Correct |
4 ms |
596 KB |
Output is correct |
63 |
Correct |
2 ms |
596 KB |
Output is correct |
64 |
Correct |
2 ms |
468 KB |
Output is correct |
65 |
Correct |
2 ms |
596 KB |
Output is correct |
66 |
Correct |
2 ms |
596 KB |
Output is correct |
67 |
Correct |
186 ms |
32040 KB |
Output is correct |
68 |
Correct |
192 ms |
38736 KB |
Output is correct |
69 |
Correct |
274 ms |
27052 KB |
Output is correct |
70 |
Correct |
290 ms |
27916 KB |
Output is correct |
71 |
Correct |
264 ms |
28020 KB |
Output is correct |
72 |
Correct |
228 ms |
39732 KB |
Output is correct |
73 |
Correct |
175 ms |
27084 KB |
Output is correct |
74 |
Correct |
153 ms |
26048 KB |
Output is correct |
75 |
Correct |
162 ms |
40324 KB |
Output is correct |
76 |
Correct |
250 ms |
30640 KB |
Output is correct |
77 |
Correct |
199 ms |
39552 KB |
Output is correct |
78 |
Correct |
169 ms |
38712 KB |
Output is correct |
79 |
Correct |
242 ms |
39544 KB |
Output is correct |
80 |
Correct |
128 ms |
39148 KB |
Output is correct |
81 |
Correct |
294 ms |
27940 KB |
Output is correct |
82 |
Correct |
333 ms |
28116 KB |
Output is correct |
83 |
Correct |
264 ms |
39584 KB |
Output is correct |
84 |
Correct |
178 ms |
36636 KB |
Output is correct |
85 |
Correct |
277 ms |
28096 KB |
Output is correct |
86 |
Correct |
259 ms |
39548 KB |
Output is correct |
87 |
Correct |
183 ms |
39620 KB |
Output is correct |
88 |
Correct |
185 ms |
31732 KB |
Output is correct |
89 |
Correct |
180 ms |
29900 KB |
Output is correct |
90 |
Correct |
314 ms |
28416 KB |
Output is correct |
91 |
Correct |
319 ms |
28000 KB |
Output is correct |
92 |
Correct |
350 ms |
28044 KB |
Output is correct |
93 |
Correct |
348 ms |
28080 KB |
Output is correct |
94 |
Correct |
284 ms |
27944 KB |
Output is correct |
95 |
Correct |
210 ms |
33756 KB |
Output is correct |
96 |
Correct |
149 ms |
39540 KB |
Output is correct |
97 |
Correct |
162 ms |
15876 KB |
Output is correct |
98 |
Correct |
216 ms |
28028 KB |
Output is correct |
99 |
Correct |
189 ms |
29884 KB |
Output is correct |
100 |
Correct |
46 ms |
3056 KB |
Output is correct |
101 |
Correct |
286 ms |
28044 KB |
Output is correct |
102 |
Correct |
231 ms |
24924 KB |
Output is correct |
103 |
Correct |
287 ms |
26144 KB |
Output is correct |
104 |
Correct |
330 ms |
28484 KB |
Output is correct |