Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- #define pb push_back
- using namespace std;
- vector<vector<int> > sp;
- vector<int> col;
- int INF = 1e9;
- int d = INF;
- bool dfs(int v, int p){
- if(p != 0){
- if(col[v] != col[p]){
- d = p;
- return 0;
- }
- }
- for(auto i : sp[v]){
- if(i != p) dfs(i, v);
- }
- }
- int dfs1(int v, int p){
- cout << "vxod ";
- if(p != d){
- if(col[v] != col[p]){
- cout << "badaBOOM " << v + 1 << ' ' << col[v] << ' ' << p + 1 << ' ' << col[p] << ' ';
- return 0;
- }
- }
- cout << "!!!!!! ";
- for(int i = 0; i < sp[v].size(); i++){
- int to = sp[v][i];
- cout << v + 1 << ' ' << p + 1 << ' ' << to + 1 << '\n';
- if(to != p){
- dfs1(to, v);
- }
- }
- }
- /*
- 4
- 1 2
- 2 3
- 3 4
- 1 2 1 1
- */
- int main(){
- int n;
- cin >> n;
- sp.resize(n);
- col.resize(n);
- for(int i = 0; i < n - 1; i++){
- int a, b;
- cin >> a >> b;
- a--, b--;
- sp[a].pb(b);
- sp[b].pb(a);
- }
- for(int i = 0; i < n; i++){
- cin >> col[i];
- }
- dfs(0, 0);
- //cout << d << endl;
- if(d == INF){
- cout << "YES" << endl;
- cout << 1;
- }
- else{
- bool ch = dfs1(d, d);
- if(!ch){
- cout << "NO";
- }
- else {
- cout << "YES" << endl;
- cout << d;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement