#include<bits/stdc++.h>
using namespace std;
using ll = long long;
typedef pair<int, int> pii;
constexpr int maxn = 2e5;
int isize[maxn], parent[maxn];
ll cursize[maxn];
bool marked[maxn] = { 0 };
int find(const int &p)
{
if (p == parent[p]) return p;
int last = parent[p];
parent[p] = find(parent[p]);
if (marked[last]) marked[p] = true;
return parent[p];
}
void merge(int d, int b) // a is newly inserted node, b is old
{
int a;
a = find(d), b = find(b);
if (a == b) return ;
// a will allways take b, but b will not allways take a
// printf("%d %d %d %d\n", a, b, cursize[b], isize[a]);
if (cursize[b] >= isize[d])
{
// Both will take each other
cursize[b] += cursize[a];
parent[a] = b;
} else {
cursize[a] += cursize[b];
parent[b] = a;
marked[b] = true;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, m;
cin >> n >> m;
for (int i = 0; i < n; i++) parent[i] = i;
vector <pii> pq(n);
for (int i = 0; i < n; i++) {
cin >> isize[i];
cursize[i] = isize[i];
pq[i] = pii(isize[i], i);
}
sort(pq.begin(), pq.end());
vector <vector <int>> adj(n);
int a, b;
for (int i = 0; i < m; i++)
{
cin >> a >> b;
a--; b--;
adj[a].emplace_back(b);
adj[b].emplace_back(a);
}
vector <int> created(n);
auto it = pq.begin();
int pos;
while (it != pq.end())
{
pos = (*it).second;
it++;
created[pos] = true;
for (int nei : adj[pos])
{
if (!created[nei]) continue;
merge(pos, nei);
}
}
for (int i = 0; i < n; i++) find(i);
for (int i = 0; i < n; i++) cout << !marked[i];
cout << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
5 |
Correct |
2 ms |
468 KB |
Output is correct |
6 |
Correct |
2 ms |
380 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
432 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
146 ms |
17160 KB |
Output is correct |
4 |
Correct |
149 ms |
17128 KB |
Output is correct |
5 |
Correct |
142 ms |
16628 KB |
Output is correct |
6 |
Correct |
179 ms |
17108 KB |
Output is correct |
7 |
Correct |
157 ms |
17080 KB |
Output is correct |
8 |
Correct |
166 ms |
17036 KB |
Output is correct |
9 |
Correct |
147 ms |
17052 KB |
Output is correct |
10 |
Correct |
110 ms |
17836 KB |
Output is correct |
11 |
Correct |
154 ms |
17732 KB |
Output is correct |
12 |
Correct |
149 ms |
17148 KB |
Output is correct |
13 |
Correct |
131 ms |
17148 KB |
Output is correct |
14 |
Correct |
134 ms |
17104 KB |
Output is correct |
15 |
Correct |
130 ms |
16932 KB |
Output is correct |
16 |
Correct |
84 ms |
16796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
172 ms |
17000 KB |
Output is correct |
3 |
Correct |
217 ms |
18744 KB |
Output is correct |
4 |
Correct |
166 ms |
18552 KB |
Output is correct |
5 |
Correct |
150 ms |
18724 KB |
Output is correct |
6 |
Correct |
173 ms |
18760 KB |
Output is correct |
7 |
Correct |
165 ms |
18596 KB |
Output is correct |
8 |
Correct |
158 ms |
18612 KB |
Output is correct |
9 |
Correct |
89 ms |
18560 KB |
Output is correct |
10 |
Correct |
150 ms |
18748 KB |
Output is correct |
11 |
Correct |
153 ms |
19228 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
197 ms |
17052 KB |
Output is correct |
3 |
Correct |
221 ms |
21492 KB |
Output is correct |
4 |
Correct |
165 ms |
21608 KB |
Output is correct |
5 |
Correct |
195 ms |
21360 KB |
Output is correct |
6 |
Correct |
170 ms |
20428 KB |
Output is correct |
7 |
Correct |
119 ms |
25912 KB |
Output is correct |
8 |
Correct |
90 ms |
20328 KB |
Output is correct |
9 |
Correct |
131 ms |
12240 KB |
Output is correct |
10 |
Correct |
162 ms |
18796 KB |
Output is correct |
11 |
Correct |
147 ms |
19312 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
5 |
Correct |
2 ms |
468 KB |
Output is correct |
6 |
Correct |
2 ms |
380 KB |
Output is correct |
7 |
Correct |
2 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
432 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
146 ms |
17160 KB |
Output is correct |
18 |
Correct |
149 ms |
17128 KB |
Output is correct |
19 |
Correct |
142 ms |
16628 KB |
Output is correct |
20 |
Correct |
179 ms |
17108 KB |
Output is correct |
21 |
Correct |
157 ms |
17080 KB |
Output is correct |
22 |
Correct |
166 ms |
17036 KB |
Output is correct |
23 |
Correct |
147 ms |
17052 KB |
Output is correct |
24 |
Correct |
110 ms |
17836 KB |
Output is correct |
25 |
Correct |
154 ms |
17732 KB |
Output is correct |
26 |
Correct |
149 ms |
17148 KB |
Output is correct |
27 |
Correct |
131 ms |
17148 KB |
Output is correct |
28 |
Correct |
134 ms |
17104 KB |
Output is correct |
29 |
Correct |
130 ms |
16932 KB |
Output is correct |
30 |
Correct |
84 ms |
16796 KB |
Output is correct |
31 |
Correct |
0 ms |
340 KB |
Output is correct |
32 |
Correct |
172 ms |
17000 KB |
Output is correct |
33 |
Correct |
217 ms |
18744 KB |
Output is correct |
34 |
Correct |
166 ms |
18552 KB |
Output is correct |
35 |
Correct |
150 ms |
18724 KB |
Output is correct |
36 |
Correct |
173 ms |
18760 KB |
Output is correct |
37 |
Correct |
165 ms |
18596 KB |
Output is correct |
38 |
Correct |
158 ms |
18612 KB |
Output is correct |
39 |
Correct |
89 ms |
18560 KB |
Output is correct |
40 |
Correct |
150 ms |
18748 KB |
Output is correct |
41 |
Correct |
153 ms |
19228 KB |
Output is correct |
42 |
Correct |
0 ms |
340 KB |
Output is correct |
43 |
Correct |
197 ms |
17052 KB |
Output is correct |
44 |
Correct |
221 ms |
21492 KB |
Output is correct |
45 |
Correct |
165 ms |
21608 KB |
Output is correct |
46 |
Correct |
195 ms |
21360 KB |
Output is correct |
47 |
Correct |
170 ms |
20428 KB |
Output is correct |
48 |
Correct |
119 ms |
25912 KB |
Output is correct |
49 |
Correct |
90 ms |
20328 KB |
Output is correct |
50 |
Correct |
131 ms |
12240 KB |
Output is correct |
51 |
Correct |
162 ms |
18796 KB |
Output is correct |
52 |
Correct |
147 ms |
19312 KB |
Output is correct |
53 |
Correct |
1 ms |
328 KB |
Output is correct |
54 |
Correct |
1 ms |
340 KB |
Output is correct |
55 |
Correct |
1 ms |
340 KB |
Output is correct |
56 |
Correct |
2 ms |
468 KB |
Output is correct |
57 |
Correct |
3 ms |
464 KB |
Output is correct |
58 |
Correct |
2 ms |
468 KB |
Output is correct |
59 |
Correct |
2 ms |
472 KB |
Output is correct |
60 |
Correct |
2 ms |
472 KB |
Output is correct |
61 |
Correct |
1 ms |
476 KB |
Output is correct |
62 |
Correct |
2 ms |
468 KB |
Output is correct |
63 |
Correct |
2 ms |
472 KB |
Output is correct |
64 |
Correct |
2 ms |
472 KB |
Output is correct |
65 |
Correct |
2 ms |
488 KB |
Output is correct |
66 |
Correct |
2 ms |
472 KB |
Output is correct |
67 |
Correct |
142 ms |
18772 KB |
Output is correct |
68 |
Correct |
170 ms |
18728 KB |
Output is correct |
69 |
Correct |
156 ms |
18308 KB |
Output is correct |
70 |
Correct |
170 ms |
18768 KB |
Output is correct |
71 |
Correct |
160 ms |
18748 KB |
Output is correct |
72 |
Correct |
182 ms |
18712 KB |
Output is correct |
73 |
Correct |
147 ms |
18656 KB |
Output is correct |
74 |
Correct |
120 ms |
19504 KB |
Output is correct |
75 |
Correct |
141 ms |
19400 KB |
Output is correct |
76 |
Correct |
152 ms |
18824 KB |
Output is correct |
77 |
Correct |
126 ms |
18804 KB |
Output is correct |
78 |
Correct |
129 ms |
18684 KB |
Output is correct |
79 |
Correct |
150 ms |
18600 KB |
Output is correct |
80 |
Correct |
102 ms |
18536 KB |
Output is correct |
81 |
Correct |
205 ms |
18660 KB |
Output is correct |
82 |
Correct |
174 ms |
18792 KB |
Output is correct |
83 |
Correct |
173 ms |
18612 KB |
Output is correct |
84 |
Correct |
134 ms |
18696 KB |
Output is correct |
85 |
Correct |
157 ms |
18744 KB |
Output is correct |
86 |
Correct |
157 ms |
18556 KB |
Output is correct |
87 |
Correct |
160 ms |
18628 KB |
Output is correct |
88 |
Correct |
140 ms |
18808 KB |
Output is correct |
89 |
Correct |
181 ms |
19304 KB |
Output is correct |
90 |
Correct |
196 ms |
18700 KB |
Output is correct |
91 |
Correct |
216 ms |
18716 KB |
Output is correct |
92 |
Correct |
216 ms |
18804 KB |
Output is correct |
93 |
Correct |
214 ms |
18600 KB |
Output is correct |
94 |
Correct |
168 ms |
17840 KB |
Output is correct |
95 |
Correct |
145 ms |
23192 KB |
Output is correct |
96 |
Correct |
112 ms |
18604 KB |
Output is correct |
97 |
Correct |
125 ms |
11364 KB |
Output is correct |
98 |
Correct |
173 ms |
18964 KB |
Output is correct |
99 |
Correct |
210 ms |
19296 KB |
Output is correct |
100 |
Correct |
50 ms |
4432 KB |
Output is correct |
101 |
Correct |
230 ms |
18784 KB |
Output is correct |
102 |
Correct |
185 ms |
15108 KB |
Output is correct |
103 |
Correct |
199 ms |
16184 KB |
Output is correct |
104 |
Correct |
198 ms |
18432 KB |
Output is correct |