#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <stack>
#include <queue>
#include <set>
#include <map>
using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
using pii = pair<int, int>;
using ll = long long;
using vpii = vector<pii>;
using pll = pair<ll, ll>;
using vpll = vector<pll>;
using vll = vector<ll>;
using vvll = vector<vll>;
using namespace std;
const int mx = 200'000;
int N, M;
vll s(1+mx);
vi sn(1+mx);
vi edge[1+mx];
vi children[1+mx];
struct dsu
{
vi parent = vi(1+mx);
vi subtree = vi(1+mx);
vi peak = vi(1+mx);
dsu()
{
for(int i = 1; i <= N; i++)
{
parent[i] = peak[i] = i;
subtree[i] = 1;
}
}
int root(int u)
{
if(parent[u] == u)
return u;
else
return (parent[u] = root(parent[u]));
}
bool join(int u, int v)
{
u = root(u);
v = root(v);
if(u == v) return 0;
if(subtree[u] < subtree[v])
swap(u, v);
subtree[u] += subtree[v];
parent[v] = u;
peak[u] = sn[peak[u]] < sn[peak[v]] ? peak[v] : peak[u];
return 1;
}
};
string res;
int rt;
vll ssum;
void dfs(int u)
{
res[u-1] = '1';
for(int v : children[u])
{
if(ssum[v] >= s[u])
dfs(v);
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> N >> M;
for(int i = 1; i <= N; i++)
cin >> s[i];
for(int e = 1; e <= M; e++)
{
int a, b;
cin >> a >> b;
edge[a].push_back(b);
edge[b].push_back(a);
}
vi ord;
for(int i = 1; i <= N; i++)
ord.push_back(i);
sort(ord.begin(), ord.end(), [] (int u, int v)
{
return s[u] < s[v];
});
for(int i = 0; i < N; i++)
sn[ord[i]] = i;
vi parent(1+N);
ssum = s;
dsu DSU;
for(int u : ord)
{
for(int v : edge[u])
{
if(sn[v] > sn[u]) continue;
if(DSU.root(u) == DSU.root(v)) continue;
int vp = DSU.peak[DSU.root(v)];
ssum[u] += ssum[vp];
parent[vp] = u;
children[u].push_back(vp);
DSU.join(u, vp);
}
}
// for(int u : ord)
// cerr << u << ' ';
// cerr << '\n';
// for(int u = 1; u <= N; u++)
// {
// cerr << "\n\n\n";
// cerr << "u = " << u << " : " << ssum[u] << '\n';
// for(int v : children[u])
// cerr << u << " -> " << v << '\n';
// }
res = string(N, '0');
rt = ord.back();
dfs(rt);
cout << res << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
15956 KB |
Output is correct |
2 |
Correct |
10 ms |
15956 KB |
Output is correct |
3 |
Correct |
13 ms |
15956 KB |
Output is correct |
4 |
Correct |
13 ms |
16084 KB |
Output is correct |
5 |
Correct |
12 ms |
16084 KB |
Output is correct |
6 |
Correct |
12 ms |
16124 KB |
Output is correct |
7 |
Correct |
11 ms |
16084 KB |
Output is correct |
8 |
Correct |
8 ms |
16084 KB |
Output is correct |
9 |
Correct |
9 ms |
16048 KB |
Output is correct |
10 |
Correct |
10 ms |
16084 KB |
Output is correct |
11 |
Correct |
9 ms |
16212 KB |
Output is correct |
12 |
Correct |
11 ms |
16080 KB |
Output is correct |
13 |
Correct |
11 ms |
16056 KB |
Output is correct |
14 |
Correct |
10 ms |
16084 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
15956 KB |
Output is correct |
2 |
Correct |
9 ms |
15956 KB |
Output is correct |
3 |
Correct |
174 ms |
33752 KB |
Output is correct |
4 |
Correct |
230 ms |
31116 KB |
Output is correct |
5 |
Correct |
207 ms |
28116 KB |
Output is correct |
6 |
Correct |
258 ms |
29476 KB |
Output is correct |
7 |
Correct |
264 ms |
28508 KB |
Output is correct |
8 |
Correct |
248 ms |
31888 KB |
Output is correct |
9 |
Correct |
199 ms |
29620 KB |
Output is correct |
10 |
Correct |
193 ms |
26864 KB |
Output is correct |
11 |
Correct |
146 ms |
26320 KB |
Output is correct |
12 |
Correct |
252 ms |
29136 KB |
Output is correct |
13 |
Correct |
190 ms |
31284 KB |
Output is correct |
14 |
Correct |
206 ms |
31816 KB |
Output is correct |
15 |
Correct |
217 ms |
40736 KB |
Output is correct |
16 |
Correct |
110 ms |
40860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
15956 KB |
Output is correct |
2 |
Correct |
265 ms |
31612 KB |
Output is correct |
3 |
Correct |
266 ms |
30740 KB |
Output is correct |
4 |
Correct |
210 ms |
31948 KB |
Output is correct |
5 |
Correct |
223 ms |
31304 KB |
Output is correct |
6 |
Correct |
269 ms |
29992 KB |
Output is correct |
7 |
Correct |
237 ms |
41480 KB |
Output is correct |
8 |
Correct |
252 ms |
41464 KB |
Output is correct |
9 |
Correct |
153 ms |
40632 KB |
Output is correct |
10 |
Correct |
224 ms |
28716 KB |
Output is correct |
11 |
Correct |
233 ms |
30904 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
15956 KB |
Output is correct |
2 |
Correct |
369 ms |
30536 KB |
Output is correct |
3 |
Correct |
291 ms |
31240 KB |
Output is correct |
4 |
Correct |
335 ms |
28964 KB |
Output is correct |
5 |
Correct |
206 ms |
32752 KB |
Output is correct |
6 |
Correct |
210 ms |
31800 KB |
Output is correct |
7 |
Correct |
138 ms |
29688 KB |
Output is correct |
8 |
Correct |
121 ms |
38184 KB |
Output is correct |
9 |
Correct |
116 ms |
24984 KB |
Output is correct |
10 |
Correct |
228 ms |
29288 KB |
Output is correct |
11 |
Correct |
218 ms |
29612 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
15956 KB |
Output is correct |
2 |
Correct |
10 ms |
15956 KB |
Output is correct |
3 |
Correct |
13 ms |
15956 KB |
Output is correct |
4 |
Correct |
13 ms |
16084 KB |
Output is correct |
5 |
Correct |
12 ms |
16084 KB |
Output is correct |
6 |
Correct |
12 ms |
16124 KB |
Output is correct |
7 |
Correct |
11 ms |
16084 KB |
Output is correct |
8 |
Correct |
8 ms |
16084 KB |
Output is correct |
9 |
Correct |
9 ms |
16048 KB |
Output is correct |
10 |
Correct |
10 ms |
16084 KB |
Output is correct |
11 |
Correct |
9 ms |
16212 KB |
Output is correct |
12 |
Correct |
11 ms |
16080 KB |
Output is correct |
13 |
Correct |
11 ms |
16056 KB |
Output is correct |
14 |
Correct |
10 ms |
16084 KB |
Output is correct |
15 |
Correct |
11 ms |
15956 KB |
Output is correct |
16 |
Correct |
9 ms |
15956 KB |
Output is correct |
17 |
Correct |
174 ms |
33752 KB |
Output is correct |
18 |
Correct |
230 ms |
31116 KB |
Output is correct |
19 |
Correct |
207 ms |
28116 KB |
Output is correct |
20 |
Correct |
258 ms |
29476 KB |
Output is correct |
21 |
Correct |
264 ms |
28508 KB |
Output is correct |
22 |
Correct |
248 ms |
31888 KB |
Output is correct |
23 |
Correct |
199 ms |
29620 KB |
Output is correct |
24 |
Correct |
193 ms |
26864 KB |
Output is correct |
25 |
Correct |
146 ms |
26320 KB |
Output is correct |
26 |
Correct |
252 ms |
29136 KB |
Output is correct |
27 |
Correct |
190 ms |
31284 KB |
Output is correct |
28 |
Correct |
206 ms |
31816 KB |
Output is correct |
29 |
Correct |
217 ms |
40736 KB |
Output is correct |
30 |
Correct |
110 ms |
40860 KB |
Output is correct |
31 |
Correct |
9 ms |
15956 KB |
Output is correct |
32 |
Correct |
265 ms |
31612 KB |
Output is correct |
33 |
Correct |
266 ms |
30740 KB |
Output is correct |
34 |
Correct |
210 ms |
31948 KB |
Output is correct |
35 |
Correct |
223 ms |
31304 KB |
Output is correct |
36 |
Correct |
269 ms |
29992 KB |
Output is correct |
37 |
Correct |
237 ms |
41480 KB |
Output is correct |
38 |
Correct |
252 ms |
41464 KB |
Output is correct |
39 |
Correct |
153 ms |
40632 KB |
Output is correct |
40 |
Correct |
224 ms |
28716 KB |
Output is correct |
41 |
Correct |
233 ms |
30904 KB |
Output is correct |
42 |
Correct |
10 ms |
15956 KB |
Output is correct |
43 |
Correct |
369 ms |
30536 KB |
Output is correct |
44 |
Correct |
291 ms |
31240 KB |
Output is correct |
45 |
Correct |
335 ms |
28964 KB |
Output is correct |
46 |
Correct |
206 ms |
32752 KB |
Output is correct |
47 |
Correct |
210 ms |
31800 KB |
Output is correct |
48 |
Correct |
138 ms |
29688 KB |
Output is correct |
49 |
Correct |
121 ms |
38184 KB |
Output is correct |
50 |
Correct |
116 ms |
24984 KB |
Output is correct |
51 |
Correct |
228 ms |
29288 KB |
Output is correct |
52 |
Correct |
218 ms |
29612 KB |
Output is correct |
53 |
Correct |
10 ms |
15920 KB |
Output is correct |
54 |
Correct |
10 ms |
15956 KB |
Output is correct |
55 |
Correct |
9 ms |
15896 KB |
Output is correct |
56 |
Correct |
14 ms |
16052 KB |
Output is correct |
57 |
Correct |
10 ms |
16056 KB |
Output is correct |
58 |
Correct |
10 ms |
16084 KB |
Output is correct |
59 |
Correct |
9 ms |
16084 KB |
Output is correct |
60 |
Correct |
9 ms |
16060 KB |
Output is correct |
61 |
Correct |
11 ms |
16084 KB |
Output is correct |
62 |
Correct |
13 ms |
16112 KB |
Output is correct |
63 |
Correct |
13 ms |
16056 KB |
Output is correct |
64 |
Correct |
11 ms |
16084 KB |
Output is correct |
65 |
Correct |
12 ms |
16104 KB |
Output is correct |
66 |
Correct |
13 ms |
16084 KB |
Output is correct |
67 |
Correct |
183 ms |
34952 KB |
Output is correct |
68 |
Correct |
186 ms |
31648 KB |
Output is correct |
69 |
Correct |
205 ms |
28632 KB |
Output is correct |
70 |
Correct |
238 ms |
29932 KB |
Output is correct |
71 |
Correct |
297 ms |
28300 KB |
Output is correct |
72 |
Correct |
274 ms |
31256 KB |
Output is correct |
73 |
Correct |
172 ms |
29288 KB |
Output is correct |
74 |
Correct |
173 ms |
27168 KB |
Output is correct |
75 |
Correct |
175 ms |
27444 KB |
Output is correct |
76 |
Correct |
180 ms |
29316 KB |
Output is correct |
77 |
Correct |
167 ms |
31072 KB |
Output is correct |
78 |
Correct |
158 ms |
31032 KB |
Output is correct |
79 |
Correct |
158 ms |
42236 KB |
Output is correct |
80 |
Correct |
114 ms |
43044 KB |
Output is correct |
81 |
Correct |
329 ms |
30580 KB |
Output is correct |
82 |
Correct |
219 ms |
31020 KB |
Output is correct |
83 |
Correct |
165 ms |
30148 KB |
Output is correct |
84 |
Correct |
161 ms |
31932 KB |
Output is correct |
85 |
Correct |
226 ms |
32768 KB |
Output is correct |
86 |
Correct |
188 ms |
44248 KB |
Output is correct |
87 |
Correct |
182 ms |
44144 KB |
Output is correct |
88 |
Correct |
142 ms |
28372 KB |
Output is correct |
89 |
Correct |
170 ms |
28620 KB |
Output is correct |
90 |
Correct |
221 ms |
31168 KB |
Output is correct |
91 |
Correct |
245 ms |
32748 KB |
Output is correct |
92 |
Correct |
212 ms |
32620 KB |
Output is correct |
93 |
Correct |
202 ms |
31676 KB |
Output is correct |
94 |
Correct |
186 ms |
32044 KB |
Output is correct |
95 |
Correct |
119 ms |
31424 KB |
Output is correct |
96 |
Correct |
115 ms |
37736 KB |
Output is correct |
97 |
Correct |
117 ms |
25488 KB |
Output is correct |
98 |
Correct |
167 ms |
31036 KB |
Output is correct |
99 |
Correct |
162 ms |
31228 KB |
Output is correct |
100 |
Correct |
46 ms |
20168 KB |
Output is correct |
101 |
Correct |
255 ms |
32552 KB |
Output is correct |
102 |
Correct |
162 ms |
30428 KB |
Output is correct |
103 |
Correct |
206 ms |
28980 KB |
Output is correct |
104 |
Correct |
246 ms |
29220 KB |
Output is correct |