#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;
}
comp[c].clear();
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 |
10 ms |
19028 KB |
Output is correct |
2 |
Correct |
10 ms |
19028 KB |
Output is correct |
3 |
Correct |
10 ms |
19028 KB |
Output is correct |
4 |
Correct |
12 ms |
19412 KB |
Output is correct |
5 |
Correct |
12 ms |
19556 KB |
Output is correct |
6 |
Correct |
13 ms |
19412 KB |
Output is correct |
7 |
Correct |
14 ms |
19480 KB |
Output is correct |
8 |
Correct |
15 ms |
19408 KB |
Output is correct |
9 |
Correct |
14 ms |
19412 KB |
Output is correct |
10 |
Correct |
15 ms |
19540 KB |
Output is correct |
11 |
Correct |
17 ms |
19540 KB |
Output is correct |
12 |
Correct |
14 ms |
19480 KB |
Output is correct |
13 |
Correct |
13 ms |
19412 KB |
Output is correct |
14 |
Correct |
12 ms |
19552 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
19092 KB |
Output is correct |
2 |
Correct |
9 ms |
19056 KB |
Output is correct |
3 |
Correct |
286 ms |
58364 KB |
Output is correct |
4 |
Correct |
300 ms |
58328 KB |
Output is correct |
5 |
Correct |
403 ms |
59636 KB |
Output is correct |
6 |
Correct |
417 ms |
61104 KB |
Output is correct |
7 |
Correct |
440 ms |
61024 KB |
Output is correct |
8 |
Correct |
400 ms |
58432 KB |
Output is correct |
9 |
Correct |
273 ms |
64340 KB |
Output is correct |
10 |
Correct |
220 ms |
56848 KB |
Output is correct |
11 |
Correct |
389 ms |
59704 KB |
Output is correct |
12 |
Correct |
497 ms |
58168 KB |
Output is correct |
13 |
Correct |
264 ms |
57248 KB |
Output is correct |
14 |
Correct |
299 ms |
57508 KB |
Output is correct |
15 |
Correct |
249 ms |
57328 KB |
Output is correct |
16 |
Correct |
200 ms |
56912 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
19080 KB |
Output is correct |
2 |
Correct |
589 ms |
61104 KB |
Output is correct |
3 |
Correct |
620 ms |
61456 KB |
Output is correct |
4 |
Correct |
279 ms |
57216 KB |
Output is correct |
5 |
Correct |
272 ms |
57464 KB |
Output is correct |
6 |
Correct |
607 ms |
61304 KB |
Output is correct |
7 |
Correct |
272 ms |
57264 KB |
Output is correct |
8 |
Correct |
293 ms |
57272 KB |
Output is correct |
9 |
Correct |
201 ms |
56832 KB |
Output is correct |
10 |
Correct |
301 ms |
56260 KB |
Output is correct |
11 |
Correct |
331 ms |
56696 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
19028 KB |
Output is correct |
2 |
Correct |
580 ms |
58596 KB |
Output is correct |
3 |
Correct |
522 ms |
59364 KB |
Output is correct |
4 |
Correct |
509 ms |
61352 KB |
Output is correct |
5 |
Correct |
589 ms |
62780 KB |
Output is correct |
6 |
Correct |
592 ms |
58548 KB |
Output is correct |
7 |
Correct |
312 ms |
59792 KB |
Output is correct |
8 |
Correct |
265 ms |
59160 KB |
Output is correct |
9 |
Correct |
211 ms |
41204 KB |
Output is correct |
10 |
Correct |
608 ms |
64344 KB |
Output is correct |
11 |
Correct |
340 ms |
56724 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 |
19028 KB |
Output is correct |
4 |
Correct |
12 ms |
19412 KB |
Output is correct |
5 |
Correct |
12 ms |
19556 KB |
Output is correct |
6 |
Correct |
13 ms |
19412 KB |
Output is correct |
7 |
Correct |
14 ms |
19480 KB |
Output is correct |
8 |
Correct |
15 ms |
19408 KB |
Output is correct |
9 |
Correct |
14 ms |
19412 KB |
Output is correct |
10 |
Correct |
15 ms |
19540 KB |
Output is correct |
11 |
Correct |
17 ms |
19540 KB |
Output is correct |
12 |
Correct |
14 ms |
19480 KB |
Output is correct |
13 |
Correct |
13 ms |
19412 KB |
Output is correct |
14 |
Correct |
12 ms |
19552 KB |
Output is correct |
15 |
Correct |
11 ms |
19092 KB |
Output is correct |
16 |
Correct |
9 ms |
19056 KB |
Output is correct |
17 |
Correct |
286 ms |
58364 KB |
Output is correct |
18 |
Correct |
300 ms |
58328 KB |
Output is correct |
19 |
Correct |
403 ms |
59636 KB |
Output is correct |
20 |
Correct |
417 ms |
61104 KB |
Output is correct |
21 |
Correct |
440 ms |
61024 KB |
Output is correct |
22 |
Correct |
400 ms |
58432 KB |
Output is correct |
23 |
Correct |
273 ms |
64340 KB |
Output is correct |
24 |
Correct |
220 ms |
56848 KB |
Output is correct |
25 |
Correct |
389 ms |
59704 KB |
Output is correct |
26 |
Correct |
497 ms |
58168 KB |
Output is correct |
27 |
Correct |
264 ms |
57248 KB |
Output is correct |
28 |
Correct |
299 ms |
57508 KB |
Output is correct |
29 |
Correct |
249 ms |
57328 KB |
Output is correct |
30 |
Correct |
200 ms |
56912 KB |
Output is correct |
31 |
Correct |
10 ms |
19080 KB |
Output is correct |
32 |
Correct |
589 ms |
61104 KB |
Output is correct |
33 |
Correct |
620 ms |
61456 KB |
Output is correct |
34 |
Correct |
279 ms |
57216 KB |
Output is correct |
35 |
Correct |
272 ms |
57464 KB |
Output is correct |
36 |
Correct |
607 ms |
61304 KB |
Output is correct |
37 |
Correct |
272 ms |
57264 KB |
Output is correct |
38 |
Correct |
293 ms |
57272 KB |
Output is correct |
39 |
Correct |
201 ms |
56832 KB |
Output is correct |
40 |
Correct |
301 ms |
56260 KB |
Output is correct |
41 |
Correct |
331 ms |
56696 KB |
Output is correct |
42 |
Correct |
10 ms |
19028 KB |
Output is correct |
43 |
Correct |
580 ms |
58596 KB |
Output is correct |
44 |
Correct |
522 ms |
59364 KB |
Output is correct |
45 |
Correct |
509 ms |
61352 KB |
Output is correct |
46 |
Correct |
589 ms |
62780 KB |
Output is correct |
47 |
Correct |
592 ms |
58548 KB |
Output is correct |
48 |
Correct |
312 ms |
59792 KB |
Output is correct |
49 |
Correct |
265 ms |
59160 KB |
Output is correct |
50 |
Correct |
211 ms |
41204 KB |
Output is correct |
51 |
Correct |
608 ms |
64344 KB |
Output is correct |
52 |
Correct |
340 ms |
56724 KB |
Output is correct |
53 |
Correct |
11 ms |
19028 KB |
Output is correct |
54 |
Correct |
10 ms |
19028 KB |
Output is correct |
55 |
Correct |
10 ms |
19028 KB |
Output is correct |
56 |
Correct |
13 ms |
19412 KB |
Output is correct |
57 |
Correct |
13 ms |
19476 KB |
Output is correct |
58 |
Correct |
12 ms |
19456 KB |
Output is correct |
59 |
Correct |
12 ms |
19472 KB |
Output is correct |
60 |
Correct |
11 ms |
19428 KB |
Output is correct |
61 |
Correct |
12 ms |
19540 KB |
Output is correct |
62 |
Correct |
12 ms |
19540 KB |
Output is correct |
63 |
Correct |
15 ms |
19540 KB |
Output is correct |
64 |
Correct |
13 ms |
19512 KB |
Output is correct |
65 |
Correct |
14 ms |
19476 KB |
Output is correct |
66 |
Correct |
14 ms |
19508 KB |
Output is correct |
67 |
Correct |
314 ms |
58308 KB |
Output is correct |
68 |
Correct |
278 ms |
58272 KB |
Output is correct |
69 |
Correct |
458 ms |
59688 KB |
Output is correct |
70 |
Correct |
403 ms |
60984 KB |
Output is correct |
71 |
Correct |
426 ms |
61136 KB |
Output is correct |
72 |
Correct |
411 ms |
58304 KB |
Output is correct |
73 |
Correct |
272 ms |
64344 KB |
Output is correct |
74 |
Correct |
201 ms |
56836 KB |
Output is correct |
75 |
Correct |
395 ms |
59712 KB |
Output is correct |
76 |
Correct |
443 ms |
58144 KB |
Output is correct |
77 |
Correct |
314 ms |
57276 KB |
Output is correct |
78 |
Correct |
263 ms |
57456 KB |
Output is correct |
79 |
Correct |
252 ms |
57320 KB |
Output is correct |
80 |
Correct |
218 ms |
56844 KB |
Output is correct |
81 |
Correct |
618 ms |
61264 KB |
Output is correct |
82 |
Correct |
578 ms |
61708 KB |
Output is correct |
83 |
Correct |
321 ms |
57200 KB |
Output is correct |
84 |
Correct |
268 ms |
57540 KB |
Output is correct |
85 |
Correct |
599 ms |
61288 KB |
Output is correct |
86 |
Correct |
280 ms |
57296 KB |
Output is correct |
87 |
Correct |
283 ms |
57308 KB |
Output is correct |
88 |
Correct |
326 ms |
56284 KB |
Output is correct |
89 |
Correct |
348 ms |
56688 KB |
Output is correct |
90 |
Correct |
567 ms |
58632 KB |
Output is correct |
91 |
Correct |
528 ms |
59336 KB |
Output is correct |
92 |
Correct |
556 ms |
61440 KB |
Output is correct |
93 |
Correct |
631 ms |
62940 KB |
Output is correct |
94 |
Correct |
533 ms |
58588 KB |
Output is correct |
95 |
Correct |
346 ms |
59672 KB |
Output is correct |
96 |
Correct |
233 ms |
58948 KB |
Output is correct |
97 |
Correct |
223 ms |
41364 KB |
Output is correct |
98 |
Correct |
572 ms |
64420 KB |
Output is correct |
99 |
Correct |
359 ms |
56612 KB |
Output is correct |
100 |
Correct |
48 ms |
24284 KB |
Output is correct |
101 |
Correct |
578 ms |
60080 KB |
Output is correct |
102 |
Correct |
405 ms |
52120 KB |
Output is correct |
103 |
Correct |
436 ms |
52228 KB |
Output is correct |
104 |
Correct |
588 ms |
57864 KB |
Output is correct |