#include <bits/stdc++.h>
using std::vector;
using std::array;
using std::pair;
using std::tuple;
using ll = long long;
template <class F> struct fixed : private F {
explicit fixed(F&& f) : F(std::forward<F>(f)) {}
template <class... Args> decltype(auto) operator()(Args&&... args) const {
return F::operator()(*this, std::forward<Args>(args)...);
}
};
template <class T>
constexpr T infty = std::numeric_limits<T>::max() / 2;
struct UnionFind {
vector<int> par;
explicit UnionFind(const int n) : par(n, -1) {}
int find(int u) {
return par[u] < 0 ? u : par[u] = find(par[u]);
}
pair<int, bool> merge(int u, int v) {
u = find(u);
v = find(v);
if (u == v) {
return {u, false};
}
if (par[u] > par[v]) {
std::swap(u, v);
}
par[u] += par[v];
par[v] = u;
return {u, true};
}
};
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
int N, M;
std::cin >> N >> M;
vector<int> P(N);
for (auto& x : P) {
std::cin >> x;
}
vector<vector<int>> graph(N);
for (int i = 0; i < M; ++i) {
int u, v;
std::cin >> u >> v;
u -= 1, v -= 1;
graph[u].push_back(v);
graph[v].push_back(u);
}
vector<int> ord(N);
std::iota(ord.begin(), ord.end(), 0);
std::sort(ord.begin(), ord.end(), [&](const int i, const int j) {
return P[i] < P[j];
});
vector<char> seen(N);
vector<int> ans(N, 1);
UnionFind dsu(N);
vector<vector<int>> list(N);
vector<ll> sum(N);
for (const int u : ord) {
vector<int> new_list = {u};
ll new_sum = P[u];
int root = u;
for (const int v : graph[u]) {
if (seen[v]) {
const int vr = dsu.find(v);
const auto [w, f] = dsu.merge(u, vr);
if (f) {
if (sum[vr] >= P[u]) {
auto& add = list[vr];
if (add.size() > new_list.size()) {
std::swap(add, new_list);
}
for (const int x : add) {
new_list.push_back(x);
}
} else {
for (const int x : list[vr]) {
ans[x] = false;
}
}
new_sum += sum[vr];
root = w;
}
}
}
list[root] = std::move(new_list);
sum[root] = new_sum;
seen[u] = true;
}
for (const int x : ans) {
std::cout << x;
}
std::cout << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
2 ms |
460 KB |
Output is correct |
5 |
Correct |
2 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
2 ms |
468 KB |
Output is correct |
10 |
Correct |
2 ms |
468 KB |
Output is correct |
11 |
Correct |
2 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
596 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
2 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
170 ms |
25512 KB |
Output is correct |
4 |
Correct |
148 ms |
25644 KB |
Output is correct |
5 |
Correct |
237 ms |
26512 KB |
Output is correct |
6 |
Correct |
191 ms |
28356 KB |
Output is correct |
7 |
Correct |
205 ms |
27268 KB |
Output is correct |
8 |
Correct |
199 ms |
27364 KB |
Output is correct |
9 |
Correct |
160 ms |
31264 KB |
Output is correct |
10 |
Correct |
167 ms |
28752 KB |
Output is correct |
11 |
Correct |
130 ms |
29632 KB |
Output is correct |
12 |
Correct |
206 ms |
26716 KB |
Output is correct |
13 |
Correct |
151 ms |
25444 KB |
Output is correct |
14 |
Correct |
137 ms |
25156 KB |
Output is correct |
15 |
Correct |
143 ms |
22776 KB |
Output is correct |
16 |
Correct |
89 ms |
22984 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
221 ms |
28420 KB |
Output is correct |
3 |
Correct |
221 ms |
28836 KB |
Output is correct |
4 |
Correct |
144 ms |
25912 KB |
Output is correct |
5 |
Correct |
136 ms |
25588 KB |
Output is correct |
6 |
Correct |
240 ms |
28772 KB |
Output is correct |
7 |
Correct |
164 ms |
23216 KB |
Output is correct |
8 |
Correct |
159 ms |
23340 KB |
Output is correct |
9 |
Correct |
98 ms |
22820 KB |
Output is correct |
10 |
Correct |
151 ms |
26756 KB |
Output is correct |
11 |
Correct |
161 ms |
27848 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
224 ms |
26368 KB |
Output is correct |
3 |
Correct |
219 ms |
27240 KB |
Output is correct |
4 |
Correct |
218 ms |
27460 KB |
Output is correct |
5 |
Correct |
199 ms |
28632 KB |
Output is correct |
6 |
Correct |
170 ms |
26024 KB |
Output is correct |
7 |
Correct |
126 ms |
29680 KB |
Output is correct |
8 |
Correct |
106 ms |
24352 KB |
Output is correct |
9 |
Correct |
100 ms |
13868 KB |
Output is correct |
10 |
Correct |
182 ms |
29192 KB |
Output is correct |
11 |
Correct |
176 ms |
27708 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
2 ms |
460 KB |
Output is correct |
5 |
Correct |
2 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
2 ms |
468 KB |
Output is correct |
10 |
Correct |
2 ms |
468 KB |
Output is correct |
11 |
Correct |
2 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
596 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
2 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
170 ms |
25512 KB |
Output is correct |
18 |
Correct |
148 ms |
25644 KB |
Output is correct |
19 |
Correct |
237 ms |
26512 KB |
Output is correct |
20 |
Correct |
191 ms |
28356 KB |
Output is correct |
21 |
Correct |
205 ms |
27268 KB |
Output is correct |
22 |
Correct |
199 ms |
27364 KB |
Output is correct |
23 |
Correct |
160 ms |
31264 KB |
Output is correct |
24 |
Correct |
167 ms |
28752 KB |
Output is correct |
25 |
Correct |
130 ms |
29632 KB |
Output is correct |
26 |
Correct |
206 ms |
26716 KB |
Output is correct |
27 |
Correct |
151 ms |
25444 KB |
Output is correct |
28 |
Correct |
137 ms |
25156 KB |
Output is correct |
29 |
Correct |
143 ms |
22776 KB |
Output is correct |
30 |
Correct |
89 ms |
22984 KB |
Output is correct |
31 |
Correct |
0 ms |
212 KB |
Output is correct |
32 |
Correct |
221 ms |
28420 KB |
Output is correct |
33 |
Correct |
221 ms |
28836 KB |
Output is correct |
34 |
Correct |
144 ms |
25912 KB |
Output is correct |
35 |
Correct |
136 ms |
25588 KB |
Output is correct |
36 |
Correct |
240 ms |
28772 KB |
Output is correct |
37 |
Correct |
164 ms |
23216 KB |
Output is correct |
38 |
Correct |
159 ms |
23340 KB |
Output is correct |
39 |
Correct |
98 ms |
22820 KB |
Output is correct |
40 |
Correct |
151 ms |
26756 KB |
Output is correct |
41 |
Correct |
161 ms |
27848 KB |
Output is correct |
42 |
Correct |
0 ms |
212 KB |
Output is correct |
43 |
Correct |
224 ms |
26368 KB |
Output is correct |
44 |
Correct |
219 ms |
27240 KB |
Output is correct |
45 |
Correct |
218 ms |
27460 KB |
Output is correct |
46 |
Correct |
199 ms |
28632 KB |
Output is correct |
47 |
Correct |
170 ms |
26024 KB |
Output is correct |
48 |
Correct |
126 ms |
29680 KB |
Output is correct |
49 |
Correct |
106 ms |
24352 KB |
Output is correct |
50 |
Correct |
100 ms |
13868 KB |
Output is correct |
51 |
Correct |
182 ms |
29192 KB |
Output is correct |
52 |
Correct |
176 ms |
27708 KB |
Output is correct |
53 |
Correct |
1 ms |
212 KB |
Output is correct |
54 |
Correct |
0 ms |
212 KB |
Output is correct |
55 |
Correct |
0 ms |
212 KB |
Output is correct |
56 |
Correct |
2 ms |
468 KB |
Output is correct |
57 |
Correct |
2 ms |
500 KB |
Output is correct |
58 |
Correct |
1 ms |
468 KB |
Output is correct |
59 |
Correct |
2 ms |
468 KB |
Output is correct |
60 |
Correct |
2 ms |
468 KB |
Output is correct |
61 |
Correct |
2 ms |
584 KB |
Output is correct |
62 |
Correct |
2 ms |
596 KB |
Output is correct |
63 |
Correct |
2 ms |
584 KB |
Output is correct |
64 |
Correct |
2 ms |
596 KB |
Output is correct |
65 |
Correct |
1 ms |
468 KB |
Output is correct |
66 |
Correct |
2 ms |
512 KB |
Output is correct |
67 |
Correct |
150 ms |
25868 KB |
Output is correct |
68 |
Correct |
138 ms |
25668 KB |
Output is correct |
69 |
Correct |
165 ms |
26996 KB |
Output is correct |
70 |
Correct |
213 ms |
28408 KB |
Output is correct |
71 |
Correct |
184 ms |
27168 KB |
Output is correct |
72 |
Correct |
181 ms |
27504 KB |
Output is correct |
73 |
Correct |
160 ms |
31276 KB |
Output is correct |
74 |
Correct |
147 ms |
29164 KB |
Output is correct |
75 |
Correct |
139 ms |
30712 KB |
Output is correct |
76 |
Correct |
180 ms |
27116 KB |
Output is correct |
77 |
Correct |
140 ms |
25536 KB |
Output is correct |
78 |
Correct |
132 ms |
24964 KB |
Output is correct |
79 |
Correct |
137 ms |
23324 KB |
Output is correct |
80 |
Correct |
85 ms |
23112 KB |
Output is correct |
81 |
Correct |
209 ms |
28756 KB |
Output is correct |
82 |
Correct |
220 ms |
28832 KB |
Output is correct |
83 |
Correct |
144 ms |
24640 KB |
Output is correct |
84 |
Correct |
137 ms |
25808 KB |
Output is correct |
85 |
Correct |
217 ms |
28880 KB |
Output is correct |
86 |
Correct |
161 ms |
23280 KB |
Output is correct |
87 |
Correct |
154 ms |
23252 KB |
Output is correct |
88 |
Correct |
134 ms |
25812 KB |
Output is correct |
89 |
Correct |
191 ms |
26640 KB |
Output is correct |
90 |
Correct |
210 ms |
25444 KB |
Output is correct |
91 |
Correct |
210 ms |
27228 KB |
Output is correct |
92 |
Correct |
228 ms |
27972 KB |
Output is correct |
93 |
Correct |
185 ms |
28644 KB |
Output is correct |
94 |
Correct |
181 ms |
25924 KB |
Output is correct |
95 |
Correct |
135 ms |
29748 KB |
Output is correct |
96 |
Correct |
102 ms |
24344 KB |
Output is correct |
97 |
Correct |
95 ms |
13112 KB |
Output is correct |
98 |
Correct |
173 ms |
28240 KB |
Output is correct |
99 |
Correct |
173 ms |
27724 KB |
Output is correct |
100 |
Correct |
42 ms |
4172 KB |
Output is correct |
101 |
Correct |
231 ms |
27636 KB |
Output is correct |
102 |
Correct |
141 ms |
21168 KB |
Output is correct |
103 |
Correct |
197 ms |
20748 KB |
Output is correct |
104 |
Correct |
252 ms |
25000 KB |
Output is correct |