#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define MASK(i) (1LL << (i))
#define GETBIT(mask, i) (((mask) >> (i)) & 1)
#define ALL(v) (v).begin(), (v).end()
#define block_of_code if(true)
ll max(ll a, ll b){return (a > b) ? a : b;}
ll min(ll a, ll b){return (a < b) ? a : b;}
ll gcd(ll a, ll b){return __gcd(a, b);}
ll lcm(ll a, ll b){return a / gcd(a, b) * b;}
ll LASTBIT(ll mask){return (mask) & (-mask);}
int pop_cnt(ll mask){return __builtin_popcountll(mask);}
int ctz(ull mask){return __builtin_ctzll(mask);}
int logOf(ull mask){return 63 - __builtin_clzll(mask);}
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rngesus(ll l, ll r){return l + (ull) rng() % (r - l + 1);}
double rngesus_d(double l, double r){
double cur = rngesus(0, MASK(60) - 1);
cur /= MASK(60) - 1;
return l + cur * (r - l);
}
template <class T1, class T2>
bool maximize(T1 &a, T2 b){
if (a < b) {a = b; return true;}
return false;
}
template <class T1, class T2>
bool minimize(T1 &a, T2 b){
if (a > b) {a = b; return true;}
return false;
}
template <class T>
void printArr(T container, string separator = " ", string finish = "\n", ostream &out = cout){
for(auto item: container) out << item << separator;
out << finish;
}
template <class T>
void remove_dup(vector<T> &a){
sort(ALL(a));
a.resize(unique(ALL(a)) - a.begin());
}
struct DSU{
int n;
vector<int> parent, sz;
vector<ll> sum;
DSU(int _n){
n = _n;
parent.resize(n+1); sz.resize(n+1, 1);
sum.resize(n+1);
for(int i = 1; i<=n; ++i) parent[i] = i;
}
int find_set(int u){return (u == parent[u]) ? u : (parent[u] = find_set(parent[u]));}
bool same_set(int u, int v){return find_set(u) == find_set(v);}
bool join_set(int u, int v){
u = find_set(u), v = find_set(v);
if (u != v){
if (sz[u] < sz[v]) swap(u, v);
parent[v] = u;
sz[u] += sz[v];
sum[u] += sum[v];
return true;
}
return false;
}
int get_size(int u){return sz[find_set(u)];}
ll get_sum(int u){return sum[find_set(u)];}
};
const int N = 2e5 + 69;
int n, m;
int a[N];
vector<int> graph[N];
int main(void){
ios::sync_with_stdio(0);cin.tie(0); cout.tie(0);
clock_t start = clock();
cin >> n >> m;
for(int i = 1; i<=n; ++i) cin >> a[i];
for(int i = 0; i<m; ++i){
int u, v; cin >> u >> v;
graph[u].push_back(v);
graph[v].push_back(u);
}
vector<int> val;
for(int i = 1; i<=n; ++i) val.push_back(a[i]);
remove_dup(val);
int k = val.size();
vector<vector<int>> ver(k);
vector<vector<pair<int, ll>>> queries(k);
for(int i = 1; i<=n; ++i){
int idx = lower_bound(ALL(val), a[i]) - val.begin();
ver[idx].push_back(i);
queries[idx].push_back({i, a[i]});
}
DSU mst(n);
for(int i = 1; i<=n; ++i) mst.sum[i] = a[i];
vector<ll> ans(n+1);
for(int i = 0; i< val.size(); ++i){
for(int u: ver[i]){
for(int v: graph[u]) if (a[v] <= a[u]) mst.join_set(u, v);
}
while(queries[i].size()){
pair<int, ll> cur = queries[i].back(); queries[i].pop_back();
ans[cur.first] = cur.second;
ll sigma = mst.get_sum(cur.first);
if (sigma > cur.second){
int idx = upper_bound(ALL(val), sigma) - val.begin() - 1;
queries[idx].push_back({cur.first, sigma});
}
}
}
string s;
for(int i = 1; i<=n; ++i) if (ans[i] == mst.get_sum(i)) s.push_back('1');
else s.push_back('0');
cout << s << "\n";
cerr << "Time elapsed: " << clock() - start << " ms\n";
return 0;
}
Compilation message
island.cpp: In function 'int main()':
island.cpp:120:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
120 | for(int i = 0; i< val.size(); ++i){
| ~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
3 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
4956 KB |
Output is correct |
4 |
Correct |
3 ms |
5468 KB |
Output is correct |
5 |
Correct |
3 ms |
5468 KB |
Output is correct |
6 |
Correct |
2 ms |
5212 KB |
Output is correct |
7 |
Correct |
3 ms |
5468 KB |
Output is correct |
8 |
Correct |
3 ms |
5468 KB |
Output is correct |
9 |
Correct |
3 ms |
5168 KB |
Output is correct |
10 |
Correct |
3 ms |
5424 KB |
Output is correct |
11 |
Correct |
4 ms |
5464 KB |
Output is correct |
12 |
Correct |
3 ms |
5468 KB |
Output is correct |
13 |
Correct |
3 ms |
5212 KB |
Output is correct |
14 |
Correct |
3 ms |
5212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4956 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
145 ms |
49392 KB |
Output is correct |
4 |
Correct |
75 ms |
26044 KB |
Output is correct |
5 |
Correct |
141 ms |
47100 KB |
Output is correct |
6 |
Correct |
139 ms |
44124 KB |
Output is correct |
7 |
Correct |
155 ms |
48332 KB |
Output is correct |
8 |
Correct |
95 ms |
33552 KB |
Output is correct |
9 |
Correct |
106 ms |
42828 KB |
Output is correct |
10 |
Correct |
84 ms |
40260 KB |
Output is correct |
11 |
Correct |
63 ms |
26436 KB |
Output is correct |
12 |
Correct |
73 ms |
26052 KB |
Output is correct |
13 |
Correct |
76 ms |
25024 KB |
Output is correct |
14 |
Correct |
70 ms |
26064 KB |
Output is correct |
15 |
Correct |
128 ms |
48196 KB |
Output is correct |
16 |
Correct |
88 ms |
47816 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4952 KB |
Output is correct |
2 |
Correct |
228 ms |
48484 KB |
Output is correct |
3 |
Correct |
216 ms |
44656 KB |
Output is correct |
4 |
Correct |
80 ms |
26636 KB |
Output is correct |
5 |
Correct |
67 ms |
25024 KB |
Output is correct |
6 |
Correct |
221 ms |
48836 KB |
Output is correct |
7 |
Correct |
135 ms |
48204 KB |
Output is correct |
8 |
Correct |
148 ms |
48196 KB |
Output is correct |
9 |
Correct |
90 ms |
47820 KB |
Output is correct |
10 |
Correct |
93 ms |
37284 KB |
Output is correct |
11 |
Correct |
82 ms |
29124 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
100 ms |
30496 KB |
Output is correct |
3 |
Correct |
93 ms |
31032 KB |
Output is correct |
4 |
Correct |
101 ms |
31180 KB |
Output is correct |
5 |
Correct |
87 ms |
26552 KB |
Output is correct |
6 |
Correct |
83 ms |
25716 KB |
Output is correct |
7 |
Correct |
75 ms |
34312 KB |
Output is correct |
8 |
Correct |
75 ms |
28948 KB |
Output is correct |
9 |
Correct |
64 ms |
19160 KB |
Output is correct |
10 |
Correct |
76 ms |
25032 KB |
Output is correct |
11 |
Correct |
79 ms |
29888 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4956 KB |
Output is correct |
2 |
Correct |
3 ms |
4956 KB |
Output is correct |
3 |
Correct |
2 ms |
4956 KB |
Output is correct |
4 |
Correct |
3 ms |
5468 KB |
Output is correct |
5 |
Correct |
3 ms |
5468 KB |
Output is correct |
6 |
Correct |
2 ms |
5212 KB |
Output is correct |
7 |
Correct |
3 ms |
5468 KB |
Output is correct |
8 |
Correct |
3 ms |
5468 KB |
Output is correct |
9 |
Correct |
3 ms |
5168 KB |
Output is correct |
10 |
Correct |
3 ms |
5424 KB |
Output is correct |
11 |
Correct |
4 ms |
5464 KB |
Output is correct |
12 |
Correct |
3 ms |
5468 KB |
Output is correct |
13 |
Correct |
3 ms |
5212 KB |
Output is correct |
14 |
Correct |
3 ms |
5212 KB |
Output is correct |
15 |
Correct |
3 ms |
4956 KB |
Output is correct |
16 |
Correct |
2 ms |
4956 KB |
Output is correct |
17 |
Correct |
145 ms |
49392 KB |
Output is correct |
18 |
Correct |
75 ms |
26044 KB |
Output is correct |
19 |
Correct |
141 ms |
47100 KB |
Output is correct |
20 |
Correct |
139 ms |
44124 KB |
Output is correct |
21 |
Correct |
155 ms |
48332 KB |
Output is correct |
22 |
Correct |
95 ms |
33552 KB |
Output is correct |
23 |
Correct |
106 ms |
42828 KB |
Output is correct |
24 |
Correct |
84 ms |
40260 KB |
Output is correct |
25 |
Correct |
63 ms |
26436 KB |
Output is correct |
26 |
Correct |
73 ms |
26052 KB |
Output is correct |
27 |
Correct |
76 ms |
25024 KB |
Output is correct |
28 |
Correct |
70 ms |
26064 KB |
Output is correct |
29 |
Correct |
128 ms |
48196 KB |
Output is correct |
30 |
Correct |
88 ms |
47816 KB |
Output is correct |
31 |
Correct |
2 ms |
4952 KB |
Output is correct |
32 |
Correct |
228 ms |
48484 KB |
Output is correct |
33 |
Correct |
216 ms |
44656 KB |
Output is correct |
34 |
Correct |
80 ms |
26636 KB |
Output is correct |
35 |
Correct |
67 ms |
25024 KB |
Output is correct |
36 |
Correct |
221 ms |
48836 KB |
Output is correct |
37 |
Correct |
135 ms |
48204 KB |
Output is correct |
38 |
Correct |
148 ms |
48196 KB |
Output is correct |
39 |
Correct |
90 ms |
47820 KB |
Output is correct |
40 |
Correct |
93 ms |
37284 KB |
Output is correct |
41 |
Correct |
82 ms |
29124 KB |
Output is correct |
42 |
Correct |
2 ms |
4956 KB |
Output is correct |
43 |
Correct |
100 ms |
30496 KB |
Output is correct |
44 |
Correct |
93 ms |
31032 KB |
Output is correct |
45 |
Correct |
101 ms |
31180 KB |
Output is correct |
46 |
Correct |
87 ms |
26552 KB |
Output is correct |
47 |
Correct |
83 ms |
25716 KB |
Output is correct |
48 |
Correct |
75 ms |
34312 KB |
Output is correct |
49 |
Correct |
75 ms |
28948 KB |
Output is correct |
50 |
Correct |
64 ms |
19160 KB |
Output is correct |
51 |
Correct |
76 ms |
25032 KB |
Output is correct |
52 |
Correct |
79 ms |
29888 KB |
Output is correct |
53 |
Correct |
3 ms |
4952 KB |
Output is correct |
54 |
Correct |
2 ms |
4956 KB |
Output is correct |
55 |
Correct |
2 ms |
4952 KB |
Output is correct |
56 |
Correct |
3 ms |
5468 KB |
Output is correct |
57 |
Correct |
4 ms |
5356 KB |
Output is correct |
58 |
Correct |
4 ms |
5168 KB |
Output is correct |
59 |
Correct |
3 ms |
5464 KB |
Output is correct |
60 |
Correct |
4 ms |
5464 KB |
Output is correct |
61 |
Correct |
3 ms |
5212 KB |
Output is correct |
62 |
Correct |
3 ms |
5464 KB |
Output is correct |
63 |
Correct |
3 ms |
5424 KB |
Output is correct |
64 |
Correct |
3 ms |
5464 KB |
Output is correct |
65 |
Correct |
2 ms |
5212 KB |
Output is correct |
66 |
Correct |
3 ms |
5212 KB |
Output is correct |
67 |
Correct |
121 ms |
49344 KB |
Output is correct |
68 |
Correct |
72 ms |
26044 KB |
Output is correct |
69 |
Correct |
130 ms |
47052 KB |
Output is correct |
70 |
Correct |
129 ms |
43972 KB |
Output is correct |
71 |
Correct |
129 ms |
48332 KB |
Output is correct |
72 |
Correct |
92 ms |
33400 KB |
Output is correct |
73 |
Correct |
114 ms |
42820 KB |
Output is correct |
74 |
Correct |
84 ms |
40260 KB |
Output is correct |
75 |
Correct |
67 ms |
26432 KB |
Output is correct |
76 |
Correct |
77 ms |
26052 KB |
Output is correct |
77 |
Correct |
69 ms |
24980 KB |
Output is correct |
78 |
Correct |
70 ms |
25988 KB |
Output is correct |
79 |
Correct |
109 ms |
48200 KB |
Output is correct |
80 |
Correct |
109 ms |
47820 KB |
Output is correct |
81 |
Correct |
213 ms |
48588 KB |
Output is correct |
82 |
Correct |
194 ms |
44488 KB |
Output is correct |
83 |
Correct |
74 ms |
26556 KB |
Output is correct |
84 |
Correct |
98 ms |
24968 KB |
Output is correct |
85 |
Correct |
205 ms |
48692 KB |
Output is correct |
86 |
Correct |
130 ms |
48120 KB |
Output is correct |
87 |
Correct |
130 ms |
48192 KB |
Output is correct |
88 |
Correct |
119 ms |
37316 KB |
Output is correct |
89 |
Correct |
98 ms |
29120 KB |
Output is correct |
90 |
Correct |
91 ms |
30408 KB |
Output is correct |
91 |
Correct |
95 ms |
30948 KB |
Output is correct |
92 |
Correct |
91 ms |
31180 KB |
Output is correct |
93 |
Correct |
117 ms |
26556 KB |
Output is correct |
94 |
Correct |
83 ms |
25836 KB |
Output is correct |
95 |
Correct |
78 ms |
34200 KB |
Output is correct |
96 |
Correct |
60 ms |
28868 KB |
Output is correct |
97 |
Correct |
78 ms |
19140 KB |
Output is correct |
98 |
Correct |
80 ms |
25028 KB |
Output is correct |
99 |
Correct |
94 ms |
29876 KB |
Output is correct |
100 |
Correct |
29 ms |
9552 KB |
Output is correct |
101 |
Correct |
196 ms |
44240 KB |
Output is correct |
102 |
Correct |
74 ms |
22748 KB |
Output is correct |
103 |
Correct |
167 ms |
38600 KB |
Output is correct |
104 |
Correct |
230 ms |
47804 KB |
Output is correct |