#include <bits/stdc++.h>
#ifndef _DEBUG
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx")
#endif
using namespace std;
#define int int64_t
template <class T>
istream &operator>>(istream &in, vector<T> &v)
{
for (T &x : v)
in >> x;
return in;
}
vector<vector<int>> adj;
struct Boi
{
int s;
int i;
vector<int> members;
int parent;
int sum;
Boi(int s, int i)
{
this->s = this->sum = s;
this->i = this->parent = i;
members.push_back(i);
}
bool operator<(const Boi &other) const
{
return s < other.s;
}
};
vector<Boi> bois;
int find(int i)
{
if (bois[i].parent == i)
return i;
return bois[i].parent = find(bois[i].parent);
}
void mergeVectors(vector<int> &a, vector<int> &b)
{
if (a.size() < b.size())
swap(a, b);
for (int x : b)
a.push_back(x);
b = vector<int>(); // empty to save memory
}
void merge(int a, int b)
{
a = find(a);
b = find(b);
if (a == b) // if already in the same group, can be ignored
return;
if (bois[b].sum >= bois[a].s) // can take over, and are not taken over
mergeVectors(bois[a].members, bois[b].members);
bois[a].sum += bois[b].sum;
bois[b].parent = a;
}
signed main()
{
cin.tie(0);
ios_base::sync_with_stdio(false);
int n, m;
cin >> n >> m;
bois = vector<Boi>(n, Boi(-1, -1));
for (int i = 0; i < n; i++)
{
int s;
cin >> s;
bois[i] = Boi(s, i);
}
vector<Boi> sorted = bois;
sort(sorted.begin(), sorted.end());
adj = vector<vector<int>>(n);
while (m--)
{
int a, b;
cin >> a >> b;
a--, b--;
if (bois[a].s < bois[b].s)
swap(a, b);
adj[a].push_back(b); // only to smaller ones
}
for (int i = 0; i < n; i++)
sort(adj[i].begin(), adj[i].end(), [&](int a, int b)
{ return bois[a].s < bois[b].s; });
for (Boi &boi : sorted)
{
for (int c : adj[boi.i])
merge(boi.i, c);
}
int bigBoi = find(sorted.back().i);
vector<bool> possible(n, false);
for (int p : bois[bigBoi].members)
possible[p] = true;
for (bool b : possible)
{
if (b)
cout << "1";
else
cout << "0";
}
cout << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
2 ms |
596 KB |
Output is correct |
5 |
Correct |
2 ms |
724 KB |
Output is correct |
6 |
Correct |
2 ms |
596 KB |
Output is correct |
7 |
Correct |
2 ms |
596 KB |
Output is correct |
8 |
Correct |
2 ms |
596 KB |
Output is correct |
9 |
Correct |
3 ms |
724 KB |
Output is correct |
10 |
Correct |
2 ms |
724 KB |
Output is correct |
11 |
Correct |
2 ms |
724 KB |
Output is correct |
12 |
Correct |
3 ms |
724 KB |
Output is correct |
13 |
Correct |
2 ms |
724 KB |
Output is correct |
14 |
Correct |
2 ms |
724 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 |
171 ms |
46080 KB |
Output is correct |
4 |
Correct |
172 ms |
46188 KB |
Output is correct |
5 |
Correct |
186 ms |
44748 KB |
Output is correct |
6 |
Correct |
215 ms |
45584 KB |
Output is correct |
7 |
Correct |
195 ms |
46020 KB |
Output is correct |
8 |
Correct |
234 ms |
46248 KB |
Output is correct |
9 |
Correct |
176 ms |
43616 KB |
Output is correct |
10 |
Correct |
160 ms |
41580 KB |
Output is correct |
11 |
Correct |
170 ms |
45476 KB |
Output is correct |
12 |
Correct |
252 ms |
44432 KB |
Output is correct |
13 |
Correct |
179 ms |
44604 KB |
Output is correct |
14 |
Correct |
188 ms |
45812 KB |
Output is correct |
15 |
Correct |
181 ms |
47416 KB |
Output is correct |
16 |
Correct |
126 ms |
47092 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
241 ms |
46936 KB |
Output is correct |
3 |
Correct |
285 ms |
47472 KB |
Output is correct |
4 |
Correct |
191 ms |
45240 KB |
Output is correct |
5 |
Correct |
168 ms |
44688 KB |
Output is correct |
6 |
Correct |
315 ms |
47092 KB |
Output is correct |
7 |
Correct |
280 ms |
47800 KB |
Output is correct |
8 |
Correct |
217 ms |
47812 KB |
Output is correct |
9 |
Correct |
121 ms |
47052 KB |
Output is correct |
10 |
Correct |
231 ms |
44228 KB |
Output is correct |
11 |
Correct |
213 ms |
46324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
232 KB |
Output is correct |
2 |
Correct |
288 ms |
45632 KB |
Output is correct |
3 |
Correct |
332 ms |
46392 KB |
Output is correct |
4 |
Correct |
311 ms |
47404 KB |
Output is correct |
5 |
Correct |
296 ms |
48300 KB |
Output is correct |
6 |
Correct |
248 ms |
45072 KB |
Output is correct |
7 |
Correct |
222 ms |
45448 KB |
Output is correct |
8 |
Correct |
151 ms |
45460 KB |
Output is correct |
9 |
Correct |
163 ms |
24780 KB |
Output is correct |
10 |
Correct |
280 ms |
48776 KB |
Output is correct |
11 |
Correct |
224 ms |
46532 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
2 ms |
596 KB |
Output is correct |
5 |
Correct |
2 ms |
724 KB |
Output is correct |
6 |
Correct |
2 ms |
596 KB |
Output is correct |
7 |
Correct |
2 ms |
596 KB |
Output is correct |
8 |
Correct |
2 ms |
596 KB |
Output is correct |
9 |
Correct |
3 ms |
724 KB |
Output is correct |
10 |
Correct |
2 ms |
724 KB |
Output is correct |
11 |
Correct |
2 ms |
724 KB |
Output is correct |
12 |
Correct |
3 ms |
724 KB |
Output is correct |
13 |
Correct |
2 ms |
724 KB |
Output is correct |
14 |
Correct |
2 ms |
724 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
171 ms |
46080 KB |
Output is correct |
18 |
Correct |
172 ms |
46188 KB |
Output is correct |
19 |
Correct |
186 ms |
44748 KB |
Output is correct |
20 |
Correct |
215 ms |
45584 KB |
Output is correct |
21 |
Correct |
195 ms |
46020 KB |
Output is correct |
22 |
Correct |
234 ms |
46248 KB |
Output is correct |
23 |
Correct |
176 ms |
43616 KB |
Output is correct |
24 |
Correct |
160 ms |
41580 KB |
Output is correct |
25 |
Correct |
170 ms |
45476 KB |
Output is correct |
26 |
Correct |
252 ms |
44432 KB |
Output is correct |
27 |
Correct |
179 ms |
44604 KB |
Output is correct |
28 |
Correct |
188 ms |
45812 KB |
Output is correct |
29 |
Correct |
181 ms |
47416 KB |
Output is correct |
30 |
Correct |
126 ms |
47092 KB |
Output is correct |
31 |
Correct |
0 ms |
212 KB |
Output is correct |
32 |
Correct |
241 ms |
46936 KB |
Output is correct |
33 |
Correct |
285 ms |
47472 KB |
Output is correct |
34 |
Correct |
191 ms |
45240 KB |
Output is correct |
35 |
Correct |
168 ms |
44688 KB |
Output is correct |
36 |
Correct |
315 ms |
47092 KB |
Output is correct |
37 |
Correct |
280 ms |
47800 KB |
Output is correct |
38 |
Correct |
217 ms |
47812 KB |
Output is correct |
39 |
Correct |
121 ms |
47052 KB |
Output is correct |
40 |
Correct |
231 ms |
44228 KB |
Output is correct |
41 |
Correct |
213 ms |
46324 KB |
Output is correct |
42 |
Correct |
1 ms |
232 KB |
Output is correct |
43 |
Correct |
288 ms |
45632 KB |
Output is correct |
44 |
Correct |
332 ms |
46392 KB |
Output is correct |
45 |
Correct |
311 ms |
47404 KB |
Output is correct |
46 |
Correct |
296 ms |
48300 KB |
Output is correct |
47 |
Correct |
248 ms |
45072 KB |
Output is correct |
48 |
Correct |
222 ms |
45448 KB |
Output is correct |
49 |
Correct |
151 ms |
45460 KB |
Output is correct |
50 |
Correct |
163 ms |
24780 KB |
Output is correct |
51 |
Correct |
280 ms |
48776 KB |
Output is correct |
52 |
Correct |
224 ms |
46532 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 |
596 KB |
Output is correct |
57 |
Correct |
3 ms |
756 KB |
Output is correct |
58 |
Correct |
3 ms |
672 KB |
Output is correct |
59 |
Correct |
3 ms |
596 KB |
Output is correct |
60 |
Correct |
2 ms |
596 KB |
Output is correct |
61 |
Correct |
5 ms |
764 KB |
Output is correct |
62 |
Correct |
2 ms |
724 KB |
Output is correct |
63 |
Correct |
2 ms |
724 KB |
Output is correct |
64 |
Correct |
2 ms |
724 KB |
Output is correct |
65 |
Correct |
2 ms |
724 KB |
Output is correct |
66 |
Correct |
2 ms |
752 KB |
Output is correct |
67 |
Correct |
180 ms |
46084 KB |
Output is correct |
68 |
Correct |
185 ms |
46088 KB |
Output is correct |
69 |
Correct |
323 ms |
44724 KB |
Output is correct |
70 |
Correct |
214 ms |
45496 KB |
Output is correct |
71 |
Correct |
200 ms |
45944 KB |
Output is correct |
72 |
Correct |
372 ms |
46176 KB |
Output is correct |
73 |
Correct |
164 ms |
43700 KB |
Output is correct |
74 |
Correct |
158 ms |
41596 KB |
Output is correct |
75 |
Correct |
260 ms |
45560 KB |
Output is correct |
76 |
Correct |
234 ms |
44404 KB |
Output is correct |
77 |
Correct |
165 ms |
44616 KB |
Output is correct |
78 |
Correct |
177 ms |
45760 KB |
Output is correct |
79 |
Correct |
192 ms |
47532 KB |
Output is correct |
80 |
Correct |
111 ms |
47036 KB |
Output is correct |
81 |
Correct |
228 ms |
46920 KB |
Output is correct |
82 |
Correct |
254 ms |
47632 KB |
Output is correct |
83 |
Correct |
199 ms |
45192 KB |
Output is correct |
84 |
Correct |
164 ms |
44664 KB |
Output is correct |
85 |
Correct |
239 ms |
47064 KB |
Output is correct |
86 |
Correct |
215 ms |
47860 KB |
Output is correct |
87 |
Correct |
231 ms |
48048 KB |
Output is correct |
88 |
Correct |
166 ms |
44232 KB |
Output is correct |
89 |
Correct |
198 ms |
46332 KB |
Output is correct |
90 |
Correct |
282 ms |
45640 KB |
Output is correct |
91 |
Correct |
252 ms |
46440 KB |
Output is correct |
92 |
Correct |
262 ms |
47328 KB |
Output is correct |
93 |
Correct |
275 ms |
48316 KB |
Output is correct |
94 |
Correct |
246 ms |
45060 KB |
Output is correct |
95 |
Correct |
180 ms |
45536 KB |
Output is correct |
96 |
Correct |
129 ms |
45516 KB |
Output is correct |
97 |
Correct |
152 ms |
24784 KB |
Output is correct |
98 |
Correct |
260 ms |
48724 KB |
Output is correct |
99 |
Correct |
202 ms |
46460 KB |
Output is correct |
100 |
Correct |
49 ms |
3900 KB |
Output is correct |
101 |
Correct |
287 ms |
47196 KB |
Output is correct |
102 |
Correct |
242 ms |
37520 KB |
Output is correct |
103 |
Correct |
247 ms |
38196 KB |
Output is correct |
104 |
Correct |
285 ms |
44644 KB |
Output is correct |