Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* _
- _oo0oo_
- o8888888o
- 88" . "88
- (| -_- |)
- 0\ = /0
- ____/`---'\____
- / \\| |// \
- / \\||| /:\ |||// \
- / _||||| -:- |||||- \
- | | \\\ \-/ /// | |
- | \_| ''\---/'' |_/ |
- \ .-\__ '-' ___/-. /
- ____'. .' /--.--\ `. .'_____
- /"" '< `.___\_<|>_/___.' >' "" \
- | | : `- \`.;`\ _ /`;.`/ - ` : | |
- \ \ `_. \_ __\ /__ _/ .-` / /
- `-.____`.___ \_____/___.-`___.-'
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- ash_1_e IIT Patna
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
- #include<bits/stdc++.h>
- using namespace std;
- #define ll long long int
- #define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
- #define FRE freopen("input.txt","r",stdin); freopen("output.txt","w",stdout);
- #define f(i,n) for(ll i=0;i<n;i++)
- #define fa(i,a,n) for(ll i=a;a<n?i<n:i>n;a<n?i+=1:i-=1)
- #define pb push_back
- #define F first
- #define S second
- #define all(x) x.begin(), x.end()
- #define clr(x) memset(x, 0, sizeof(x))
- #define sortall(x) sort(all(x))
- #define PI 3.1415926535897932384626
- #define MOD 1000000007
- typedef pair<int, int> pii;
- typedef pair<ll, ll> pll;
- typedef vector<int> vi;
- typedef vector<ll> vl;
- typedef vector<pii> vpii;
- typedef vector<pll> vpll;
- typedef vector<vi> vvi;
- typedef vector<vl> vvl;
- const ll mod = 1000000007;
- const ll N = 3e5, M = N, ninf = -2e5;
- vl g[N];
- ll a[N], dp[N], in[N], out[N];
- ll n;
- bool ok;
- ll curr_color;
- bool dfs(ll u, ll par){
- ok = ok && (a[u] == curr_color);
- for(ll v: g[u]) {
- if (v == par) continue;
- dfs(v, u);
- }
- }
- bool solve(ll u){
- ll ans=true;
- for(ll v: g[u]){
- curr_color=a[v];
- ok=true;
- dfs(v, u);
- ans=ans && ok;
- }
- return ans;
- }
- int32_t main()
- {
- IOS
- ll t,m,k,x,y,z,p,q,u,v,ct=0,flag=0,d;
- cin>>n;
- f(i,n-1) cin>>u>>v, g[u].pb(v), g[v].pb(u);
- fa(i,1,n+1) cin>>a[i];
- ll root1=-1;
- ll root2=-1;
- fa(i,1,n+1){
- for(ll ele: g[i]){
- if(a[ele]!=a[i]){
- root1=ele;
- root2=i;
- break;
- }
- }
- }
- if(root1==-1){
- cout<<"YES\n1";
- return 0;
- }
- bool res1=solve(root1);
- bool res2=solve(root2);
- if(res1){
- cout<<"YES\n"<<root1;
- }else if(res2){
- cout<<"YES\n"<<root2;
- }else{
- cout<<"NO";
- }
- }
Add Comment
Please, Sign In to add comment