This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
#define int long long
#define pb push_back
#define fore(i , n) for(int i = 0 ; i<n;i++)
#define forr(i , x , y) for(int i = x ; i <= y; i++)
#define forn(i , x , y) for(int i = x ; i >= y; i--)
const int N = 2e5 + 10;
vector<int> adj[N];
int a[N];
int n , m;
vector<bool> vis(N , 0);
vector<int> ans(N , -1);
priority_queue<pair<int ,int> , vector<pair<int ,int>> , greater<pair<int , int>>> pq;
vector<int> possible;
vector<int> nodes;
void expand(int x)
{
pq = priority_queue<pair<int ,int> , vector<pair<int ,int>> , greater<pair<int , int>>>();
int cur = a[x];
pq.push({cur , x});
possible.clear();
nodes.clear();
while(!pq.empty())
{
auto [val , node] = pq.top();
pq.pop();
if(val == cur)
{
possible.pb(node);
if(ans[node] == 0)
{
for(auto v : nodes)
vis[v] = 0;
ans[x] = 0;
return ;
}
}
if(val > cur)
break;
if(vis[node])
continue;
vis[node] = 1;
nodes.pb(node);
if(node != x)
cur+=val;
if(ans[node] == 1)
{
ans[x] = 1;
for(auto v : nodes)
vis[v] = 0;
return;
}
for(auto u : adj[node])
{
pq.push({a[u] , u});
}
}
bool acc = 1;
if((int)nodes.size() != n)
acc = 0;
for(auto u : nodes)
vis[u] = 0;
if(acc)
{
ans[x] = 1;
for(auto u : possible)
ans[u] = 1;
}
else
{
for(auto p : nodes)
{
ans[p] = 0;
}
ans[x] = 0;
}
}
signed main()
{
// n = 2e5 , m = n - 1;
cin>>n>>m;
vector<pair<int ,int>> ve;
// int f[10] = {1 , 2 , 5 , 10 , 20 , 6 , 7 , 18 , 40 , 31};
fore(i , n)
{
// a[i] = f[rand()%10];
cin>>a[i];
ve.pb({a[i] , i});
}
sort(ve.begin() , ve.end());
fore(i , m)
{
int u , v;
// u = i + 1 ;
// v = i + 2;
cin>>u>>v;
u-- , v--;
adj[u].pb(v);
adj[v].pb(u);
}
// int cnt = 0;
for(auto [val , i] : ve)
{
if(ans[i] == -1)
{
expand(i);
}
}
fore(i , n)
{
cout<<ans[i];
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |