#include <bits/stdc++.h>
using namespace std;
#define int long long
vector<int>link[200100];
vector<pair<int,int>>x;
int uf[200100];
int sum[200100];
int mi[200100];
int cnt[200100];
int f(int n)
{
if(uf[n]==n)
return n;
return uf[n]=f(uf[n]);
}
void u(int n,int m)
{
n=f(n);
m=f(m);
if(n==m)
return;
sum[n]+=sum[m];
if(mi[n]==mi[m])
cnt[n]+=cnt[m];
else if(mi[n]>mi[m])
{
mi[n]=mi[m];
cnt[n]=cnt[m];
}
uf[m]=n;
}
vector<int>carry;
int arr[200100];
int ans[200100];
signed main()
{
int N,M;
cin >>N>>M;
int i;
for(i=0;i<N;i++)
cin >>arr[i+1];
for(i=0;i<M;i++)
{
int a,b;
cin >>a>>b;
link[a].push_back(b);
link[b].push_back(a);
x.push_back({a,b});
}
int summ=0;
vector<pair<int,int>>t;
for(i=0;i<N;i++)
{
summ+=arr[i+1];
t.push_back({arr[i+1],i+1});
carry.push_back(i+1);
}
sort(t.begin(),t.end());
for(i=0;i<32;i++)
{
int ve=(1LL<<i);
int i;
memset(uf,-1,sizeof(uf));
memset(sum,0,sizeof(sum));
memset(mi,10,sizeof(mi));
memset(cnt,0,sizeof(cnt));
for(i=0;i<N;i++)
{
if(t[i].first<ve)
{
uf[t[i].second]=t[i].second;
sum[t[i].second]=t[i].first;
int j;
for(j=0;j<link[t[i].second].size();j++)
{
if(uf[link[t[i].second][j]]>=0)
{
u(link[t[i].second][j],t[i].second);
}
}
}
else
{
unordered_map<int,int>maaa;
int j;
for(j=0;j<link[t[i].second].size();j++)
{
if(uf[link[t[i].second][j]]>=0)
{
if(maaa[f(link[t[i].second][j])])
continue;
maaa[f(link[t[i].second][j])]=1;
if(mi[f(link[t[i].second][j])]==t[i].first)
cnt[f(link[t[i].second][j])]++;
else if(mi[f(link[t[i].second][j])]>t[i].first)
{
mi[f(link[t[i].second][j])]=t[i].first;
cnt[f(link[t[i].second][j])]=1;
}
}
}
}
}
vector<int>nc;
for(i=0;i<carry.size();i++)
{
if(arr[carry[i]]<ve)
{
int v=sum[f(carry[i])];
if(mi[f(carry[i])]<=v)
v+=mi[f(carry[i])];
if(v>=summ)
ans[carry[i]]=1;
else if(v>=2*ve)
{
nc.push_back(carry[i]);
}
}
else
{
int v=arr[carry[i]];
int ma=1LL<<60;
int j;
vector<int>arrr;
for(j=0;j<link[carry[i]].size();j++)
{
if(uf[link[carry[i]][j]]>=0)
arrr.push_back(f(link[carry[i]][j]));
else
arrr.push_back(link[carry[i]][j]);
}
sort(arrr.begin(),arrr.end());
arrr.erase(unique(arrr.begin(),arrr.end()),arrr.end());
for(j=0;j<arrr.size();j++)
{
if(uf[arrr[j]]>=0)
{
if(mi[arrr[j]]!=arr[carry[i]]||cnt[arrr[j]]!=1)
ma=min(ma,mi[arrr[j]]);
v+=sum[arrr[j]];
}
else
{
ma=min(ma,arr[arrr[j]]);
}
}
if(ma<=v)
v+=ma;
if(v>=summ)
ans[carry[i]]=1;
else if(v>=2*ve)
{
nc.push_back(carry[i]);
}
}
}
carry=nc;
}
for(i=1;i<=N;i++)
cout <<ans[i];
}
Compilation message
island.cpp: In function 'int main()':
island.cpp:74:26: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
74 | for(j=0;j<link[t[i].second].size();j++)
| ~^~~~~~~~~~~~~~~~~~~~~~~~~
island.cpp:86:26: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
86 | for(j=0;j<link[t[i].second].size();j++)
| ~^~~~~~~~~~~~~~~~~~~~~~~~~
island.cpp:105:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
105 | for(i=0;i<carry.size();i++)
| ~^~~~~~~~~~~~~
island.cpp:125:26: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
125 | for(j=0;j<link[carry[i]].size();j++)
| ~^~~~~~~~~~~~~~~~~~~~~~
island.cpp:134:26: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
134 | for(j=0;j<arrr.size();j++)
| ~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
11220 KB |
Output is correct |
2 |
Correct |
17 ms |
11276 KB |
Output is correct |
3 |
Correct |
16 ms |
11276 KB |
Output is correct |
4 |
Incorrect |
30 ms |
11468 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
11276 KB |
Output is correct |
2 |
Correct |
19 ms |
11220 KB |
Output is correct |
3 |
Execution timed out |
1076 ms |
33080 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
11220 KB |
Output is correct |
2 |
Execution timed out |
1078 ms |
31940 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
11220 KB |
Output is correct |
2 |
Execution timed out |
1096 ms |
31912 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
11220 KB |
Output is correct |
2 |
Correct |
17 ms |
11276 KB |
Output is correct |
3 |
Correct |
16 ms |
11276 KB |
Output is correct |
4 |
Incorrect |
30 ms |
11468 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |