#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pld = pair<ld, ld>;
#define fi first
#define se second
#define pb push_back
#define pf push_front
#define mp make_pair
#define ins insert
#define btpc __builtin_popcount
#define btclz __builtin_clz
#define sz(x) (int)(x.size());
#define all(x) x.begin(), x.end()
#define debug(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int d4x[4] = {1, 0, -1, 0}; int d4y[4] = {0, 1, 0, -1};
int d8x[8] = {0, 1, 1, 1, 0, -1, -1, -1};
int d8y[8] = {1, 1, 0, -1, -1, -1, 0, 1};
template<class X, class Y>
bool minimize(X &x, const Y &y) {
if (x > y)
{
x = y;
return true;
}
return false;
}
template<class X, class Y>
bool maximize(X &x, const Y &y) {
if (x < y)
{
x = y;
return true;
}
return false;
}
const int MOD = 1e9 + 7; //998244353
template<class X, class Y>
void add(X &x, const Y &y) {
x = (x + y);
if(x >= MOD) x -= MOD;
}
template<class X, class Y>
void sub(X &x, const Y &y) {
x = (x - y);
if(x < 0) x += MOD;
}
/* Author : Le Ngoc Bao Anh, 12A5, LQD High School for Gifted Student*/
const ll INF = 1e9;
const int N = 2e5 + 10;
int par[N], id[N], ans[N];
ll sum[N], a[N];
vector<int> g[N], son[N];
int find_par(int u) {
if(par[u] < 0) return u;
par[u] = find_par(par[u]);
return par[u];
}
void merge(int u, int v) {
u = find_par(u), v = find_par(v);
if(u == v) return;
if(par[u] > par[v]) swap(u, v);
par[u] += par[v];
sum[u] += sum[v];
par[v] = u;
for(auto x : son[v]) son[u].pb(x);
}
bool cmp(int x, int y) {
return a[x] < a[y];
}
void solve() {
int n, m; cin >> n >> m;
for(int i = 1; i <= n; i++) cin >> a[i];
for(int i = 1; i <= m; i++) {
int u, v; cin >> u >> v;
g[u].pb(v);
g[v].pb(u);
}
for(int i = 1; i <= n; i++) id[i] = i, sum[i] = a[i], son[i].pb(i), par[i] = -1;
sort(id + 1, id + 1 + n, cmp);
for(int i = 1; i <= n; i++) {
int u = id[i];
for(auto v : g[u]) {
if(a[v] < a[u]) {
v = find_par(v);
if(sum[v] < a[u]) {
for(auto x : son[v]) ans[x] = 1;
son[v].clear();
}
}
if(a[v] <= a[u]) {
merge(u, v);
}
}
}
for(int i = 1; i <= n; i++) cout << (ans[i] ^ 1);
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int tc = 1, ddd = 0;
// cin >> tc;
while(tc--) {
//ddd++;
//cout << "Case #" << ddd << ": ";
solve();
}
}
Compilation message
island.cpp: In function 'int main()':
island.cpp:131:17: warning: unused variable 'ddd' [-Wunused-variable]
131 | int tc = 1, ddd = 0;
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
9728 KB |
Output is correct |
2 |
Correct |
6 ms |
9684 KB |
Output is correct |
3 |
Correct |
6 ms |
9684 KB |
Output is correct |
4 |
Correct |
7 ms |
9892 KB |
Output is correct |
5 |
Correct |
8 ms |
9940 KB |
Output is correct |
6 |
Correct |
6 ms |
9896 KB |
Output is correct |
7 |
Correct |
7 ms |
9872 KB |
Output is correct |
8 |
Correct |
7 ms |
9812 KB |
Output is correct |
9 |
Correct |
9 ms |
9940 KB |
Output is correct |
10 |
Correct |
9 ms |
9940 KB |
Output is correct |
11 |
Correct |
8 ms |
9996 KB |
Output is correct |
12 |
Correct |
7 ms |
10000 KB |
Output is correct |
13 |
Correct |
8 ms |
9868 KB |
Output is correct |
14 |
Correct |
7 ms |
9940 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
9684 KB |
Output is correct |
2 |
Correct |
9 ms |
9732 KB |
Output is correct |
3 |
Correct |
255 ms |
32528 KB |
Output is correct |
4 |
Correct |
230 ms |
34724 KB |
Output is correct |
5 |
Correct |
269 ms |
34284 KB |
Output is correct |
6 |
Correct |
320 ms |
35128 KB |
Output is correct |
7 |
Correct |
330 ms |
35024 KB |
Output is correct |
8 |
Correct |
272 ms |
34184 KB |
Output is correct |
9 |
Correct |
183 ms |
36000 KB |
Output is correct |
10 |
Correct |
161 ms |
32220 KB |
Output is correct |
11 |
Correct |
152 ms |
32556 KB |
Output is correct |
12 |
Correct |
243 ms |
32584 KB |
Output is correct |
13 |
Correct |
183 ms |
35272 KB |
Output is correct |
14 |
Correct |
170 ms |
34856 KB |
Output is correct |
15 |
Correct |
172 ms |
32504 KB |
Output is correct |
16 |
Correct |
103 ms |
31648 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
9684 KB |
Output is correct |
2 |
Correct |
268 ms |
35152 KB |
Output is correct |
3 |
Correct |
247 ms |
35976 KB |
Output is correct |
4 |
Correct |
270 ms |
36000 KB |
Output is correct |
5 |
Correct |
160 ms |
35004 KB |
Output is correct |
6 |
Correct |
263 ms |
36104 KB |
Output is correct |
7 |
Correct |
220 ms |
32580 KB |
Output is correct |
8 |
Correct |
215 ms |
32576 KB |
Output is correct |
9 |
Correct |
102 ms |
31684 KB |
Output is correct |
10 |
Correct |
195 ms |
33628 KB |
Output is correct |
11 |
Correct |
208 ms |
34200 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
9684 KB |
Output is correct |
2 |
Correct |
295 ms |
33076 KB |
Output is correct |
3 |
Correct |
291 ms |
34344 KB |
Output is correct |
4 |
Correct |
289 ms |
35164 KB |
Output is correct |
5 |
Correct |
337 ms |
35288 KB |
Output is correct |
6 |
Correct |
241 ms |
33000 KB |
Output is correct |
7 |
Correct |
149 ms |
33652 KB |
Output is correct |
8 |
Correct |
127 ms |
32932 KB |
Output is correct |
9 |
Correct |
159 ms |
22892 KB |
Output is correct |
10 |
Correct |
247 ms |
35200 KB |
Output is correct |
11 |
Correct |
204 ms |
34380 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
9728 KB |
Output is correct |
2 |
Correct |
6 ms |
9684 KB |
Output is correct |
3 |
Correct |
6 ms |
9684 KB |
Output is correct |
4 |
Correct |
7 ms |
9892 KB |
Output is correct |
5 |
Correct |
8 ms |
9940 KB |
Output is correct |
6 |
Correct |
6 ms |
9896 KB |
Output is correct |
7 |
Correct |
7 ms |
9872 KB |
Output is correct |
8 |
Correct |
7 ms |
9812 KB |
Output is correct |
9 |
Correct |
9 ms |
9940 KB |
Output is correct |
10 |
Correct |
9 ms |
9940 KB |
Output is correct |
11 |
Correct |
8 ms |
9996 KB |
Output is correct |
12 |
Correct |
7 ms |
10000 KB |
Output is correct |
13 |
Correct |
8 ms |
9868 KB |
Output is correct |
14 |
Correct |
7 ms |
9940 KB |
Output is correct |
15 |
Correct |
9 ms |
9684 KB |
Output is correct |
16 |
Correct |
9 ms |
9732 KB |
Output is correct |
17 |
Correct |
255 ms |
32528 KB |
Output is correct |
18 |
Correct |
230 ms |
34724 KB |
Output is correct |
19 |
Correct |
269 ms |
34284 KB |
Output is correct |
20 |
Correct |
320 ms |
35128 KB |
Output is correct |
21 |
Correct |
330 ms |
35024 KB |
Output is correct |
22 |
Correct |
272 ms |
34184 KB |
Output is correct |
23 |
Correct |
183 ms |
36000 KB |
Output is correct |
24 |
Correct |
161 ms |
32220 KB |
Output is correct |
25 |
Correct |
152 ms |
32556 KB |
Output is correct |
26 |
Correct |
243 ms |
32584 KB |
Output is correct |
27 |
Correct |
183 ms |
35272 KB |
Output is correct |
28 |
Correct |
170 ms |
34856 KB |
Output is correct |
29 |
Correct |
172 ms |
32504 KB |
Output is correct |
30 |
Correct |
103 ms |
31648 KB |
Output is correct |
31 |
Correct |
5 ms |
9684 KB |
Output is correct |
32 |
Correct |
268 ms |
35152 KB |
Output is correct |
33 |
Correct |
247 ms |
35976 KB |
Output is correct |
34 |
Correct |
270 ms |
36000 KB |
Output is correct |
35 |
Correct |
160 ms |
35004 KB |
Output is correct |
36 |
Correct |
263 ms |
36104 KB |
Output is correct |
37 |
Correct |
220 ms |
32580 KB |
Output is correct |
38 |
Correct |
215 ms |
32576 KB |
Output is correct |
39 |
Correct |
102 ms |
31684 KB |
Output is correct |
40 |
Correct |
195 ms |
33628 KB |
Output is correct |
41 |
Correct |
208 ms |
34200 KB |
Output is correct |
42 |
Correct |
6 ms |
9684 KB |
Output is correct |
43 |
Correct |
295 ms |
33076 KB |
Output is correct |
44 |
Correct |
291 ms |
34344 KB |
Output is correct |
45 |
Correct |
289 ms |
35164 KB |
Output is correct |
46 |
Correct |
337 ms |
35288 KB |
Output is correct |
47 |
Correct |
241 ms |
33000 KB |
Output is correct |
48 |
Correct |
149 ms |
33652 KB |
Output is correct |
49 |
Correct |
127 ms |
32932 KB |
Output is correct |
50 |
Correct |
159 ms |
22892 KB |
Output is correct |
51 |
Correct |
247 ms |
35200 KB |
Output is correct |
52 |
Correct |
204 ms |
34380 KB |
Output is correct |
53 |
Correct |
6 ms |
9684 KB |
Output is correct |
54 |
Correct |
6 ms |
9684 KB |
Output is correct |
55 |
Correct |
5 ms |
9684 KB |
Output is correct |
56 |
Correct |
8 ms |
9940 KB |
Output is correct |
57 |
Correct |
8 ms |
9996 KB |
Output is correct |
58 |
Correct |
7 ms |
9852 KB |
Output is correct |
59 |
Correct |
7 ms |
9940 KB |
Output is correct |
60 |
Correct |
7 ms |
9812 KB |
Output is correct |
61 |
Correct |
7 ms |
9984 KB |
Output is correct |
62 |
Correct |
16 ms |
10004 KB |
Output is correct |
63 |
Correct |
7 ms |
9940 KB |
Output is correct |
64 |
Correct |
10 ms |
9940 KB |
Output is correct |
65 |
Correct |
9 ms |
9940 KB |
Output is correct |
66 |
Correct |
9 ms |
9872 KB |
Output is correct |
67 |
Correct |
166 ms |
33384 KB |
Output is correct |
68 |
Correct |
167 ms |
34720 KB |
Output is correct |
69 |
Correct |
242 ms |
34192 KB |
Output is correct |
70 |
Correct |
369 ms |
35268 KB |
Output is correct |
71 |
Correct |
255 ms |
34960 KB |
Output is correct |
72 |
Correct |
257 ms |
34112 KB |
Output is correct |
73 |
Correct |
196 ms |
35964 KB |
Output is correct |
74 |
Correct |
157 ms |
32216 KB |
Output is correct |
75 |
Correct |
141 ms |
32616 KB |
Output is correct |
76 |
Correct |
240 ms |
32632 KB |
Output is correct |
77 |
Correct |
185 ms |
35264 KB |
Output is correct |
78 |
Correct |
177 ms |
34768 KB |
Output is correct |
79 |
Correct |
179 ms |
32464 KB |
Output is correct |
80 |
Correct |
105 ms |
31552 KB |
Output is correct |
81 |
Correct |
257 ms |
35984 KB |
Output is correct |
82 |
Correct |
320 ms |
36064 KB |
Output is correct |
83 |
Correct |
205 ms |
36032 KB |
Output is correct |
84 |
Correct |
188 ms |
34988 KB |
Output is correct |
85 |
Correct |
314 ms |
36064 KB |
Output is correct |
86 |
Correct |
207 ms |
32548 KB |
Output is correct |
87 |
Correct |
213 ms |
32616 KB |
Output is correct |
88 |
Correct |
192 ms |
33568 KB |
Output is correct |
89 |
Correct |
225 ms |
34100 KB |
Output is correct |
90 |
Correct |
275 ms |
33792 KB |
Output is correct |
91 |
Correct |
315 ms |
34324 KB |
Output is correct |
92 |
Correct |
337 ms |
35124 KB |
Output is correct |
93 |
Correct |
306 ms |
35256 KB |
Output is correct |
94 |
Correct |
280 ms |
32876 KB |
Output is correct |
95 |
Correct |
211 ms |
33652 KB |
Output is correct |
96 |
Correct |
136 ms |
32944 KB |
Output is correct |
97 |
Correct |
150 ms |
22860 KB |
Output is correct |
98 |
Correct |
212 ms |
35152 KB |
Output is correct |
99 |
Correct |
218 ms |
34372 KB |
Output is correct |
100 |
Correct |
57 ms |
14056 KB |
Output is correct |
101 |
Correct |
336 ms |
34544 KB |
Output is correct |
102 |
Correct |
210 ms |
29068 KB |
Output is correct |
103 |
Correct |
282 ms |
29760 KB |
Output is correct |
104 |
Correct |
256 ms |
33168 KB |
Output is correct |