#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);
} comp[b].clear();
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);
for(auto k: comp[c]) {
answ[k] = 0;
}
st.erase(st.begin());
}
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 |
13 ms |
19028 KB |
Output is correct |
2 |
Correct |
10 ms |
19028 KB |
Output is correct |
3 |
Correct |
11 ms |
19136 KB |
Output is correct |
4 |
Correct |
14 ms |
19404 KB |
Output is correct |
5 |
Correct |
12 ms |
19540 KB |
Output is correct |
6 |
Correct |
12 ms |
19432 KB |
Output is correct |
7 |
Correct |
12 ms |
19412 KB |
Output is correct |
8 |
Correct |
12 ms |
19352 KB |
Output is correct |
9 |
Correct |
11 ms |
19540 KB |
Output is correct |
10 |
Correct |
12 ms |
19540 KB |
Output is correct |
11 |
Correct |
12 ms |
19540 KB |
Output is correct |
12 |
Correct |
13 ms |
19544 KB |
Output is correct |
13 |
Correct |
13 ms |
19412 KB |
Output is correct |
14 |
Correct |
13 ms |
19540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
19028 KB |
Output is correct |
2 |
Correct |
9 ms |
19028 KB |
Output is correct |
3 |
Correct |
296 ms |
58568 KB |
Output is correct |
4 |
Correct |
282 ms |
58348 KB |
Output is correct |
5 |
Correct |
455 ms |
63624 KB |
Output is correct |
6 |
Correct |
440 ms |
65148 KB |
Output is correct |
7 |
Correct |
438 ms |
65652 KB |
Output is correct |
8 |
Correct |
370 ms |
58364 KB |
Output is correct |
9 |
Correct |
325 ms |
73528 KB |
Output is correct |
10 |
Correct |
222 ms |
59784 KB |
Output is correct |
11 |
Correct |
425 ms |
59696 KB |
Output is correct |
12 |
Correct |
477 ms |
59908 KB |
Output is correct |
13 |
Correct |
310 ms |
57272 KB |
Output is correct |
14 |
Correct |
249 ms |
57304 KB |
Output is correct |
15 |
Correct |
249 ms |
57280 KB |
Output is correct |
16 |
Correct |
193 ms |
56904 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
19028 KB |
Output is correct |
2 |
Correct |
613 ms |
67552 KB |
Output is correct |
3 |
Correct |
598 ms |
67612 KB |
Output is correct |
4 |
Correct |
297 ms |
57348 KB |
Output is correct |
5 |
Correct |
269 ms |
57924 KB |
Output is correct |
6 |
Correct |
600 ms |
67708 KB |
Output is correct |
7 |
Correct |
265 ms |
57268 KB |
Output is correct |
8 |
Correct |
285 ms |
57276 KB |
Output is correct |
9 |
Correct |
195 ms |
56900 KB |
Output is correct |
10 |
Correct |
280 ms |
57312 KB |
Output is correct |
11 |
Correct |
360 ms |
58224 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
19028 KB |
Output is correct |
2 |
Correct |
525 ms |
61088 KB |
Output is correct |
3 |
Correct |
518 ms |
62404 KB |
Output is correct |
4 |
Correct |
541 ms |
64212 KB |
Output is correct |
5 |
Correct |
584 ms |
62848 KB |
Output is correct |
6 |
Correct |
543 ms |
58496 KB |
Output is correct |
7 |
Correct |
303 ms |
59704 KB |
Output is correct |
8 |
Correct |
216 ms |
58852 KB |
Output is correct |
9 |
Correct |
215 ms |
42128 KB |
Output is correct |
10 |
Correct |
553 ms |
64412 KB |
Output is correct |
11 |
Correct |
354 ms |
57752 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
19028 KB |
Output is correct |
2 |
Correct |
10 ms |
19028 KB |
Output is correct |
3 |
Correct |
11 ms |
19136 KB |
Output is correct |
4 |
Correct |
14 ms |
19404 KB |
Output is correct |
5 |
Correct |
12 ms |
19540 KB |
Output is correct |
6 |
Correct |
12 ms |
19432 KB |
Output is correct |
7 |
Correct |
12 ms |
19412 KB |
Output is correct |
8 |
Correct |
12 ms |
19352 KB |
Output is correct |
9 |
Correct |
11 ms |
19540 KB |
Output is correct |
10 |
Correct |
12 ms |
19540 KB |
Output is correct |
11 |
Correct |
12 ms |
19540 KB |
Output is correct |
12 |
Correct |
13 ms |
19544 KB |
Output is correct |
13 |
Correct |
13 ms |
19412 KB |
Output is correct |
14 |
Correct |
13 ms |
19540 KB |
Output is correct |
15 |
Correct |
10 ms |
19028 KB |
Output is correct |
16 |
Correct |
9 ms |
19028 KB |
Output is correct |
17 |
Correct |
296 ms |
58568 KB |
Output is correct |
18 |
Correct |
282 ms |
58348 KB |
Output is correct |
19 |
Correct |
455 ms |
63624 KB |
Output is correct |
20 |
Correct |
440 ms |
65148 KB |
Output is correct |
21 |
Correct |
438 ms |
65652 KB |
Output is correct |
22 |
Correct |
370 ms |
58364 KB |
Output is correct |
23 |
Correct |
325 ms |
73528 KB |
Output is correct |
24 |
Correct |
222 ms |
59784 KB |
Output is correct |
25 |
Correct |
425 ms |
59696 KB |
Output is correct |
26 |
Correct |
477 ms |
59908 KB |
Output is correct |
27 |
Correct |
310 ms |
57272 KB |
Output is correct |
28 |
Correct |
249 ms |
57304 KB |
Output is correct |
29 |
Correct |
249 ms |
57280 KB |
Output is correct |
30 |
Correct |
193 ms |
56904 KB |
Output is correct |
31 |
Correct |
10 ms |
19028 KB |
Output is correct |
32 |
Correct |
613 ms |
67552 KB |
Output is correct |
33 |
Correct |
598 ms |
67612 KB |
Output is correct |
34 |
Correct |
297 ms |
57348 KB |
Output is correct |
35 |
Correct |
269 ms |
57924 KB |
Output is correct |
36 |
Correct |
600 ms |
67708 KB |
Output is correct |
37 |
Correct |
265 ms |
57268 KB |
Output is correct |
38 |
Correct |
285 ms |
57276 KB |
Output is correct |
39 |
Correct |
195 ms |
56900 KB |
Output is correct |
40 |
Correct |
280 ms |
57312 KB |
Output is correct |
41 |
Correct |
360 ms |
58224 KB |
Output is correct |
42 |
Correct |
10 ms |
19028 KB |
Output is correct |
43 |
Correct |
525 ms |
61088 KB |
Output is correct |
44 |
Correct |
518 ms |
62404 KB |
Output is correct |
45 |
Correct |
541 ms |
64212 KB |
Output is correct |
46 |
Correct |
584 ms |
62848 KB |
Output is correct |
47 |
Correct |
543 ms |
58496 KB |
Output is correct |
48 |
Correct |
303 ms |
59704 KB |
Output is correct |
49 |
Correct |
216 ms |
58852 KB |
Output is correct |
50 |
Correct |
215 ms |
42128 KB |
Output is correct |
51 |
Correct |
553 ms |
64412 KB |
Output is correct |
52 |
Correct |
354 ms |
57752 KB |
Output is correct |
53 |
Correct |
11 ms |
19028 KB |
Output is correct |
54 |
Correct |
12 ms |
19028 KB |
Output is correct |
55 |
Correct |
10 ms |
19028 KB |
Output is correct |
56 |
Correct |
12 ms |
19496 KB |
Output is correct |
57 |
Correct |
12 ms |
19484 KB |
Output is correct |
58 |
Correct |
11 ms |
19480 KB |
Output is correct |
59 |
Correct |
13 ms |
19412 KB |
Output is correct |
60 |
Correct |
12 ms |
19412 KB |
Output is correct |
61 |
Correct |
13 ms |
19516 KB |
Output is correct |
62 |
Correct |
12 ms |
19516 KB |
Output is correct |
63 |
Correct |
12 ms |
19540 KB |
Output is correct |
64 |
Correct |
14 ms |
19676 KB |
Output is correct |
65 |
Correct |
11 ms |
19412 KB |
Output is correct |
66 |
Correct |
12 ms |
19540 KB |
Output is correct |
67 |
Correct |
280 ms |
58384 KB |
Output is correct |
68 |
Correct |
294 ms |
58248 KB |
Output is correct |
69 |
Correct |
443 ms |
63876 KB |
Output is correct |
70 |
Correct |
429 ms |
65148 KB |
Output is correct |
71 |
Correct |
453 ms |
65548 KB |
Output is correct |
72 |
Correct |
390 ms |
58260 KB |
Output is correct |
73 |
Correct |
277 ms |
73516 KB |
Output is correct |
74 |
Correct |
195 ms |
59696 KB |
Output is correct |
75 |
Correct |
409 ms |
59784 KB |
Output is correct |
76 |
Correct |
477 ms |
59880 KB |
Output is correct |
77 |
Correct |
273 ms |
57364 KB |
Output is correct |
78 |
Correct |
313 ms |
57336 KB |
Output is correct |
79 |
Correct |
234 ms |
57288 KB |
Output is correct |
80 |
Correct |
192 ms |
56860 KB |
Output is correct |
81 |
Correct |
603 ms |
67584 KB |
Output is correct |
82 |
Correct |
587 ms |
67708 KB |
Output is correct |
83 |
Correct |
296 ms |
57264 KB |
Output is correct |
84 |
Correct |
255 ms |
57848 KB |
Output is correct |
85 |
Correct |
617 ms |
67700 KB |
Output is correct |
86 |
Correct |
255 ms |
57300 KB |
Output is correct |
87 |
Correct |
252 ms |
57320 KB |
Output is correct |
88 |
Correct |
284 ms |
57308 KB |
Output is correct |
89 |
Correct |
342 ms |
58180 KB |
Output is correct |
90 |
Correct |
544 ms |
61112 KB |
Output is correct |
91 |
Correct |
526 ms |
62392 KB |
Output is correct |
92 |
Correct |
533 ms |
64260 KB |
Output is correct |
93 |
Correct |
622 ms |
62772 KB |
Output is correct |
94 |
Correct |
523 ms |
58596 KB |
Output is correct |
95 |
Correct |
301 ms |
59720 KB |
Output is correct |
96 |
Correct |
216 ms |
58864 KB |
Output is correct |
97 |
Correct |
207 ms |
42104 KB |
Output is correct |
98 |
Correct |
573 ms |
64352 KB |
Output is correct |
99 |
Correct |
344 ms |
57704 KB |
Output is correct |
100 |
Correct |
55 ms |
24252 KB |
Output is correct |
101 |
Correct |
601 ms |
63836 KB |
Output is correct |
102 |
Correct |
435 ms |
52116 KB |
Output is correct |
103 |
Correct |
454 ms |
53820 KB |
Output is correct |
104 |
Correct |
574 ms |
59872 KB |
Output is correct |