#include <bits/stdc++.h>
using namespace std;
#define int int64_t
typedef pair<int,int> pii;
typedef vector<int> vii;
#define MP make_pair
#define PB push_back
#define f first
#define s second
#define INF 2e18
#define fast_io() ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((ll)(x).size())
vector<int> canWin;
vector<int> par;
vector<int> parWSum;
vector<vector<int>> membersAtPar;
vector<vector<int>> children;
int N, M;
vector<int> w;
int maxComp = 0;
int Parent(int n)
{
if (par[n] == n || par[n] == -1)
{
return par[n];
}
return Parent(par[n]);
}
void CreateNode(int n)
{
par[n] = n;
parWSum[n] = w[n];
membersAtPar[n].push_back(n);
}
bool Join(int a, int b)
{
if (a == b)
return false;
//cout << "Join(" << a << " " << b << ")\n";
if (membersAtPar[b].size() > membersAtPar[a].size())
{
swap(a, b);
}
par[b] = a;
children[a].push_back(b);
parWSum[a] += parWSum[b];
for (int i : membersAtPar[b])
{
membersAtPar[a].push_back(i);
}
return true;
}
void MarkWinFalse(int n)
{
//if (!canWin[n])
// return;
//cout << "mark false(" << n << ")\n";
canWin[n] = false;
for (int i : children[n])
{
MarkWinFalse(i);
}
}
int32_t main()
{
fast_io();
cin >> N >> M;
canWin = vector<int>(N + 1, true);
par = vector<int>(N + 1, -1);
w = vector<int>(N + 1);
membersAtPar = vector<vector<int>>(N + 1);
parWSum = vector<int>(N + 1);
children = vector<vector<int>>(N + 1);
for (int i = 1; i <= N; i++)
{
cin >> w[i];
}
auto w2 = w;
sort(all(w2));
map<int, vector<pii>> paths;
map<int, vector<int>> verticies;
for (int i = 1; i <= N; i++)
{
verticies[w[i]].push_back(i);
}
for (int i = 0; i < M; i++)
{
int a, b;
cin >> a >> b;
paths[max(w[a], w[b])].push_back({a, b});
}
for (auto it = verticies.begin(); it != verticies.end(); ++it)
{
//cout << "now it(1): " << (*it).f << '\n';
int currVal = (*it).f;
for (int ver : (*it).s)
{
CreateNode(ver);
}
for (pii &edge : paths[(*it).f])
{
//cout << "edge: {" << edge.f << ", " << edge.s << "}\n";
if (w[edge.f] < w[edge.s])
swap(edge.f, edge.s);
int parF = Parent(edge.f);
int parS = Parent(edge.s);
//cout << parS << " par sum: " << parWSum[parS] << '\n';
//cout << "w[" << edge.f << "] = " << w[edge.f] << '\n';
if (parWSum[parS] < w[edge.f])
{
MarkWinFalse(parS);
}
Join(parF, parS);
}
/*int nextVal = (*next(it)).f;
for (int ver : (*it).s)
{
int parent = Parent(ver);
// assert(edge.f == Parent(edge.f));
// assert(edge.s == Parent(edge.s));
cout << "analyze parent: " << parent << '\n';
cout << "nextVal = " << nextVal << '\n';
if (parWSum[parent] < nextVal)
{
MarkWinFalse(parent);
}
}*/
//cout << "now it(2): " << (*it).f << '\n';
//cout << "curr = " << currVal << ", next = " << nextVal << '\n';
/*for (int i = 1; i <= N; i++)
{
cout << "par[" << i << "] = " << par[i] << '\n';
}*/
}
for (int i = 1; i <= N; i++)
{
cout << canWin[i];
}
cout << '\n';
}
Compilation message
island.cpp: In function 'int32_t main()':
island.cpp:120:13: warning: unused variable 'currVal' [-Wunused-variable]
120 | int currVal = (*it).f;
| ^~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
3 ms |
860 KB |
Output is correct |
5 |
Correct |
2 ms |
1116 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
2 ms |
860 KB |
Output is correct |
8 |
Correct |
2 ms |
860 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
2 ms |
1112 KB |
Output is correct |
11 |
Correct |
2 ms |
1116 KB |
Output is correct |
12 |
Correct |
2 ms |
1116 KB |
Output is correct |
13 |
Correct |
1 ms |
860 KB |
Output is correct |
14 |
Correct |
1 ms |
600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
251 ms |
71948 KB |
Output is correct |
4 |
Correct |
129 ms |
45052 KB |
Output is correct |
5 |
Correct |
324 ms |
80700 KB |
Output is correct |
6 |
Correct |
324 ms |
74296 KB |
Output is correct |
7 |
Correct |
348 ms |
83608 KB |
Output is correct |
8 |
Correct |
129 ms |
47688 KB |
Output is correct |
9 |
Correct |
246 ms |
81536 KB |
Output is correct |
10 |
Correct |
171 ms |
64448 KB |
Output is correct |
11 |
Correct |
88 ms |
37960 KB |
Output is correct |
12 |
Correct |
170 ms |
48440 KB |
Output is correct |
13 |
Correct |
136 ms |
52036 KB |
Output is correct |
14 |
Correct |
138 ms |
49732 KB |
Output is correct |
15 |
Correct |
280 ms |
75748 KB |
Output is correct |
16 |
Correct |
180 ms |
74104 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
508 ms |
82480 KB |
Output is correct |
3 |
Correct |
434 ms |
78364 KB |
Output is correct |
4 |
Correct |
156 ms |
53376 KB |
Output is correct |
5 |
Correct |
158 ms |
51304 KB |
Output is correct |
6 |
Correct |
582 ms |
87008 KB |
Output is correct |
7 |
Correct |
320 ms |
76068 KB |
Output is correct |
8 |
Correct |
288 ms |
75572 KB |
Output is correct |
9 |
Correct |
176 ms |
74036 KB |
Output is correct |
10 |
Correct |
245 ms |
69908 KB |
Output is correct |
11 |
Correct |
154 ms |
49724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
356 KB |
Output is correct |
2 |
Correct |
154 ms |
37764 KB |
Output is correct |
3 |
Correct |
148 ms |
43312 KB |
Output is correct |
4 |
Correct |
181 ms |
46472 KB |
Output is correct |
5 |
Correct |
143 ms |
45804 KB |
Output is correct |
6 |
Correct |
134 ms |
41544 KB |
Output is correct |
7 |
Correct |
91 ms |
38108 KB |
Output is correct |
8 |
Correct |
66 ms |
37232 KB |
Output is correct |
9 |
Correct |
62 ms |
23232 KB |
Output is correct |
10 |
Correct |
146 ms |
45892 KB |
Output is correct |
11 |
Correct |
142 ms |
48716 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
3 ms |
860 KB |
Output is correct |
5 |
Correct |
2 ms |
1116 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
2 ms |
860 KB |
Output is correct |
8 |
Correct |
2 ms |
860 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
2 ms |
1112 KB |
Output is correct |
11 |
Correct |
2 ms |
1116 KB |
Output is correct |
12 |
Correct |
2 ms |
1116 KB |
Output is correct |
13 |
Correct |
1 ms |
860 KB |
Output is correct |
14 |
Correct |
1 ms |
600 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
251 ms |
71948 KB |
Output is correct |
18 |
Correct |
129 ms |
45052 KB |
Output is correct |
19 |
Correct |
324 ms |
80700 KB |
Output is correct |
20 |
Correct |
324 ms |
74296 KB |
Output is correct |
21 |
Correct |
348 ms |
83608 KB |
Output is correct |
22 |
Correct |
129 ms |
47688 KB |
Output is correct |
23 |
Correct |
246 ms |
81536 KB |
Output is correct |
24 |
Correct |
171 ms |
64448 KB |
Output is correct |
25 |
Correct |
88 ms |
37960 KB |
Output is correct |
26 |
Correct |
170 ms |
48440 KB |
Output is correct |
27 |
Correct |
136 ms |
52036 KB |
Output is correct |
28 |
Correct |
138 ms |
49732 KB |
Output is correct |
29 |
Correct |
280 ms |
75748 KB |
Output is correct |
30 |
Correct |
180 ms |
74104 KB |
Output is correct |
31 |
Correct |
0 ms |
344 KB |
Output is correct |
32 |
Correct |
508 ms |
82480 KB |
Output is correct |
33 |
Correct |
434 ms |
78364 KB |
Output is correct |
34 |
Correct |
156 ms |
53376 KB |
Output is correct |
35 |
Correct |
158 ms |
51304 KB |
Output is correct |
36 |
Correct |
582 ms |
87008 KB |
Output is correct |
37 |
Correct |
320 ms |
76068 KB |
Output is correct |
38 |
Correct |
288 ms |
75572 KB |
Output is correct |
39 |
Correct |
176 ms |
74036 KB |
Output is correct |
40 |
Correct |
245 ms |
69908 KB |
Output is correct |
41 |
Correct |
154 ms |
49724 KB |
Output is correct |
42 |
Correct |
1 ms |
356 KB |
Output is correct |
43 |
Correct |
154 ms |
37764 KB |
Output is correct |
44 |
Correct |
148 ms |
43312 KB |
Output is correct |
45 |
Correct |
181 ms |
46472 KB |
Output is correct |
46 |
Correct |
143 ms |
45804 KB |
Output is correct |
47 |
Correct |
134 ms |
41544 KB |
Output is correct |
48 |
Correct |
91 ms |
38108 KB |
Output is correct |
49 |
Correct |
66 ms |
37232 KB |
Output is correct |
50 |
Correct |
62 ms |
23232 KB |
Output is correct |
51 |
Correct |
146 ms |
45892 KB |
Output is correct |
52 |
Correct |
142 ms |
48716 KB |
Output is correct |
53 |
Correct |
0 ms |
348 KB |
Output is correct |
54 |
Correct |
0 ms |
348 KB |
Output is correct |
55 |
Correct |
0 ms |
348 KB |
Output is correct |
56 |
Correct |
2 ms |
1116 KB |
Output is correct |
57 |
Correct |
3 ms |
908 KB |
Output is correct |
58 |
Correct |
1 ms |
604 KB |
Output is correct |
59 |
Correct |
2 ms |
860 KB |
Output is correct |
60 |
Correct |
2 ms |
860 KB |
Output is correct |
61 |
Correct |
1 ms |
604 KB |
Output is correct |
62 |
Correct |
2 ms |
972 KB |
Output is correct |
63 |
Correct |
3 ms |
1112 KB |
Output is correct |
64 |
Correct |
2 ms |
1116 KB |
Output is correct |
65 |
Correct |
1 ms |
860 KB |
Output is correct |
66 |
Correct |
2 ms |
860 KB |
Output is correct |
67 |
Correct |
287 ms |
76612 KB |
Output is correct |
68 |
Correct |
132 ms |
48116 KB |
Output is correct |
69 |
Correct |
341 ms |
80588 KB |
Output is correct |
70 |
Correct |
311 ms |
74300 KB |
Output is correct |
71 |
Correct |
343 ms |
83696 KB |
Output is correct |
72 |
Correct |
153 ms |
47368 KB |
Output is correct |
73 |
Correct |
250 ms |
81708 KB |
Output is correct |
74 |
Correct |
158 ms |
64640 KB |
Output is correct |
75 |
Correct |
83 ms |
37192 KB |
Output is correct |
76 |
Correct |
161 ms |
48964 KB |
Output is correct |
77 |
Correct |
143 ms |
51288 KB |
Output is correct |
78 |
Correct |
138 ms |
50244 KB |
Output is correct |
79 |
Correct |
262 ms |
75324 KB |
Output is correct |
80 |
Correct |
174 ms |
74036 KB |
Output is correct |
81 |
Correct |
563 ms |
87184 KB |
Output is correct |
82 |
Correct |
434 ms |
78396 KB |
Output is correct |
83 |
Correct |
158 ms |
52568 KB |
Output is correct |
84 |
Correct |
147 ms |
51380 KB |
Output is correct |
85 |
Correct |
542 ms |
87068 KB |
Output is correct |
86 |
Correct |
271 ms |
75568 KB |
Output is correct |
87 |
Correct |
273 ms |
75572 KB |
Output is correct |
88 |
Correct |
215 ms |
69952 KB |
Output is correct |
89 |
Correct |
150 ms |
49856 KB |
Output is correct |
90 |
Correct |
139 ms |
41972 KB |
Output is correct |
91 |
Correct |
145 ms |
43404 KB |
Output is correct |
92 |
Correct |
147 ms |
46476 KB |
Output is correct |
93 |
Correct |
155 ms |
46528 KB |
Output is correct |
94 |
Correct |
143 ms |
40780 KB |
Output is correct |
95 |
Correct |
103 ms |
38212 KB |
Output is correct |
96 |
Correct |
88 ms |
37248 KB |
Output is correct |
97 |
Correct |
69 ms |
23220 KB |
Output is correct |
98 |
Correct |
150 ms |
45896 KB |
Output is correct |
99 |
Correct |
163 ms |
48816 KB |
Output is correct |
100 |
Correct |
47 ms |
8528 KB |
Output is correct |
101 |
Correct |
511 ms |
73292 KB |
Output is correct |
102 |
Correct |
128 ms |
33608 KB |
Output is correct |
103 |
Correct |
394 ms |
58664 KB |
Output is correct |
104 |
Correct |
583 ms |
77456 KB |
Output is correct |