#include <bits/stdc++.h>
#define int long long
using namespace std;
int n,m;
int s[200005];
vector<pair<int,int>> e;
bool ok[200005];
int t[200005],sum[200005];
vector<int> l[200005];
int reprezentant(int nod)
{
if(t[nod]==nod)
{
return nod;
}
return reprezentant(t[nod]);
}
void uneste(int x, int y)
{
x = reprezentant(x);
y = reprezentant(y);
if(x==y)
{
return;
}
if(l[x].size() > l[y].size())
{
sum[x] += sum[y];
t[y] = x;
for(auto it : l[y])
{
l[x].push_back(it);
}
}
else
{
sum[y] += sum[x];
t[x] = y;
for(auto it : l[x])
{
l[y].push_back(it);
}
}
}
bool cmp(pair<int,int> a, pair<int,int> b)
{
return (s[a.second] < s[b.second] || (s[a.second]==s[b.second] && s[a.first] < s[b.first]));
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin>>n>>m;
for(int i=1; i<=n; i++)
{
cin>>s[i];
}
for(int i=1; i<=m; i++)
{
int x,y;
cin>>x>>y;
if(s[x]<s[y])
{
e.push_back({x,y});
}
else
{
e.push_back({y,x});
}
}
for(int i=1; i<=n; i++)
{
t[i] = i;
l[i].push_back(i);
sum[i] = s[i];
ok[i] = true;
}
sort(e.begin(),e.end(),cmp);
for(auto it : e)
{
int x = it.first;
int y = it.second;
if(sum[reprezentant(x)] >= s[y])
{
uneste(x,y);
}
else
{
for(auto it2 : l[reprezentant(x)])
{
ok[it2] = false;
}
}
}
for(int i=1; i<=n; i++)
{
cout<<ok[i];
}
return 0;
}
# |
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 |
3 ms |
4948 KB |
Output is correct |
4 |
Incorrect |
3 ms |
5176 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Incorrect |
134 ms |
21056 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Incorrect |
140 ms |
19404 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Incorrect |
139 ms |
19912 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
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 |
3 ms |
4948 KB |
Output is correct |
4 |
Incorrect |
3 ms |
5176 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |