#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
//#include "temp.cpp"
#include <cstdio>
using namespace std;
#ifndef ONLINE_JUDGE
#define dbg(x) cerr << #x <<" "; print(x); cerr << endl;
#else
#define dbg(x)
#endif
#define sz(x) (int((x).size()))
#define len(x) (int)x.length()
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define clr(x) (x).clear()
#define uniq(x) x.resize(unique(all(x)) - x.begin());
#define blt __builtin_popcount
#define pb push_back
#define popf pop_front
#define popb pop_back
void print(long long t) {cerr << t;}
void print(int t) {cerr << t;}
void print(string t) {cerr << t;}
void print(char t) {cerr << t;}
void print(double t) {cerr << t;}
void print(long double t) {cerr << t;}
void print(unsigned long long t) {cerr << t;}
template <class T, class V> void print(pair <T, V> p);
template <class T> void print(vector <T> v);
template <class T> void print(set <T> v);
template <class T, class V> void print(map <T, V> v);
template <class T> void print(multiset <T> v);
template <class T, class V> void print(T v[],V n) {cerr << "["; for(int i = 0; i < n; i++) {print(v[i]); cerr << " "; } cerr << "]";}
template <class T, class V> void print(pair <T, V> p) {cerr << "{"; print(p.first); cerr << ","; print(p.second); cerr << "}";}
template <class T> void print(vector <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T> void print(deque<T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T> void print(set <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T> void print(multiset <T> v) {cerr << "[ "; for (T i : v) {print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void print(map <T, V> v) {cerr << "[ "; for (auto i : v) {print(i); cerr << " ";} cerr << "]";}
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
#define nl '\n'
// for grid problems
int dx[8] = {-1,0,1,0,1,-1,1,-1};
int dy[8] = {0,1,0,-1,1,1,-1,-1};
// lowest / (1 << 17) >= 1e5 / (1 << 18) >= 2e5 / (1 << 21) >= 1e6
void fastIO() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
}
// file in/out
void setIO(string str = "") {
fastIO();
if (str != "") {
freopen((str + ".in").c_str(), "r", stdin);
freopen((str + ".out").c_str(), "w", stdout);
}
}
// Indexed Set
template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const int N = 4e5 + 10;
long long n, m, p[N], sz[N], answ[N];
long long value[N];
set<pair<long long, int>> st;
vector<long long> adj[N], comp[N];
long long find(long long a) {
if(a != p[a]) {
p[a] = find(p[a]);
}
return p[a];
}
long long merge(long long a, long long b) {
a = find(a), b = find(b);
if(a == b) {
return false;
}
if(sz(comp[a]) < sz(comp[b])) {
swap(a, b);
}
auto it = st.find({value[b], b});
if(it != st.end()) {
st.erase(it);
}
it = st.find({value[a], a});
if(it != st.end()) {
st.erase(it);
}
p[b] = a, sz[a] += sz[b];
value[a] += value[b];
st.insert({value[a], a});
for(auto i: comp[b]) {
comp[a].push_back(i);
}
return true;
}
void solve_() {
cin >> n >> m;
vector<pair<long long, int>> A(n + 1);
vector<long long> B(n + 1);
for(int i = 1; i <= n; i++) {
cin >> A[i].first;
A[i].second = i;
B[i] = A[i].first;
p[i] = i, sz[i] = 1;
value[i] = A[i].first;
comp[i].push_back(i);
st.insert({value[i], i});
answ[i] = 1;
}
for(int i = 1; i <= m; i++) {
int a, b; cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
sort(all(A));
for(int i = 1; i <= n; i++) {
int j = i;
while(j <= n && A[j].first == A[i].first) {
for(auto k: adj[A[j].second]) {
if(B[k] <= A[j].first) {
merge(k, A[j].second);
}
}
j++;
}
if(j > n) {
break;
}
while(!st.empty() && st.begin()->first < A[j].first) {
long long c = find(st.begin()->second);
st.erase(st.begin());
for(auto k: comp[c]) {
answ[k] = 0;
}
comp[c].clear();
}
i = j - 1;
}
for(int i = 1; i <= n; i++) {
printf("%lld", answ[i]);
} printf("\n");
}
int main() {
setIO("");
auto solve = [&](int test_case)-> void {
while(test_case--) {
solve_();
}
};
int test_cases = 1;
solve(test_cases);
return 0;
}
Compilation message
island.cpp: In function 'void setIO(std::string)':
island.cpp:64:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
64 | freopen((str + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
island.cpp:65:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
65 | freopen((str + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
19028 KB |
Output is correct |
2 |
Correct |
10 ms |
19028 KB |
Output is correct |
3 |
Correct |
10 ms |
19116 KB |
Output is correct |
4 |
Correct |
13 ms |
19412 KB |
Output is correct |
5 |
Correct |
12 ms |
19508 KB |
Output is correct |
6 |
Correct |
13 ms |
19400 KB |
Output is correct |
7 |
Correct |
12 ms |
19376 KB |
Output is correct |
8 |
Correct |
13 ms |
19412 KB |
Output is correct |
9 |
Correct |
12 ms |
19540 KB |
Output is correct |
10 |
Correct |
12 ms |
19468 KB |
Output is correct |
11 |
Correct |
13 ms |
19540 KB |
Output is correct |
12 |
Correct |
15 ms |
19520 KB |
Output is correct |
13 |
Correct |
11 ms |
19412 KB |
Output is correct |
14 |
Correct |
15 ms |
19496 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
19028 KB |
Output is correct |
2 |
Correct |
10 ms |
19048 KB |
Output is correct |
3 |
Correct |
321 ms |
58328 KB |
Output is correct |
4 |
Correct |
297 ms |
58344 KB |
Output is correct |
5 |
Correct |
420 ms |
59688 KB |
Output is correct |
6 |
Correct |
425 ms |
61036 KB |
Output is correct |
7 |
Correct |
447 ms |
61172 KB |
Output is correct |
8 |
Correct |
384 ms |
58508 KB |
Output is correct |
9 |
Correct |
273 ms |
64432 KB |
Output is correct |
10 |
Correct |
249 ms |
56952 KB |
Output is correct |
11 |
Correct |
406 ms |
59788 KB |
Output is correct |
12 |
Correct |
508 ms |
58148 KB |
Output is correct |
13 |
Correct |
280 ms |
57420 KB |
Output is correct |
14 |
Correct |
285 ms |
57324 KB |
Output is correct |
15 |
Correct |
247 ms |
57240 KB |
Output is correct |
16 |
Correct |
208 ms |
56892 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
19028 KB |
Output is correct |
2 |
Correct |
628 ms |
61136 KB |
Output is correct |
3 |
Correct |
657 ms |
61536 KB |
Output is correct |
4 |
Correct |
318 ms |
57300 KB |
Output is correct |
5 |
Correct |
276 ms |
57364 KB |
Output is correct |
6 |
Correct |
655 ms |
61368 KB |
Output is correct |
7 |
Correct |
278 ms |
57328 KB |
Output is correct |
8 |
Correct |
279 ms |
57328 KB |
Output is correct |
9 |
Correct |
203 ms |
56844 KB |
Output is correct |
10 |
Correct |
292 ms |
56276 KB |
Output is correct |
11 |
Correct |
359 ms |
56612 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
19028 KB |
Output is correct |
2 |
Correct |
546 ms |
58572 KB |
Output is correct |
3 |
Correct |
587 ms |
59400 KB |
Output is correct |
4 |
Correct |
571 ms |
61400 KB |
Output is correct |
5 |
Correct |
567 ms |
62768 KB |
Output is correct |
6 |
Correct |
544 ms |
58544 KB |
Output is correct |
7 |
Correct |
312 ms |
59784 KB |
Output is correct |
8 |
Correct |
232 ms |
58988 KB |
Output is correct |
9 |
Correct |
218 ms |
41232 KB |
Output is correct |
10 |
Correct |
600 ms |
64564 KB |
Output is correct |
11 |
Correct |
365 ms |
56592 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
19028 KB |
Output is correct |
2 |
Correct |
10 ms |
19028 KB |
Output is correct |
3 |
Correct |
10 ms |
19116 KB |
Output is correct |
4 |
Correct |
13 ms |
19412 KB |
Output is correct |
5 |
Correct |
12 ms |
19508 KB |
Output is correct |
6 |
Correct |
13 ms |
19400 KB |
Output is correct |
7 |
Correct |
12 ms |
19376 KB |
Output is correct |
8 |
Correct |
13 ms |
19412 KB |
Output is correct |
9 |
Correct |
12 ms |
19540 KB |
Output is correct |
10 |
Correct |
12 ms |
19468 KB |
Output is correct |
11 |
Correct |
13 ms |
19540 KB |
Output is correct |
12 |
Correct |
15 ms |
19520 KB |
Output is correct |
13 |
Correct |
11 ms |
19412 KB |
Output is correct |
14 |
Correct |
15 ms |
19496 KB |
Output is correct |
15 |
Correct |
10 ms |
19028 KB |
Output is correct |
16 |
Correct |
10 ms |
19048 KB |
Output is correct |
17 |
Correct |
321 ms |
58328 KB |
Output is correct |
18 |
Correct |
297 ms |
58344 KB |
Output is correct |
19 |
Correct |
420 ms |
59688 KB |
Output is correct |
20 |
Correct |
425 ms |
61036 KB |
Output is correct |
21 |
Correct |
447 ms |
61172 KB |
Output is correct |
22 |
Correct |
384 ms |
58508 KB |
Output is correct |
23 |
Correct |
273 ms |
64432 KB |
Output is correct |
24 |
Correct |
249 ms |
56952 KB |
Output is correct |
25 |
Correct |
406 ms |
59788 KB |
Output is correct |
26 |
Correct |
508 ms |
58148 KB |
Output is correct |
27 |
Correct |
280 ms |
57420 KB |
Output is correct |
28 |
Correct |
285 ms |
57324 KB |
Output is correct |
29 |
Correct |
247 ms |
57240 KB |
Output is correct |
30 |
Correct |
208 ms |
56892 KB |
Output is correct |
31 |
Correct |
11 ms |
19028 KB |
Output is correct |
32 |
Correct |
628 ms |
61136 KB |
Output is correct |
33 |
Correct |
657 ms |
61536 KB |
Output is correct |
34 |
Correct |
318 ms |
57300 KB |
Output is correct |
35 |
Correct |
276 ms |
57364 KB |
Output is correct |
36 |
Correct |
655 ms |
61368 KB |
Output is correct |
37 |
Correct |
278 ms |
57328 KB |
Output is correct |
38 |
Correct |
279 ms |
57328 KB |
Output is correct |
39 |
Correct |
203 ms |
56844 KB |
Output is correct |
40 |
Correct |
292 ms |
56276 KB |
Output is correct |
41 |
Correct |
359 ms |
56612 KB |
Output is correct |
42 |
Correct |
12 ms |
19028 KB |
Output is correct |
43 |
Correct |
546 ms |
58572 KB |
Output is correct |
44 |
Correct |
587 ms |
59400 KB |
Output is correct |
45 |
Correct |
571 ms |
61400 KB |
Output is correct |
46 |
Correct |
567 ms |
62768 KB |
Output is correct |
47 |
Correct |
544 ms |
58544 KB |
Output is correct |
48 |
Correct |
312 ms |
59784 KB |
Output is correct |
49 |
Correct |
232 ms |
58988 KB |
Output is correct |
50 |
Correct |
218 ms |
41232 KB |
Output is correct |
51 |
Correct |
600 ms |
64564 KB |
Output is correct |
52 |
Correct |
365 ms |
56592 KB |
Output is correct |
53 |
Correct |
13 ms |
19116 KB |
Output is correct |
54 |
Correct |
11 ms |
19028 KB |
Output is correct |
55 |
Correct |
13 ms |
19028 KB |
Output is correct |
56 |
Correct |
12 ms |
19412 KB |
Output is correct |
57 |
Correct |
12 ms |
19540 KB |
Output is correct |
58 |
Correct |
13 ms |
19456 KB |
Output is correct |
59 |
Correct |
14 ms |
19416 KB |
Output is correct |
60 |
Correct |
12 ms |
19352 KB |
Output is correct |
61 |
Correct |
13 ms |
19432 KB |
Output is correct |
62 |
Correct |
15 ms |
19568 KB |
Output is correct |
63 |
Correct |
16 ms |
19520 KB |
Output is correct |
64 |
Correct |
13 ms |
19540 KB |
Output is correct |
65 |
Correct |
15 ms |
19412 KB |
Output is correct |
66 |
Correct |
12 ms |
19556 KB |
Output is correct |
67 |
Correct |
298 ms |
58412 KB |
Output is correct |
68 |
Correct |
316 ms |
58312 KB |
Output is correct |
69 |
Correct |
414 ms |
59736 KB |
Output is correct |
70 |
Correct |
383 ms |
60996 KB |
Output is correct |
71 |
Correct |
499 ms |
61120 KB |
Output is correct |
72 |
Correct |
396 ms |
58528 KB |
Output is correct |
73 |
Correct |
319 ms |
64448 KB |
Output is correct |
74 |
Correct |
199 ms |
56972 KB |
Output is correct |
75 |
Correct |
482 ms |
59820 KB |
Output is correct |
76 |
Correct |
454 ms |
58180 KB |
Output is correct |
77 |
Correct |
294 ms |
57448 KB |
Output is correct |
78 |
Correct |
332 ms |
57320 KB |
Output is correct |
79 |
Correct |
272 ms |
57292 KB |
Output is correct |
80 |
Correct |
212 ms |
56932 KB |
Output is correct |
81 |
Correct |
590 ms |
61124 KB |
Output is correct |
82 |
Correct |
618 ms |
61560 KB |
Output is correct |
83 |
Correct |
293 ms |
57280 KB |
Output is correct |
84 |
Correct |
282 ms |
57444 KB |
Output is correct |
85 |
Correct |
598 ms |
61312 KB |
Output is correct |
86 |
Correct |
274 ms |
57284 KB |
Output is correct |
87 |
Correct |
264 ms |
57312 KB |
Output is correct |
88 |
Correct |
294 ms |
56296 KB |
Output is correct |
89 |
Correct |
352 ms |
56676 KB |
Output is correct |
90 |
Correct |
525 ms |
58620 KB |
Output is correct |
91 |
Correct |
560 ms |
59424 KB |
Output is correct |
92 |
Correct |
522 ms |
61428 KB |
Output is correct |
93 |
Correct |
646 ms |
62732 KB |
Output is correct |
94 |
Correct |
569 ms |
58504 KB |
Output is correct |
95 |
Correct |
337 ms |
59676 KB |
Output is correct |
96 |
Correct |
222 ms |
59004 KB |
Output is correct |
97 |
Correct |
194 ms |
41200 KB |
Output is correct |
98 |
Correct |
635 ms |
64332 KB |
Output is correct |
99 |
Correct |
348 ms |
56672 KB |
Output is correct |
100 |
Correct |
49 ms |
24276 KB |
Output is correct |
101 |
Correct |
636 ms |
60000 KB |
Output is correct |
102 |
Correct |
440 ms |
52120 KB |
Output is correct |
103 |
Correct |
445 ms |
52308 KB |
Output is correct |
104 |
Correct |
598 ms |
57688 KB |
Output is correct |