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>
#define pb push_back
#define f first
#define sc second
using namespace std;
typedef long long int ll;
typedef string str;
int n, k;
vector<vector<int>> tree;
vector<int> sz;
vector<int> state, scnt;
bool separable = 0;
void add(int nd, int ss, vector<int> &cnt){
for(int x: tree[nd]) if(x != ss) add(x, nd, cnt);
cnt[state[nd]]++;
}
void dfs(int nd, int ss){
for(int x: tree[nd]) if(x != ss) dfs(x, nd);
if(ss == -1) return;
vector<int> cnt(k, 0);
add(nd, ss, cnt);
bool ok = 1;
for(int i = 0; i < k; i++) ok&=(cnt[i] == 0 || cnt[i] == scnt[i]);
separable|=ok;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> k;
tree.resize(n);
state.resize(n);
scnt.resize(k, 0);
for(int i = 0; i < n-1; i++){
int a, b; cin >> a >> b; a--, b--;
tree[a].push_back(b);
tree[b].push_back(a);
}
for(int i = 0; i < n; i++){
cin >> state[i]; state[i]--;
scnt[state[i]]++;
}
dfs(0, -1);
cout << separable << "\n";
}
# | 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... |