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;
vector<vector<int>>g;
vector<int>num;
vector<long long>cnt1;
vector<int>canTraverse; /// 0-unchecked, 1-false, 2-true
void calc(int u, int par){
cnt1[u] = num[u];
for(int v : g[u]){
if(v != par){
calc(v, u);
cnt1[u] += cnt1[v];
}
}
}
void dfs1(int u, int par){
if(canTraverse[par] == 1)canTraverse[u] = 1;
else{
canTraverse[u] = (cnt1[u] >= num[par]) + 1;
}
for(int v : g[u]){
if(v != par){
dfs1(v, u);
}
}
}
vector<bool>vis, chosen;
bool cmp(int a, int b){
return num[a] < num[b];
}
long long cnt = 0;
vector<int>tmp;
void dfs(int u){
vis[u] = 1;
// cout << u << " ";
vector<int>tempo;
for(int v : g[u]){
if(!chosen[v] && cnt >= num[v]){
cnt += num[v];
chosen[v] = 1;
tempo.push_back(v);
}
else if(cnt < num[v]){
tmp.push_back(v);
}
}
for(int v : tempo){
if(!vis[v]){
dfs(v);
}
}
}
int main()
{
int n, m;
cin >> n >> m;
if(n <= 2000 && m <= 2000){
g.resize(n + 1);
num.resize(n + 1);
vis.resize(n + 1);
chosen.resize(n + 1);
for(int i = 1;i <= n;++i)cin >> num[i];
for(int i = 0;i < m;++i){
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
for(int i = 1;i <= n;++i){
sort(g[i].begin(), g[i].end(), cmp);
}
// for(int i = 1;i <= n;++i){
// cout << i << ": ";
// for(int j : g[i]){
// cout << j << " ";
// }
// cout << '\n';
// }
string s = "";
for(int i = 1;i <= n;++i){
// cout << i << ": ";
cnt = num[i];
chosen[i] = 1;
dfs(i);
sort(tmp.begin(), tmp.end(), cmp);
int ind = 0;
while(tmp.size() && ind < tmp.size()){
int j = tmp[ind], sz = tmp.size();
if(!chosen[j] && cnt >= num[j]){
chosen[j] = 1;
cnt += num[j];
dfs(j);
}
else{
ind++;
continue;
}
sort(tmp.begin(), tmp.end(), cmp);
ind = 0;
}
// cout << "CNT: " << cnt << '\n';
bool ok = 1;
for(int j = 1;j <= n && ok;++j){
if(!vis[j]){
ok = 0;
}
}
for(int j = 1;j <= n;++j){
vis[j] = 0;
chosen[j] = 0;
}
if(ok)s += '1';
else s += '0';
tmp.clear();
}
cout << s;
}
else{
g.resize(n + 1);
num.resize(n + 1);
cnt1.resize(n + 1);
canTraverse.resize(n + 1);
for(int i = 1;i <= n;++i){
cin >> num[i];
}
for(int i = 0;i < m;++i){
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
calc(1, 1);
dfs1(1, 1);
for(int i = 1;i <= n;++i){
if(canTraverse[i] == 2)cout << 1;
else cout << 0;
}
}
}
Compilation message (stderr)
island.cpp: In function 'int main()':
island.cpp:87:37: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
87 | while(tmp.size() && ind < tmp.size()){
| ~~~~^~~~~~~~~~~~
island.cpp:88:35: warning: unused variable 'sz' [-Wunused-variable]
88 | int j = tmp[ind], sz = tmp.size();
| ^~
# | 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... |