#include<bits/stdc++.h>
using namespace std;
using ll = long long;
struct dsu {
vector<int> par;
vector<ll> sum;
vector<vector<int>> el;
vector<set<int>> ed;
dsu(int n, vector<int> s) : par(n), sum(s.begin(), s.end()), el(n), ed(n) {
for (int i = 0; i < n; i++) par[i] = i, el[i] = {i};
}
int get(int i) {
return par[i] == i ? par[i] : par[i] = get(par[i]);
}
void edge(int i, int j) {
ed[get(i)].insert(j);
}
void merge(int a, int b) {
a = get(a);
b = get(b);
if (el[a].size() < el[b].size())
swap(a, b);
if (a != b) {
par[b] =a;
sum[a] += sum[b];
for (int x : el[b])
el[a].push_back(x);
if (ed[a].size() < ed[b].size())
swap(ed[a], ed[b]);
for (int x : ed[b])
ed[a].insert(x);
}
}
};
int main() {
int n, m;
cin >> n >> m;
vector s(n, 0);
for (int& i : s) cin >> i;
vector<int> ix(n);
for (int i{}; i < n ;i++) ix[i] = i;
stable_sort(ix.begin(), ix.end(), [&](int a, int b) {
return s[a] > s[b];
});
vector<int> jx(n);
for (int i = 0; i < n; i ++)
jx[ix[i]] = i;
stable_sort(s.begin(), s.end(), greater{});
vector<vector<int>> adj(n);
for (int i = 0; i < m ;i++) {
int u, v;
cin >> u >> v;
u--;v--;
adj[jx[u]].push_back(jx[v]);
adj[jx[v]].push_back(jx[u]);
}
for (int i = 0; i < n; i++) {
sort(adj[i].begin(), adj[i].end());
}
//for (int i : ix) cout << i << ' ';cout << endl;
//for (int i : jx) cout << i << ' ';cout << endl;
//for (int i : s) cout << i << ' ';cout << endl;
string res(n, '?');
vector<int> back(n, -1);
dsu cc(n, s);
for (int i =n -1; i >= 0;i --) {
for (int j : adj[i]) {
cc.edge(i, j);
if (j > i)
cc.merge(i, j);
}
int c = cc.get(i);
ll sum = cc.sum[c];
auto it = cc.ed[c].lower_bound(i);
if (it != cc.ed[c].begin()) {
--it;
back[i] = max(back[i], *it);
}
if (back[i] != -1 && s[back[i]] > sum)
back[i] = -1;
if (back[i] == -1) {
if (i == 0)
res[i] = '1';
else res[i] = '0';
}
}
string ans(n, '?');
for (int i = 1; i < n;i++) {
if (back[i] != -1) res[i] = res[back[i]];
}
for (int i = 0; i < n; i++) {
ans[ix[i]] = res[i];
}
cout << ans << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
224 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
4 ms |
852 KB |
Output is correct |
5 |
Correct |
4 ms |
1108 KB |
Output is correct |
6 |
Correct |
3 ms |
852 KB |
Output is correct |
7 |
Correct |
4 ms |
852 KB |
Output is correct |
8 |
Correct |
4 ms |
724 KB |
Output is correct |
9 |
Correct |
3 ms |
852 KB |
Output is correct |
10 |
Correct |
5 ms |
1236 KB |
Output is correct |
11 |
Correct |
5 ms |
1236 KB |
Output is correct |
12 |
Correct |
6 ms |
1236 KB |
Output is correct |
13 |
Correct |
3 ms |
948 KB |
Output is correct |
14 |
Correct |
4 ms |
1060 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 |
425 ms |
70964 KB |
Output is correct |
4 |
Correct |
391 ms |
71156 KB |
Output is correct |
5 |
Correct |
614 ms |
93624 KB |
Output is correct |
6 |
Correct |
700 ms |
97852 KB |
Output is correct |
7 |
Correct |
646 ms |
98208 KB |
Output is correct |
8 |
Correct |
716 ms |
95228 KB |
Output is correct |
9 |
Correct |
598 ms |
135396 KB |
Output is correct |
10 |
Correct |
329 ms |
58140 KB |
Output is correct |
11 |
Correct |
322 ms |
57996 KB |
Output is correct |
12 |
Correct |
611 ms |
94992 KB |
Output is correct |
13 |
Correct |
377 ms |
66612 KB |
Output is correct |
14 |
Correct |
363 ms |
66760 KB |
Output is correct |
15 |
Correct |
410 ms |
66716 KB |
Output is correct |
16 |
Correct |
336 ms |
66212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
755 ms |
117908 KB |
Output is correct |
3 |
Correct |
737 ms |
117960 KB |
Output is correct |
4 |
Correct |
425 ms |
66620 KB |
Output is correct |
5 |
Correct |
393 ms |
69904 KB |
Output is correct |
6 |
Correct |
762 ms |
116968 KB |
Output is correct |
7 |
Correct |
431 ms |
66760 KB |
Output is correct |
8 |
Correct |
432 ms |
66668 KB |
Output is correct |
9 |
Correct |
322 ms |
65960 KB |
Output is correct |
10 |
Correct |
394 ms |
62032 KB |
Output is correct |
11 |
Correct |
469 ms |
78968 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
747 ms |
85632 KB |
Output is correct |
3 |
Correct |
729 ms |
91812 KB |
Output is correct |
4 |
Correct |
899 ms |
97384 KB |
Output is correct |
5 |
Correct |
774 ms |
93120 KB |
Output is correct |
6 |
Correct |
658 ms |
78460 KB |
Output is correct |
7 |
Correct |
373 ms |
58212 KB |
Output is correct |
8 |
Correct |
383 ms |
66748 KB |
Output is correct |
9 |
Correct |
432 ms |
45264 KB |
Output is correct |
10 |
Correct |
782 ms |
97268 KB |
Output is correct |
11 |
Correct |
520 ms |
75668 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
224 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
4 ms |
852 KB |
Output is correct |
5 |
Correct |
4 ms |
1108 KB |
Output is correct |
6 |
Correct |
3 ms |
852 KB |
Output is correct |
7 |
Correct |
4 ms |
852 KB |
Output is correct |
8 |
Correct |
4 ms |
724 KB |
Output is correct |
9 |
Correct |
3 ms |
852 KB |
Output is correct |
10 |
Correct |
5 ms |
1236 KB |
Output is correct |
11 |
Correct |
5 ms |
1236 KB |
Output is correct |
12 |
Correct |
6 ms |
1236 KB |
Output is correct |
13 |
Correct |
3 ms |
948 KB |
Output is correct |
14 |
Correct |
4 ms |
1060 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
425 ms |
70964 KB |
Output is correct |
18 |
Correct |
391 ms |
71156 KB |
Output is correct |
19 |
Correct |
614 ms |
93624 KB |
Output is correct |
20 |
Correct |
700 ms |
97852 KB |
Output is correct |
21 |
Correct |
646 ms |
98208 KB |
Output is correct |
22 |
Correct |
716 ms |
95228 KB |
Output is correct |
23 |
Correct |
598 ms |
135396 KB |
Output is correct |
24 |
Correct |
329 ms |
58140 KB |
Output is correct |
25 |
Correct |
322 ms |
57996 KB |
Output is correct |
26 |
Correct |
611 ms |
94992 KB |
Output is correct |
27 |
Correct |
377 ms |
66612 KB |
Output is correct |
28 |
Correct |
363 ms |
66760 KB |
Output is correct |
29 |
Correct |
410 ms |
66716 KB |
Output is correct |
30 |
Correct |
336 ms |
66212 KB |
Output is correct |
31 |
Correct |
1 ms |
212 KB |
Output is correct |
32 |
Correct |
755 ms |
117908 KB |
Output is correct |
33 |
Correct |
737 ms |
117960 KB |
Output is correct |
34 |
Correct |
425 ms |
66620 KB |
Output is correct |
35 |
Correct |
393 ms |
69904 KB |
Output is correct |
36 |
Correct |
762 ms |
116968 KB |
Output is correct |
37 |
Correct |
431 ms |
66760 KB |
Output is correct |
38 |
Correct |
432 ms |
66668 KB |
Output is correct |
39 |
Correct |
322 ms |
65960 KB |
Output is correct |
40 |
Correct |
394 ms |
62032 KB |
Output is correct |
41 |
Correct |
469 ms |
78968 KB |
Output is correct |
42 |
Correct |
0 ms |
212 KB |
Output is correct |
43 |
Correct |
747 ms |
85632 KB |
Output is correct |
44 |
Correct |
729 ms |
91812 KB |
Output is correct |
45 |
Correct |
899 ms |
97384 KB |
Output is correct |
46 |
Correct |
774 ms |
93120 KB |
Output is correct |
47 |
Correct |
658 ms |
78460 KB |
Output is correct |
48 |
Correct |
373 ms |
58212 KB |
Output is correct |
49 |
Correct |
383 ms |
66748 KB |
Output is correct |
50 |
Correct |
432 ms |
45264 KB |
Output is correct |
51 |
Correct |
782 ms |
97268 KB |
Output is correct |
52 |
Correct |
520 ms |
75668 KB |
Output is correct |
53 |
Correct |
0 ms |
212 KB |
Output is correct |
54 |
Correct |
1 ms |
212 KB |
Output is correct |
55 |
Correct |
1 ms |
212 KB |
Output is correct |
56 |
Correct |
4 ms |
852 KB |
Output is correct |
57 |
Correct |
6 ms |
980 KB |
Output is correct |
58 |
Correct |
4 ms |
852 KB |
Output is correct |
59 |
Correct |
4 ms |
852 KB |
Output is correct |
60 |
Correct |
4 ms |
724 KB |
Output is correct |
61 |
Correct |
5 ms |
852 KB |
Output is correct |
62 |
Correct |
9 ms |
1180 KB |
Output is correct |
63 |
Correct |
6 ms |
1236 KB |
Output is correct |
64 |
Correct |
7 ms |
1236 KB |
Output is correct |
65 |
Correct |
3 ms |
852 KB |
Output is correct |
66 |
Correct |
4 ms |
1108 KB |
Output is correct |
67 |
Correct |
441 ms |
71080 KB |
Output is correct |
68 |
Correct |
398 ms |
70980 KB |
Output is correct |
69 |
Correct |
670 ms |
93592 KB |
Output is correct |
70 |
Correct |
690 ms |
97668 KB |
Output is correct |
71 |
Correct |
696 ms |
98096 KB |
Output is correct |
72 |
Correct |
638 ms |
95160 KB |
Output is correct |
73 |
Correct |
623 ms |
135336 KB |
Output is correct |
74 |
Correct |
342 ms |
58144 KB |
Output is correct |
75 |
Correct |
327 ms |
57936 KB |
Output is correct |
76 |
Correct |
618 ms |
94972 KB |
Output is correct |
77 |
Correct |
377 ms |
66620 KB |
Output is correct |
78 |
Correct |
373 ms |
66884 KB |
Output is correct |
79 |
Correct |
416 ms |
66604 KB |
Output is correct |
80 |
Correct |
340 ms |
65892 KB |
Output is correct |
81 |
Correct |
724 ms |
117892 KB |
Output is correct |
82 |
Correct |
750 ms |
117824 KB |
Output is correct |
83 |
Correct |
399 ms |
66552 KB |
Output is correct |
84 |
Correct |
371 ms |
69912 KB |
Output is correct |
85 |
Correct |
730 ms |
117056 KB |
Output is correct |
86 |
Correct |
422 ms |
66792 KB |
Output is correct |
87 |
Correct |
401 ms |
66540 KB |
Output is correct |
88 |
Correct |
398 ms |
61888 KB |
Output is correct |
89 |
Correct |
467 ms |
79136 KB |
Output is correct |
90 |
Correct |
676 ms |
85588 KB |
Output is correct |
91 |
Correct |
681 ms |
91808 KB |
Output is correct |
92 |
Correct |
704 ms |
97384 KB |
Output is correct |
93 |
Correct |
699 ms |
93284 KB |
Output is correct |
94 |
Correct |
606 ms |
78364 KB |
Output is correct |
95 |
Correct |
367 ms |
58036 KB |
Output is correct |
96 |
Correct |
329 ms |
66612 KB |
Output is correct |
97 |
Correct |
329 ms |
45448 KB |
Output is correct |
98 |
Correct |
654 ms |
97592 KB |
Output is correct |
99 |
Correct |
466 ms |
75788 KB |
Output is correct |
100 |
Correct |
166 ms |
12932 KB |
Output is correct |
101 |
Correct |
745 ms |
96928 KB |
Output is correct |
102 |
Correct |
569 ms |
62764 KB |
Output is correct |
103 |
Correct |
585 ms |
66948 KB |
Output is correct |
104 |
Correct |
713 ms |
81844 KB |
Output is correct |