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>
using namespace std;
using ll = long long;
struct ufds{
vector<int> par;
ufds(int n): par(n){
iota(par.begin(), par.end(), 0);
}
int find(int a){
return a == par[a] ? a : par[a] = find(par[a]);
}
int unite(int a,int b){
a = find(a), b = find(b);
if(a == b) return 0;
par[b] = a;
return 1;
}
};
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int n,m;
cin >> n >> m;
vector<int> s(n);
for(int &x: s){
cin >> x;
}
vector<vector<int>> g(n);
while(m--){
int u,v;
cin >> u >> v,u--,v--;
g[u].push_back(v);
g[v].push_back(u);
}
vector<int> ok(n);
vector<pair<int,int>> srt;
for(int i = 0; i < n; i++){
srt.emplace_back(s[i],i);
}
sort(srt.begin(), srt.end());
vector<vector<int>> tree(n);
ufds ds(n);
for(auto [x,v]: srt){
for(int u: g[v]) if(ok[u]){
if(ok[u] && ds.find(u) != ds.find(v)){
tree[v].push_back(ds.find(u));
ds.unite(v,u);
}
}
ok[v] = 1;
}
vector<ll> sums;
for(auto x: s){
sums.push_back(x);
}
vector<int> ans(n);
function <void(int)> dfs = [&](int v){
ans[v] = 1;
for(int u: tree[v]){
dfs(u);
ans[u] &= s[v] <= sums[u];
sums[v] += sums[u];
}
};
function <void(int)> dfs1 = [&](int v){
for(int u: tree[v]){
ans[u] &= ans[v];
dfs1(u);
}
};
dfs(ds.find(0));
dfs1(ds.find(0));
for(int x: ans){
cout << x;
}
}
# | 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... |