#include <bits/stdc++.h>
using namespace std;
#ifdef DEBUG
#define dbg(...) printf(__VA_ARGS__);
#define getchar_unlocked getchar
#else
#define dbg(...)
#endif
#define sf scanf
#define pf printf
#define fi first
#define se second
#define pb push_back
#define psf push_front
#define ppb pop_back
#define ppf pop_front
#define sz(x) (int)x.size()
#define mnto(x,y) x=min(x,(__typeof__(x))y)
#define mxto(x,y) x=max(x,(__typeof__(x))y)
#define INF 1023456789
#define LINF 1023456789123456789
#define all(x) x.begin(),x.end()
#define lb(x,v) (lower_bound(all(x),v)-x.begin())
#define ub(x,v) (upper_bound(all(x),v)-x.begin())
#define disc(x) sort(all(x));x.resize(unique(all(x))-x.begin());
typedef long long ll;
typedef long double ld;
typedef pair<int,int> ii;
typedef pair<ll,ll> pll;
typedef tuple<int,int,int> iii;
typedef tuple<int,int,int,int> iiii;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ii> vii;
mt19937 rng(time(0));
#define mod 1000000007
inline int add(int a,int b){
int r=a+b;
while(r>=mod)r-=mod;
while(r<0)r+=mod;
return r;
}
inline int mult(int a,int b){
return (int)(((ll)(a*b))%mod);
}
inline int rd(){
int x=0;
char ch=getchar_unlocked();
while(!(ch&16))ch=getchar();//keep reading while current character is whitespace
while(ch&16){//this will break when ‘\n’ or ‘ ‘ is encountered
x=(x<<3)+(x<<1)+(ch&15);
ch=getchar_unlocked();
}
return x;
}
#define maxn 200005
int n,m,s[maxn],p[maxn],rnk[maxn];
ll tot[maxn];
bool good[maxn];
vi ans[maxn];
vii EL;
int fp(int i){
return p[i]==i?i:p[i]=fp(p[i]);
}
int join(int x,int y){
x=fp(x),y=fp(y);
if(rnk[x]<rnk[y])p[x]=y;
else p[y]=x;
if(rnk[x]==rnk[y])++rnk[x];
return p[x];
}
int main(){
sf("%d%d",&n,&m);
for(int i=0;i<n;++i){
sf("%d",&s[i]);
ans[i].pb(i);
p[i]=i,rnk[i]=0;
tot[i]=s[i];
}
for(int i=0;i<m;++i){
int a,b;
sf("%d%d",&a,&b);
--a,--b;
if(s[a]>s[b])swap(a,b);
EL.pb({a,b});
}
sort(all(EL),[&](ii &a,ii &b){
return s[a.se]==s[b.se]?s[a.fi]<s[b.fi]:s[a.se]<s[b.se];
});
for(auto[u,v]:EL){
//pf("edge: %d %d\n",u,v);
int pu=fp(u),pv=fp(v);
if(pu==pv)continue;
int p=join(pu,pv);
if(tot[pu]>=s[v]){
vi tmp;
swap(ans[pu],tmp);
if(sz(tmp)<sz(ans[pv]))swap(tmp,ans[pv]);
for(int i:ans[pv])tmp.pb(i);
swap(tmp,ans[p]);
}
else{
if(pu==p)swap(ans[pu],ans[pv]);
}
tot[p]=tot[pu]+tot[pv];
vector<int> clr;
if(pu==p)swap(ans[pv],clr);
else swap(ans[pu],clr);
/*
pf("now:\n");
for(int i=0;i<n;++i){
pf("%d %d: ",i,tot[i]);
for(int x:ans[i])pf("%d ",x);
pf("\n");
}
*/
}
int p=fp(0);
for(int i:ans[p]){
good[i]=true;
}
for(int i=0;i<n;++i)pf("%d",good[i]);
pf("\n");
}
Compilation message
island.cpp: In function 'int main()':
island.cpp:83:4: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
83 | sf("%d%d",&n,&m);
| ^
island.cpp:85:5: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
85 | sf("%d",&s[i]);
| ^
island.cpp:92:5: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
92 | sf("%d%d",&a,&b);
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5036 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
4 ms |
5076 KB |
Output is correct |
5 |
Correct |
6 ms |
5156 KB |
Output is correct |
6 |
Correct |
3 ms |
5076 KB |
Output is correct |
7 |
Correct |
5 ms |
5076 KB |
Output is correct |
8 |
Correct |
4 ms |
5160 KB |
Output is correct |
9 |
Correct |
4 ms |
5076 KB |
Output is correct |
10 |
Correct |
4 ms |
5212 KB |
Output is correct |
11 |
Correct |
4 ms |
5160 KB |
Output is correct |
12 |
Correct |
4 ms |
5160 KB |
Output is correct |
13 |
Correct |
4 ms |
5204 KB |
Output is correct |
14 |
Correct |
4 ms |
5076 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
155 ms |
17296 KB |
Output is correct |
4 |
Correct |
160 ms |
21680 KB |
Output is correct |
5 |
Correct |
185 ms |
21412 KB |
Output is correct |
6 |
Correct |
159 ms |
21560 KB |
Output is correct |
7 |
Correct |
148 ms |
21948 KB |
Output is correct |
8 |
Correct |
189 ms |
24424 KB |
Output is correct |
9 |
Correct |
126 ms |
21400 KB |
Output is correct |
10 |
Correct |
116 ms |
20632 KB |
Output is correct |
11 |
Correct |
148 ms |
22284 KB |
Output is correct |
12 |
Correct |
162 ms |
20108 KB |
Output is correct |
13 |
Correct |
162 ms |
21360 KB |
Output is correct |
14 |
Correct |
154 ms |
21372 KB |
Output is correct |
15 |
Correct |
135 ms |
21728 KB |
Output is correct |
16 |
Correct |
98 ms |
20796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
170 ms |
17532 KB |
Output is correct |
3 |
Correct |
173 ms |
22128 KB |
Output is correct |
4 |
Correct |
168 ms |
23024 KB |
Output is correct |
5 |
Correct |
171 ms |
20948 KB |
Output is correct |
6 |
Correct |
175 ms |
22048 KB |
Output is correct |
7 |
Correct |
130 ms |
21736 KB |
Output is correct |
8 |
Correct |
120 ms |
21616 KB |
Output is correct |
9 |
Correct |
99 ms |
20988 KB |
Output is correct |
10 |
Correct |
126 ms |
20984 KB |
Output is correct |
11 |
Correct |
158 ms |
21516 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
174 ms |
18360 KB |
Output is correct |
3 |
Correct |
182 ms |
22828 KB |
Output is correct |
4 |
Correct |
184 ms |
23436 KB |
Output is correct |
5 |
Correct |
174 ms |
23992 KB |
Output is correct |
6 |
Correct |
192 ms |
23160 KB |
Output is correct |
7 |
Correct |
152 ms |
23332 KB |
Output is correct |
8 |
Correct |
102 ms |
20408 KB |
Output is correct |
9 |
Correct |
108 ms |
14868 KB |
Output is correct |
10 |
Correct |
178 ms |
22840 KB |
Output is correct |
11 |
Correct |
164 ms |
21816 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5036 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
4 ms |
5076 KB |
Output is correct |
5 |
Correct |
6 ms |
5156 KB |
Output is correct |
6 |
Correct |
3 ms |
5076 KB |
Output is correct |
7 |
Correct |
5 ms |
5076 KB |
Output is correct |
8 |
Correct |
4 ms |
5160 KB |
Output is correct |
9 |
Correct |
4 ms |
5076 KB |
Output is correct |
10 |
Correct |
4 ms |
5212 KB |
Output is correct |
11 |
Correct |
4 ms |
5160 KB |
Output is correct |
12 |
Correct |
4 ms |
5160 KB |
Output is correct |
13 |
Correct |
4 ms |
5204 KB |
Output is correct |
14 |
Correct |
4 ms |
5076 KB |
Output is correct |
15 |
Correct |
3 ms |
4948 KB |
Output is correct |
16 |
Correct |
3 ms |
4948 KB |
Output is correct |
17 |
Correct |
155 ms |
17296 KB |
Output is correct |
18 |
Correct |
160 ms |
21680 KB |
Output is correct |
19 |
Correct |
185 ms |
21412 KB |
Output is correct |
20 |
Correct |
159 ms |
21560 KB |
Output is correct |
21 |
Correct |
148 ms |
21948 KB |
Output is correct |
22 |
Correct |
189 ms |
24424 KB |
Output is correct |
23 |
Correct |
126 ms |
21400 KB |
Output is correct |
24 |
Correct |
116 ms |
20632 KB |
Output is correct |
25 |
Correct |
148 ms |
22284 KB |
Output is correct |
26 |
Correct |
162 ms |
20108 KB |
Output is correct |
27 |
Correct |
162 ms |
21360 KB |
Output is correct |
28 |
Correct |
154 ms |
21372 KB |
Output is correct |
29 |
Correct |
135 ms |
21728 KB |
Output is correct |
30 |
Correct |
98 ms |
20796 KB |
Output is correct |
31 |
Correct |
2 ms |
4948 KB |
Output is correct |
32 |
Correct |
170 ms |
17532 KB |
Output is correct |
33 |
Correct |
173 ms |
22128 KB |
Output is correct |
34 |
Correct |
168 ms |
23024 KB |
Output is correct |
35 |
Correct |
171 ms |
20948 KB |
Output is correct |
36 |
Correct |
175 ms |
22048 KB |
Output is correct |
37 |
Correct |
130 ms |
21736 KB |
Output is correct |
38 |
Correct |
120 ms |
21616 KB |
Output is correct |
39 |
Correct |
99 ms |
20988 KB |
Output is correct |
40 |
Correct |
126 ms |
20984 KB |
Output is correct |
41 |
Correct |
158 ms |
21516 KB |
Output is correct |
42 |
Correct |
3 ms |
4948 KB |
Output is correct |
43 |
Correct |
174 ms |
18360 KB |
Output is correct |
44 |
Correct |
182 ms |
22828 KB |
Output is correct |
45 |
Correct |
184 ms |
23436 KB |
Output is correct |
46 |
Correct |
174 ms |
23992 KB |
Output is correct |
47 |
Correct |
192 ms |
23160 KB |
Output is correct |
48 |
Correct |
152 ms |
23332 KB |
Output is correct |
49 |
Correct |
102 ms |
20408 KB |
Output is correct |
50 |
Correct |
108 ms |
14868 KB |
Output is correct |
51 |
Correct |
178 ms |
22840 KB |
Output is correct |
52 |
Correct |
164 ms |
21816 KB |
Output is correct |
53 |
Correct |
3 ms |
4996 KB |
Output is correct |
54 |
Correct |
3 ms |
5016 KB |
Output is correct |
55 |
Correct |
3 ms |
4948 KB |
Output is correct |
56 |
Correct |
4 ms |
5076 KB |
Output is correct |
57 |
Correct |
4 ms |
5204 KB |
Output is correct |
58 |
Correct |
4 ms |
5156 KB |
Output is correct |
59 |
Correct |
4 ms |
5076 KB |
Output is correct |
60 |
Correct |
5 ms |
5120 KB |
Output is correct |
61 |
Correct |
3 ms |
5076 KB |
Output is correct |
62 |
Correct |
4 ms |
5204 KB |
Output is correct |
63 |
Correct |
3 ms |
5156 KB |
Output is correct |
64 |
Correct |
4 ms |
5132 KB |
Output is correct |
65 |
Correct |
3 ms |
5204 KB |
Output is correct |
66 |
Correct |
4 ms |
5076 KB |
Output is correct |
67 |
Correct |
132 ms |
21748 KB |
Output is correct |
68 |
Correct |
157 ms |
21632 KB |
Output is correct |
69 |
Correct |
162 ms |
21540 KB |
Output is correct |
70 |
Correct |
157 ms |
21508 KB |
Output is correct |
71 |
Correct |
151 ms |
22024 KB |
Output is correct |
72 |
Correct |
182 ms |
24320 KB |
Output is correct |
73 |
Correct |
130 ms |
21424 KB |
Output is correct |
74 |
Correct |
131 ms |
20608 KB |
Output is correct |
75 |
Correct |
158 ms |
22404 KB |
Output is correct |
76 |
Correct |
159 ms |
20088 KB |
Output is correct |
77 |
Correct |
193 ms |
21428 KB |
Output is correct |
78 |
Correct |
161 ms |
21348 KB |
Output is correct |
79 |
Correct |
123 ms |
21656 KB |
Output is correct |
80 |
Correct |
101 ms |
20908 KB |
Output is correct |
81 |
Correct |
175 ms |
22008 KB |
Output is correct |
82 |
Correct |
174 ms |
22024 KB |
Output is correct |
83 |
Correct |
181 ms |
23120 KB |
Output is correct |
84 |
Correct |
182 ms |
21008 KB |
Output is correct |
85 |
Correct |
176 ms |
22088 KB |
Output is correct |
86 |
Correct |
126 ms |
21672 KB |
Output is correct |
87 |
Correct |
121 ms |
21740 KB |
Output is correct |
88 |
Correct |
125 ms |
20976 KB |
Output is correct |
89 |
Correct |
158 ms |
21596 KB |
Output is correct |
90 |
Correct |
192 ms |
22716 KB |
Output is correct |
91 |
Correct |
173 ms |
22828 KB |
Output is correct |
92 |
Correct |
184 ms |
23384 KB |
Output is correct |
93 |
Correct |
173 ms |
24012 KB |
Output is correct |
94 |
Correct |
179 ms |
22860 KB |
Output is correct |
95 |
Correct |
185 ms |
23268 KB |
Output is correct |
96 |
Correct |
123 ms |
20424 KB |
Output is correct |
97 |
Correct |
93 ms |
14800 KB |
Output is correct |
98 |
Correct |
161 ms |
22776 KB |
Output is correct |
99 |
Correct |
166 ms |
21772 KB |
Output is correct |
100 |
Correct |
64 ms |
8756 KB |
Output is correct |
101 |
Correct |
177 ms |
23180 KB |
Output is correct |
102 |
Correct |
142 ms |
20560 KB |
Output is correct |
103 |
Correct |
151 ms |
20388 KB |
Output is correct |
104 |
Correct |
175 ms |
22540 KB |
Output is correct |