#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <queue>
using namespace std;
#define ll long long
#define pii pair<int, int>
const int INF = 1e9;
vector<vector<int>> w;
vector<ll> vals;
vector<int> r, p;
int find(int a) { return a == p[a] ? a : p[a] = find(p[a]); }
void link(int a, int b) {
a = find(a), b = find(b);
if (a == b) return;
if (r[a] < r[b]) swap(a, b);
if (r[a] == r[b]) ++r[a];
p[b] = a;
}
int main() {
int n, m; cin >> n >> m;
vector<int> arr(n); for (int i = 0; i < n; ++i) cin >> arr[i];
set<int> nums; for (int num : arr) nums.insert(num);
vector<int> copy(nums.begin(), nums.end());
vector<vector<pii>> edges(copy.size());
for (int i = 0; i < m; ++i) {
int a, b; cin >> a >> b; --a, --b;
edges[lower_bound(copy.begin(), copy.end(), max(arr[a], arr[b])) - copy.begin()].push_back({a, b});
}
w.resize(n), vals.resize(n), r.resize(n), p.resize(n);
for (int i = 0; i < n; ++i) w[i].push_back(i), vals[i] = arr[i], p[i] = i;
for (int i = 0; i < copy.size(); ++i) {
for (pii edge : edges[i]) {
int a = edge.first, b = edge.second;
int pa = find(a), pb = find(b);
if (pa == pb) continue;
link(a, b);
int np = find(a);
if (vals[pa] < arr[b]) swap(w[np], w[pb]);
else if (vals[pb] < arr[a]) swap(w[np], w[pa]);
else {
if (w[pa].size() < w[pb].size()) swap(w[pa], w[pb]);
for (int v : w[pb]) w[pa].push_back(v);
swap(w[np], w[pa]);
}
vals[np] = vals[pa] + vals[pb];
}
}
vector<bool> ans(n);
for (int v : w[find(0)]) ans[v] = true;
for (int num : ans) cout << num;
cout << '\n';
/*
vector<int> nodes(n); for (int i = 0; i < n; ++i) cin >> nodes[i];
vector<pii> arr(n); for (int i = 0; i < n; ++i) arr[i] = {nodes[i], n - i};
sort(arr.begin(), arr.end()), reverse(arr.begin(), arr.end());
for (int i = 0; i < n; ++i) arr[i].second = n - arr[i].second;
vector<int> io(n); for (int i = 0; i < n; ++i) io[arr[i].second] = i;
vector<vector<int>> adj(n);
for (int i = 0; i < m; ++i) {
int a, b; cin >> a >> b; --a, --b;
adj[a].push_back(b), adj[b].push_back(a);
}
r.resize(n), p.resize(n); for (int i = 0; i < n; ++i) p[i] = i;
vector<ll> sizes(n), psizes(n);
for (int i = n - 1; i >= 0; --i) {
int v = arr[i].second;
sizes[v] = nodes[v];
bool bs = false;
set<int> done;
for (int u : adj[v]) {
if (io[u] < io[v] && nodes[u] == nodes[v]) bs = true;
if (io[u] < io[v] || done.count(find(u))) continue;
sizes[v] += psizes[find(u)], done.insert(find(u));
}
if (!bs) for (int u : adj[v]) if (nodes[u] == nodes[v]) sizes[u] = sizes[v];
for (int u : adj[v]) if (io[u] > io[v]) link(u, v);
psizes[find(v)] = sizes[v];
}
vector<int> nd(n, INF); nd[arr[0].second] = 0;
for (int i = 1; i < n; ++i) {
for (int u : adj[arr[i].second]) {
if (io[u] > i || nd[u] == INF || nodes[u] > sizes[arr[i].second]) continue;
nd[arr[i].second] = 0;
}
}
queue<pii> q; q.push({arr[0].second, -1});
while (q.size()) {
int v = q.front().first, from = q.front().second, rn = max(nodes[v], nd[v]); q.pop();
cout << v << ' ' << nd[v] << '\n';
for (int u : adj[v]) {
if (u == from) continue;
ll need;
if (nodes[u] >= nodes[v]) need = max(nd[v] - sizes[u] + sizes[v], 0LL);
else need = max(max(nodes[v] - sizes[u], (ll) nd[v]), 0LL);
if (need < nd[u]) {
nd[u] = need;
q.push({u, v});
}
}
}
//for (int num : nd) cout << num << '\n';
for (int i = 0; i < n; ++i) cout << (nd[i] == 0);
cout << '\n';
*/
//for (ll sz : sizes) cout << sz << ' ';
//cout << '\n';
return 0;
}
Compilation message
island.cpp: In function 'int main()':
island.cpp:39:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
39 | for (int i = 0; i < copy.size(); ++i) {
| ~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
296 KB |
Output is correct |
4 |
Correct |
3 ms |
596 KB |
Output is correct |
5 |
Correct |
3 ms |
600 KB |
Output is correct |
6 |
Correct |
2 ms |
468 KB |
Output is correct |
7 |
Correct |
3 ms |
596 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
2 ms |
440 KB |
Output is correct |
10 |
Correct |
3 ms |
572 KB |
Output is correct |
11 |
Correct |
3 ms |
596 KB |
Output is correct |
12 |
Correct |
3 ms |
596 KB |
Output is correct |
13 |
Correct |
2 ms |
468 KB |
Output is correct |
14 |
Correct |
2 ms |
468 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 |
377 ms |
38136 KB |
Output is correct |
4 |
Correct |
222 ms |
25412 KB |
Output is correct |
5 |
Correct |
356 ms |
39052 KB |
Output is correct |
6 |
Correct |
330 ms |
36116 KB |
Output is correct |
7 |
Correct |
398 ms |
40260 KB |
Output is correct |
8 |
Correct |
267 ms |
25404 KB |
Output is correct |
9 |
Correct |
311 ms |
36536 KB |
Output is correct |
10 |
Correct |
232 ms |
31208 KB |
Output is correct |
11 |
Correct |
215 ms |
21740 KB |
Output is correct |
12 |
Correct |
221 ms |
21952 KB |
Output is correct |
13 |
Correct |
226 ms |
28188 KB |
Output is correct |
14 |
Correct |
234 ms |
27784 KB |
Output is correct |
15 |
Correct |
329 ms |
40396 KB |
Output is correct |
16 |
Correct |
272 ms |
40116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
502 ms |
40500 KB |
Output is correct |
3 |
Correct |
420 ms |
38404 KB |
Output is correct |
4 |
Correct |
290 ms |
29588 KB |
Output is correct |
5 |
Correct |
218 ms |
28036 KB |
Output is correct |
6 |
Correct |
464 ms |
42544 KB |
Output is correct |
7 |
Correct |
383 ms |
41928 KB |
Output is correct |
8 |
Correct |
331 ms |
42040 KB |
Output is correct |
9 |
Correct |
261 ms |
40872 KB |
Output is correct |
10 |
Correct |
257 ms |
32020 KB |
Output is correct |
11 |
Correct |
225 ms |
25700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
292 ms |
21284 KB |
Output is correct |
3 |
Correct |
246 ms |
23512 KB |
Output is correct |
4 |
Correct |
251 ms |
24808 KB |
Output is correct |
5 |
Correct |
278 ms |
26124 KB |
Output is correct |
6 |
Correct |
244 ms |
23952 KB |
Output is correct |
7 |
Correct |
231 ms |
23200 KB |
Output is correct |
8 |
Correct |
203 ms |
21516 KB |
Output is correct |
9 |
Correct |
141 ms |
12948 KB |
Output is correct |
10 |
Correct |
235 ms |
25472 KB |
Output is correct |
11 |
Correct |
218 ms |
24988 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
296 KB |
Output is correct |
4 |
Correct |
3 ms |
596 KB |
Output is correct |
5 |
Correct |
3 ms |
600 KB |
Output is correct |
6 |
Correct |
2 ms |
468 KB |
Output is correct |
7 |
Correct |
3 ms |
596 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
9 |
Correct |
2 ms |
440 KB |
Output is correct |
10 |
Correct |
3 ms |
572 KB |
Output is correct |
11 |
Correct |
3 ms |
596 KB |
Output is correct |
12 |
Correct |
3 ms |
596 KB |
Output is correct |
13 |
Correct |
2 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 |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
377 ms |
38136 KB |
Output is correct |
18 |
Correct |
222 ms |
25412 KB |
Output is correct |
19 |
Correct |
356 ms |
39052 KB |
Output is correct |
20 |
Correct |
330 ms |
36116 KB |
Output is correct |
21 |
Correct |
398 ms |
40260 KB |
Output is correct |
22 |
Correct |
267 ms |
25404 KB |
Output is correct |
23 |
Correct |
311 ms |
36536 KB |
Output is correct |
24 |
Correct |
232 ms |
31208 KB |
Output is correct |
25 |
Correct |
215 ms |
21740 KB |
Output is correct |
26 |
Correct |
221 ms |
21952 KB |
Output is correct |
27 |
Correct |
226 ms |
28188 KB |
Output is correct |
28 |
Correct |
234 ms |
27784 KB |
Output is correct |
29 |
Correct |
329 ms |
40396 KB |
Output is correct |
30 |
Correct |
272 ms |
40116 KB |
Output is correct |
31 |
Correct |
0 ms |
212 KB |
Output is correct |
32 |
Correct |
502 ms |
40500 KB |
Output is correct |
33 |
Correct |
420 ms |
38404 KB |
Output is correct |
34 |
Correct |
290 ms |
29588 KB |
Output is correct |
35 |
Correct |
218 ms |
28036 KB |
Output is correct |
36 |
Correct |
464 ms |
42544 KB |
Output is correct |
37 |
Correct |
383 ms |
41928 KB |
Output is correct |
38 |
Correct |
331 ms |
42040 KB |
Output is correct |
39 |
Correct |
261 ms |
40872 KB |
Output is correct |
40 |
Correct |
257 ms |
32020 KB |
Output is correct |
41 |
Correct |
225 ms |
25700 KB |
Output is correct |
42 |
Correct |
0 ms |
212 KB |
Output is correct |
43 |
Correct |
292 ms |
21284 KB |
Output is correct |
44 |
Correct |
246 ms |
23512 KB |
Output is correct |
45 |
Correct |
251 ms |
24808 KB |
Output is correct |
46 |
Correct |
278 ms |
26124 KB |
Output is correct |
47 |
Correct |
244 ms |
23952 KB |
Output is correct |
48 |
Correct |
231 ms |
23200 KB |
Output is correct |
49 |
Correct |
203 ms |
21516 KB |
Output is correct |
50 |
Correct |
141 ms |
12948 KB |
Output is correct |
51 |
Correct |
235 ms |
25472 KB |
Output is correct |
52 |
Correct |
218 ms |
24988 KB |
Output is correct |
53 |
Correct |
1 ms |
212 KB |
Output is correct |
54 |
Correct |
1 ms |
212 KB |
Output is correct |
55 |
Correct |
0 ms |
212 KB |
Output is correct |
56 |
Correct |
3 ms |
596 KB |
Output is correct |
57 |
Correct |
3 ms |
604 KB |
Output is correct |
58 |
Correct |
2 ms |
436 KB |
Output is correct |
59 |
Correct |
3 ms |
596 KB |
Output is correct |
60 |
Correct |
3 ms |
468 KB |
Output is correct |
61 |
Correct |
2 ms |
468 KB |
Output is correct |
62 |
Correct |
4 ms |
596 KB |
Output is correct |
63 |
Correct |
3 ms |
596 KB |
Output is correct |
64 |
Correct |
4 ms |
596 KB |
Output is correct |
65 |
Correct |
2 ms |
468 KB |
Output is correct |
66 |
Correct |
2 ms |
468 KB |
Output is correct |
67 |
Correct |
324 ms |
40088 KB |
Output is correct |
68 |
Correct |
237 ms |
26908 KB |
Output is correct |
69 |
Correct |
336 ms |
40324 KB |
Output is correct |
70 |
Correct |
353 ms |
37544 KB |
Output is correct |
71 |
Correct |
343 ms |
41696 KB |
Output is correct |
72 |
Correct |
268 ms |
27064 KB |
Output is correct |
73 |
Correct |
321 ms |
37848 KB |
Output is correct |
74 |
Correct |
226 ms |
31700 KB |
Output is correct |
75 |
Correct |
205 ms |
22116 KB |
Output is correct |
76 |
Correct |
220 ms |
22076 KB |
Output is correct |
77 |
Correct |
220 ms |
28108 KB |
Output is correct |
78 |
Correct |
231 ms |
27768 KB |
Output is correct |
79 |
Correct |
331 ms |
41968 KB |
Output is correct |
80 |
Correct |
296 ms |
40776 KB |
Output is correct |
81 |
Correct |
412 ms |
42420 KB |
Output is correct |
82 |
Correct |
407 ms |
38376 KB |
Output is correct |
83 |
Correct |
268 ms |
29608 KB |
Output is correct |
84 |
Correct |
239 ms |
27904 KB |
Output is correct |
85 |
Correct |
434 ms |
42576 KB |
Output is correct |
86 |
Correct |
336 ms |
41900 KB |
Output is correct |
87 |
Correct |
320 ms |
41836 KB |
Output is correct |
88 |
Correct |
266 ms |
32028 KB |
Output is correct |
89 |
Correct |
225 ms |
25628 KB |
Output is correct |
90 |
Correct |
255 ms |
23220 KB |
Output is correct |
91 |
Correct |
259 ms |
23468 KB |
Output is correct |
92 |
Correct |
266 ms |
24992 KB |
Output is correct |
93 |
Correct |
267 ms |
26124 KB |
Output is correct |
94 |
Correct |
265 ms |
23764 KB |
Output is correct |
95 |
Correct |
237 ms |
23092 KB |
Output is correct |
96 |
Correct |
179 ms |
21432 KB |
Output is correct |
97 |
Correct |
149 ms |
12972 KB |
Output is correct |
98 |
Correct |
217 ms |
25444 KB |
Output is correct |
99 |
Correct |
254 ms |
25016 KB |
Output is correct |
100 |
Correct |
113 ms |
5324 KB |
Output is correct |
101 |
Correct |
418 ms |
37032 KB |
Output is correct |
102 |
Correct |
232 ms |
19880 KB |
Output is correct |
103 |
Correct |
316 ms |
30596 KB |
Output is correct |
104 |
Correct |
410 ms |
39320 KB |
Output is correct |