#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int, int>
#define vb vector<bool>
#define vi vector<ll>
#define vpii vector<pii>
#define vvpii vector<vpii>
#define vvi vector<vi>
#define tiii tuple<int, int, int>
#define vtiii vector<tiii>
#define sz(x) ((int)(x).size())
#define all(x) begin(x), end(x)
struct DSU
{
vector<int> arr;
vector<ll> sum;
vvi nodes;
void init(vi &s)
{
arr.resize(sz(s)), sum.resize(sz(s)), nodes.resize(sz(s));
for (int i = 0; i < sz(s); ++i)
arr[i] = i, sum[i] = s[i], nodes[i].push_back(i);
}
int find(int i)
{
if (arr[i] == i)
return i;
return arr[i] = find(arr[i]);
}
void clear(int i)
{
i = find(i);
nodes[i].clear();
}
void join(int a, int b)
{
a = find(a);
b = find(b);
if (a == b)
return;
if (sz(nodes[a]) > sz(nodes[b]))
swap(a, b);
sum[b] += sum[a];
for (int x : nodes[a])
nodes[b].push_back(x);
nodes[a].clear();
arr[a] = b;
}
ll getsum(int i)
{
i = find(i);
return sum[i];
}
void answer(int i, vi &ans)
{
i = find(i);
for (int j : nodes[i])
ans[j] = true;
}
};
int main()
{
ios::sync_with_stdio(false);
cout.tie(0);
cin.tie(0);
int n, m;
cin >> n >> m;
vi s(n);
vvi adj(n);
for (int i = 0; i < n; ++i)
cin >> s[i];
for (int i = 0, a, b; i < m; ++i)
{
cin >> a >> b;
--a, --b;
adj[a].push_back(b);
adj[b].push_back(a);
}
vi order(n);
iota(all(order), 0);
sort(all(order), [&](ll &a, ll &b)
{ return s[a] < s[b]; });
DSU dsu;
dsu.init(s);
for (int v : order)
{
for (int w : adj[v])
{
if (s[w] > s[v])
continue; // neighbor is larger, I will go the other direction later
if (dsu.getsum(w) < s[v])
{ // the nodes of the other subtree aren't enough to spread their tie
dsu.clear(w);
}
dsu.join(w, v);
}
/*
for (int tmp = 0; tmp < n; ++tmp)
{
cout << tmp << " : (" << dsu.find(tmp) << ") : " << dsu.getsum(tmp) << " mit ";
for (int w : dsu.nodes[dsu.find(tmp)])
cout << " " << w;
cout << endl;
}
*/
}
vi ans(n, 0);
dsu.answer(order.back(), ans);
for (int x : ans)
cout << x;
cout << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
324 KB |
Output is correct |
4 |
Correct |
6 ms |
596 KB |
Output is correct |
5 |
Correct |
2 ms |
596 KB |
Output is correct |
6 |
Correct |
1 ms |
532 KB |
Output is correct |
7 |
Correct |
2 ms |
596 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
2 ms |
668 KB |
Output is correct |
10 |
Correct |
2 ms |
724 KB |
Output is correct |
11 |
Correct |
2 ms |
596 KB |
Output is correct |
12 |
Correct |
3 ms |
724 KB |
Output is correct |
13 |
Correct |
1 ms |
592 KB |
Output is correct |
14 |
Correct |
3 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
324 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
184 ms |
36476 KB |
Output is correct |
4 |
Correct |
163 ms |
41340 KB |
Output is correct |
5 |
Correct |
166 ms |
38936 KB |
Output is correct |
6 |
Correct |
201 ms |
40440 KB |
Output is correct |
7 |
Correct |
177 ms |
40328 KB |
Output is correct |
8 |
Correct |
216 ms |
40268 KB |
Output is correct |
9 |
Correct |
222 ms |
43276 KB |
Output is correct |
10 |
Correct |
116 ms |
34584 KB |
Output is correct |
11 |
Correct |
122 ms |
36160 KB |
Output is correct |
12 |
Correct |
198 ms |
37220 KB |
Output is correct |
13 |
Correct |
173 ms |
41392 KB |
Output is correct |
14 |
Correct |
157 ms |
40712 KB |
Output is correct |
15 |
Correct |
142 ms |
35624 KB |
Output is correct |
16 |
Correct |
98 ms |
34544 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
203 ms |
40840 KB |
Output is correct |
3 |
Correct |
214 ms |
41288 KB |
Output is correct |
4 |
Correct |
168 ms |
42964 KB |
Output is correct |
5 |
Correct |
144 ms |
40784 KB |
Output is correct |
6 |
Correct |
218 ms |
40988 KB |
Output is correct |
7 |
Correct |
161 ms |
35604 KB |
Output is correct |
8 |
Correct |
156 ms |
35620 KB |
Output is correct |
9 |
Correct |
92 ms |
34560 KB |
Output is correct |
10 |
Correct |
141 ms |
37056 KB |
Output is correct |
11 |
Correct |
165 ms |
39312 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
205 ms |
37748 KB |
Output is correct |
3 |
Correct |
199 ms |
38648 KB |
Output is correct |
4 |
Correct |
215 ms |
40376 KB |
Output is correct |
5 |
Correct |
217 ms |
42284 KB |
Output is correct |
6 |
Correct |
189 ms |
38068 KB |
Output is correct |
7 |
Correct |
138 ms |
37232 KB |
Output is correct |
8 |
Correct |
104 ms |
40284 KB |
Output is correct |
9 |
Correct |
105 ms |
21364 KB |
Output is correct |
10 |
Correct |
203 ms |
41948 KB |
Output is correct |
11 |
Correct |
169 ms |
39564 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 |
1 ms |
324 KB |
Output is correct |
4 |
Correct |
6 ms |
596 KB |
Output is correct |
5 |
Correct |
2 ms |
596 KB |
Output is correct |
6 |
Correct |
1 ms |
532 KB |
Output is correct |
7 |
Correct |
2 ms |
596 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
2 ms |
668 KB |
Output is correct |
10 |
Correct |
2 ms |
724 KB |
Output is correct |
11 |
Correct |
2 ms |
596 KB |
Output is correct |
12 |
Correct |
3 ms |
724 KB |
Output is correct |
13 |
Correct |
1 ms |
592 KB |
Output is correct |
14 |
Correct |
3 ms |
596 KB |
Output is correct |
15 |
Correct |
1 ms |
324 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
184 ms |
36476 KB |
Output is correct |
18 |
Correct |
163 ms |
41340 KB |
Output is correct |
19 |
Correct |
166 ms |
38936 KB |
Output is correct |
20 |
Correct |
201 ms |
40440 KB |
Output is correct |
21 |
Correct |
177 ms |
40328 KB |
Output is correct |
22 |
Correct |
216 ms |
40268 KB |
Output is correct |
23 |
Correct |
222 ms |
43276 KB |
Output is correct |
24 |
Correct |
116 ms |
34584 KB |
Output is correct |
25 |
Correct |
122 ms |
36160 KB |
Output is correct |
26 |
Correct |
198 ms |
37220 KB |
Output is correct |
27 |
Correct |
173 ms |
41392 KB |
Output is correct |
28 |
Correct |
157 ms |
40712 KB |
Output is correct |
29 |
Correct |
142 ms |
35624 KB |
Output is correct |
30 |
Correct |
98 ms |
34544 KB |
Output is correct |
31 |
Correct |
1 ms |
212 KB |
Output is correct |
32 |
Correct |
203 ms |
40840 KB |
Output is correct |
33 |
Correct |
214 ms |
41288 KB |
Output is correct |
34 |
Correct |
168 ms |
42964 KB |
Output is correct |
35 |
Correct |
144 ms |
40784 KB |
Output is correct |
36 |
Correct |
218 ms |
40988 KB |
Output is correct |
37 |
Correct |
161 ms |
35604 KB |
Output is correct |
38 |
Correct |
156 ms |
35620 KB |
Output is correct |
39 |
Correct |
92 ms |
34560 KB |
Output is correct |
40 |
Correct |
141 ms |
37056 KB |
Output is correct |
41 |
Correct |
165 ms |
39312 KB |
Output is correct |
42 |
Correct |
0 ms |
212 KB |
Output is correct |
43 |
Correct |
205 ms |
37748 KB |
Output is correct |
44 |
Correct |
199 ms |
38648 KB |
Output is correct |
45 |
Correct |
215 ms |
40376 KB |
Output is correct |
46 |
Correct |
217 ms |
42284 KB |
Output is correct |
47 |
Correct |
189 ms |
38068 KB |
Output is correct |
48 |
Correct |
138 ms |
37232 KB |
Output is correct |
49 |
Correct |
104 ms |
40284 KB |
Output is correct |
50 |
Correct |
105 ms |
21364 KB |
Output is correct |
51 |
Correct |
203 ms |
41948 KB |
Output is correct |
52 |
Correct |
169 ms |
39564 KB |
Output is correct |
53 |
Correct |
0 ms |
212 KB |
Output is correct |
54 |
Correct |
0 ms |
320 KB |
Output is correct |
55 |
Correct |
0 ms |
212 KB |
Output is correct |
56 |
Correct |
1 ms |
596 KB |
Output is correct |
57 |
Correct |
2 ms |
704 KB |
Output is correct |
58 |
Correct |
1 ms |
596 KB |
Output is correct |
59 |
Correct |
2 ms |
596 KB |
Output is correct |
60 |
Correct |
1 ms |
464 KB |
Output is correct |
61 |
Correct |
1 ms |
588 KB |
Output is correct |
62 |
Correct |
2 ms |
596 KB |
Output is correct |
63 |
Correct |
2 ms |
596 KB |
Output is correct |
64 |
Correct |
2 ms |
724 KB |
Output is correct |
65 |
Correct |
1 ms |
596 KB |
Output is correct |
66 |
Correct |
2 ms |
584 KB |
Output is correct |
67 |
Correct |
146 ms |
36492 KB |
Output is correct |
68 |
Correct |
154 ms |
41372 KB |
Output is correct |
69 |
Correct |
161 ms |
38844 KB |
Output is correct |
70 |
Correct |
177 ms |
40368 KB |
Output is correct |
71 |
Correct |
168 ms |
40312 KB |
Output is correct |
72 |
Correct |
197 ms |
40220 KB |
Output is correct |
73 |
Correct |
163 ms |
43264 KB |
Output is correct |
74 |
Correct |
109 ms |
34592 KB |
Output is correct |
75 |
Correct |
117 ms |
36096 KB |
Output is correct |
76 |
Correct |
183 ms |
37192 KB |
Output is correct |
77 |
Correct |
154 ms |
41456 KB |
Output is correct |
78 |
Correct |
148 ms |
40720 KB |
Output is correct |
79 |
Correct |
132 ms |
35516 KB |
Output is correct |
80 |
Correct |
93 ms |
34588 KB |
Output is correct |
81 |
Correct |
208 ms |
40956 KB |
Output is correct |
82 |
Correct |
209 ms |
41268 KB |
Output is correct |
83 |
Correct |
151 ms |
42784 KB |
Output is correct |
84 |
Correct |
146 ms |
40916 KB |
Output is correct |
85 |
Correct |
197 ms |
40980 KB |
Output is correct |
86 |
Correct |
167 ms |
35584 KB |
Output is correct |
87 |
Correct |
157 ms |
35644 KB |
Output is correct |
88 |
Correct |
148 ms |
37148 KB |
Output is correct |
89 |
Correct |
159 ms |
39132 KB |
Output is correct |
90 |
Correct |
197 ms |
37824 KB |
Output is correct |
91 |
Correct |
200 ms |
38916 KB |
Output is correct |
92 |
Correct |
204 ms |
40344 KB |
Output is correct |
93 |
Correct |
209 ms |
42248 KB |
Output is correct |
94 |
Correct |
196 ms |
38104 KB |
Output is correct |
95 |
Correct |
131 ms |
37152 KB |
Output is correct |
96 |
Correct |
110 ms |
40256 KB |
Output is correct |
97 |
Correct |
104 ms |
21420 KB |
Output is correct |
98 |
Correct |
195 ms |
42000 KB |
Output is correct |
99 |
Correct |
157 ms |
39548 KB |
Output is correct |
100 |
Correct |
38 ms |
7024 KB |
Output is correct |
101 |
Correct |
206 ms |
39072 KB |
Output is correct |
102 |
Correct |
170 ms |
31412 KB |
Output is correct |
103 |
Correct |
180 ms |
31560 KB |
Output is correct |
104 |
Correct |
209 ms |
36768 KB |
Output is correct |