#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#define pb push_back

using namespace std;
struct lit
{
	int x, y;
};
struct qnode
{
	int ind, lite, dist;
};
queue< qnode > q;
vector<int> row[10005], col[10005];
lit arr[10005];
int n, m, k, strt, depth[10005][7], inf=1e9;
bool visited[10005][7], actvis[10005][7];
void pusher(qnode child)
{
	if(!visited[child.ind][child.lite] && (child.ind!=k || child.lite==2 || child.lite==5))
	{
		q.push(child);
		visited[child.ind][child.lite]=1;
	}
}
void bfs(int node, int light)
{
	//cout << node << " " << light << " - " << depth[node][light] <<endl;
	actvis[node][light]=true;
	int x = arr[node].x; int y = arr[node].y;
	int d=depth[node][light];
	qnode child;
	child.dist=d+1;
	for(int i = 0; i < row[x-2].size(); i++)
	{
		child.ind=row[x-2][i];
		child.lite=3;	pusher(child);
	}
	for(int i = 0; i < row[x-1].size(); i++)
	{
		child.ind=row[x-1][i];
		child.lite=2;	pusher(child);
		child.lite=3;	pusher(child);
	}
	for(int i = 0; i < row[x].size(); i++)
	{
		child.ind=row[x][i];
		child.lite=1;	pusher(child);
		child.lite=2;	pusher(child);
		child.lite=3;	pusher(child);
	}
	for(int i = 0; i < row[x+1].size(); i++)
	{
		child.ind=row[x+1][i];
		child.lite=2;	pusher(child);
		child.lite=1;	pusher(child);
	}
	for(int i = 0; i < row[x+2].size(); i++)
	{
		child.ind=row[x+2][i];
		child.lite=1;	pusher(child);
	}
	for(int i = 0; i < col[y-2].size(); i++)
	{
		child.ind=col[y-2][i];
		child.lite=6;	pusher(child);
	}
	for(int i = 0; i < col[y-1].size(); i++)
	{
		child.ind=col[y-1][i];
		child.lite=5;	pusher(child);
		child.lite=6;	pusher(child);
	}
	for(int i = 0; i < col[y].size(); i++)
	{
		child.ind=col[y][i];
		child.lite=4;	pusher(child);
		child.lite=5;	pusher(child);
		child.lite=6;	pusher(child);
	}
	for(int i = 0; i < col[y+1].size(); i++)
	{
		child.ind=col[y+1][i];
		child.lite=4;	pusher(child);
		child.lite=5;	pusher(child);
	}
	for(int i = 0; i < col[y+2].size(); i++)
	{
		child.ind=col[y+2][i];
		child.lite=4;	pusher(child);
	}
	for(int i = 0; i < col[y].size(); i++)
	{
		child.ind=col[y][i];
		if(arr[child.ind].x==x-1)
		{
			if(light==1) child.lite=2;
			else if(light==2) child.lite=3;
			else if(light==3) child.lite=0;
			else child.lite=light;
			if(!actvis[child.ind][child.lite])
			{
				depth[child.ind][child.lite]=d;
				bfs(child.ind, child.lite);
			}
		}
		else if(arr[child.ind].x==x+1)
		{
			if(light==1) child.lite=0;
			else if(light==2) child.lite=1;
			else if(light==3) child.lite=2;
			else child.lite=light;
			if(!actvis[child.ind][child.lite])
			{
				depth[child.ind][child.lite]=d;
				bfs(child.ind, child.lite);
			}
		}
	}
	for(int i = 0; i < row[x].size(); i++)
	{
		child.ind=row[x][i];
		if(arr[child.ind].y==y-1)
		{
			if(light==4) child.lite=5;
			else if(light==5) child.lite=6;
			else if(light==6) child.lite=0;
			else child.lite=light;
			if(!actvis[child.ind][child.lite])
			{
				depth[child.ind][child.lite]=d;
				bfs(child.ind, child.lite);
			}
		}
		else if(arr[child.ind].y==y+1)
		{
			if(light==4) child.lite=0;
			else if(light==5) child.lite=4;
			else if(light==6) child.lite=5;
			else child.lite=light;
			if(!actvis[child.ind][child.lite])
			{
				depth[child.ind][child.lite]=d;
				bfs(child.ind, child.lite);
			}
		}
	}
	while(q.size()>0)
	{
		child=q.front();
		q.pop();
		if(!actvis[child.ind][child.lite])
		{
			depth[child.ind][child.lite]=child.dist;
			bfs(child.ind, child.lite);
		}
	}
}
int main()
{
	cin >> n >> m >> k;
	bool endlit=false;
	for(int i = 0; i < k; i++)
	{
		cin >> arr[i].x >> arr[i].y;
		if(arr[i].x==1 && arr[i].y==1) strt=i;
		else if(arr[i].x==n && arr[i].y==m) endlit=true;
		row[arr[i].x].pb(i);
		col[arr[i].y].pb(i);
	}
	for(int i = 0; i <= k; i++)
	{
		for(int j = 0; j < 7; j++)
		{
			depth[i][j]=-1;
		}
	}
	arr[k].x=n; arr[k].y=m;
	row[n].pb(k);
	col[m].pb(k);
	depth[strt][0]=0;
	visited[strt][0]=1;
	bfs(strt, 0);
	int mn=inf;
	if(endlit)
	{
		for(int i = 0; i < 7; i++)
		{
			if(depth[k][i]<mn && depth[k][i]!=-1)
			{
				mn=depth[k][i];
			}
		}
	}
	if(mn!=inf)
	{
		cout << mn << endl;
		return 0;
	}
	if(depth[k][2]==-1)
	{
		cout << depth[k][5];
	}
	else if(depth[k][5])
	{
		cout << depth[k][2];
	}
	else cout << min(depth[k][2], depth[k][5]);
}


#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#define pb push_back

using namespace std;
struct lit
{
	int x, y;
};
struct qnode
{
	int ind, lite, dist;
};
queue< qnode > q;
vector<int> row[10005], col[10005];
lit arr[10005];
int n, m, k, strt, depth[10005][7], inf=1e9;
bool visited[10005][7], actvis[10005][7];
void pusher(qnode child)
{
	if(!visited[child.ind][child.lite] && (child.ind!=k || child.lite==2 || child.lite==5))
	{
		q.push(child);
		visited[child.ind][child.lite]=1;
	}
}
void bfs(int node, int light)
{
	//cout << node << " " << light << " - " << depth[node][light] <<endl;
	actvis[node][light]=true;
	int x = arr[node].x; int y = arr[node].y;
	int d=depth[node][light];
	qnode child;
	child.dist=d+1;
	if(x!=1)
	{
		for(int i = 0; i < row[x-2].size(); i++)
		{
			child.ind=row[x-2][i];
			child.lite=3;	pusher(child);
		}
	}
	for(int i = 0; i < row[x-1].size(); i++)
	{
		child.ind=row[x-1][i];
		child.lite=2;	pusher(child);
		child.lite=3;	pusher(child);
	}
	for(int i = 0; i < row[x].size(); i++)
	{
		child.ind=row[x][i];
		child.lite=1;	pusher(child);
		child.lite=2;	pusher(child);
		child.lite=3;	pusher(child);
	}
	for(int i = 0; i < row[x+1].size(); i++)
	{
		child.ind=row[x+1][i];
		child.lite=2;	pusher(child);
		child.lite=1;	pusher(child);
	}
	for(int i = 0; i < row[x+2].size(); i++)
	{
		child.ind=row[x+2][i];
		child.lite=1;	pusher(child);
	}
	if(y!=1)
	{
		for(int i = 0; i < col[y-2].size(); i++)
		{
			child.ind=col[y-2][i];
			child.lite=6;	pusher(child);
		}
	}
	for(int i = 0; i < col[y-1].size(); i++)
	{
		child.ind=col[y-1][i];
		child.lite=5;	pusher(child);
		child.lite=6;	pusher(child);
	}
	for(int i = 0; i < col[y].size(); i++)
	{
		child.ind=col[y][i];
		child.lite=4;	pusher(child);
		child.lite=5;	pusher(child);
		child.lite=6;	pusher(child);
	}
	for(int i = 0; i < col[y+1].size(); i++)
	{
		child.ind=col[y+1][i];
		child.lite=4;	pusher(child);
		child.lite=5;	pusher(child);
	}
	for(int i = 0; i < col[y+2].size(); i++)
	{
		child.ind=col[y+2][i];
		child.lite=4;	pusher(child);
	}
	for(int i = 0; i < col[y].size(); i++)
	{
		child.ind=col[y][i];
		if(arr[child.ind].x==x-1)
		{
			if(light==1) child.lite=2;
			else if(light==2) child.lite=3;
			else if(light==3) child.lite=0;
			else child.lite=light;
			if(!actvis[child.ind][child.lite])
			{
				depth[child.ind][child.lite]=d;
				bfs(child.ind, child.lite);
			}
		}
		else if(arr[child.ind].x==x+1)
		{
			if(light==1) child.lite=0;
			else if(light==2) child.lite=1;
			else if(light==3) child.lite=2;
			else child.lite=light;
			if(!actvis[child.ind][child.lite])
			{
				depth[child.ind][child.lite]=d;
				bfs(child.ind, child.lite);
			}
		}
	}
	for(int i = 0; i < row[x].size(); i++)
	{
		child.ind=row[x][i];
		if(arr[child.ind].y==y-1)
		{
			if(light==4) child.lite=5;
			else if(light==5) child.lite=6;
			else if(light==6) child.lite=0;
			else child.lite=light;
			if(!actvis[child.ind][child.lite])
			{
				depth[child.ind][child.lite]=d;
				bfs(child.ind, child.lite);
			}
		}
		else if(arr[child.ind].y==y+1)
		{
			if(light==4) child.lite=0;
			else if(light==5) child.lite=4;
			else if(light==6) child.lite=5;
			else child.lite=light;
			if(!actvis[child.ind][child.lite])
			{
				depth[child.ind][child.lite]=d;
				bfs(child.ind, child.lite);
			}
		}
	}
	while(q.size()>0)
	{
		child=q.front();
		q.pop();
		if(!actvis[child.ind][child.lite])
		{
			depth[child.ind][child.lite]=child.dist;
			bfs(child.ind, child.lite);
		}
	}
}
int main()
{
	cin >> n >> m >> k;
	bool endlit=false;
	for(int i = 0; i < k; i++)
	{
		cin >> arr[i].x >> arr[i].y;
		if(arr[i].x==1 && arr[i].y==1) strt=i;
		else if(arr[i].x==n && arr[i].y==m) endlit=true;
		row[arr[i].x].pb(i);
		col[arr[i].y].pb(i);
	}
	for(int i = 0; i <= k; i++)
	{
		for(int j = 0; j < 7; j++)
		{
			depth[i][j]=-1;
		}
	}
	arr[k].x=n; arr[k].y=m;
	row[n].pb(k);
	col[m].pb(k);
	depth[strt][0]=0;
	visited[strt][0]=1;
	bfs(strt, 0);
	int mn=inf;
	if(endlit)
	{
		for(int i = 0; i < 7; i++)
		{
			if(depth[k][i]<mn && depth[k][i]!=-1)
			{
				mn=depth[k][i];
			}
		}
	}
	if(mn!=inf)
	{
		cout << mn << endl;
		return 0;
	}
	if(depth[k][2]==-1)
	{
		cout << depth[k][5];
	}
	else if(depth[k][5]==-1)
	{
		cout << depth[k][2];
	}
	else cout << min(depth[k][2], depth[k][5]);
}


#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#include<deque>
#define pb push_back
#define pf push_front

using namespace std;
struct lit
{
	int x, y;
};
struct qnode
{
	int ind, lite, dist;
};
deque< qnode > q;
vector<int> row[10005], col[10005];
lit arr[10005];
int n, m, k, strt, depth[10005][7], inf=1e9;
bool visited[10005][7], actvis[10005][7];
void pusher(qnode child)
{
	if(!visited[child.ind][child.lite] && (child.ind!=k || child.lite==2 || child.lite==5))
	{
		q.pb(child);
		visited[child.ind][child.lite]=1;
	}
}
void bfs(int node, int light)
{
	//cout << node << " " << light << " - " << depth[node][light] <<endl;
	actvis[node][light]=true;
	int x = arr[node].x; int y = arr[node].y;
	int d=depth[node][light];
	qnode child;
	child.dist=d+1;
	if(x!=1)
	{
		for(int i = 0; i < row[x-2].size(); i++)
		{
			child.ind=row[x-2][i];
			child.lite=3;	pusher(child);
		}
	}
	for(int i = 0; i < row[x-1].size(); i++)
	{
		child.ind=row[x-1][i];
		child.lite=2;	pusher(child);
		child.lite=3;	pusher(child);
	}
	for(int i = 0; i < row[x].size(); i++)
	{
		child.ind=row[x][i];
		child.lite=1;	pusher(child);
		child.lite=2;	pusher(child);
		child.lite=3;	pusher(child);
	}
	for(int i = 0; i < row[x+1].size(); i++)
	{
		child.ind=row[x+1][i];
		child.lite=2;	pusher(child);
		child.lite=1;	pusher(child);
	}
	for(int i = 0; i < row[x+2].size(); i++)
	{
		child.ind=row[x+2][i];
		child.lite=1;	pusher(child);
	}
	if(y!=1)
	{
		for(int i = 0; i < col[y-2].size(); i++)
		{
			child.ind=col[y-2][i];
			child.lite=6;	pusher(child);
		}
	}
	for(int i = 0; i < col[y-1].size(); i++)
	{
		child.ind=col[y-1][i];
		child.lite=5;	pusher(child);
		child.lite=6;	pusher(child);
	}
	for(int i = 0; i < col[y].size(); i++)
	{
		child.ind=col[y][i];
		child.lite=4;	pusher(child);
		child.lite=5;	pusher(child);
		child.lite=6;	pusher(child);
	}
	for(int i = 0; i < col[y+1].size(); i++)
	{
		child.ind=col[y+1][i];
		child.lite=4;	pusher(child);
		child.lite=5;	pusher(child);
	}
	for(int i = 0; i < col[y+2].size(); i++)
	{
		child.ind=col[y+2][i];
		child.lite=4;	pusher(child);
	}
	child.dist=d;
	for(int i = 0; i < col[y].size(); i++)
	{
		child.ind=col[y][i];
		if(arr[child.ind].x==x-1)
		{
			if(light==1) child.lite=2;
			else if(light==2) child.lite=3;
			else if(light==3) child.lite=0;
			else child.lite=light;
			if(!actvis[child.ind][child.lite])
			{
				q.pf(child);
			}
		}
		else if(arr[child.ind].x==x+1)
		{
			if(light==1) child.lite=0;
			else if(light==2) child.lite=1;
			else if(light==3) child.lite=2;
			else child.lite=light;
			if(!actvis[child.ind][child.lite])
			{
				q.pf(child);
			}
		}
	}
	for(int i = 0; i < row[x].size(); i++)
	{
		child.ind=row[x][i];
		if(arr[child.ind].y==y-1)
		{
			if(light==4) child.lite=5;
			else if(light==5) child.lite=6;
			else if(light==6) child.lite=0;
			else child.lite=light;
			if(!actvis[child.ind][child.lite])
			{
				q.pf(child);
			}
		}
		else if(arr[child.ind].y==y+1)
		{
			if(light==4) child.lite=0;
			else if(light==5) child.lite=4;
			else if(light==6) child.lite=5;
			else child.lite=light;
			if(!actvis[child.ind][child.lite])
			{
				q.pf(child);
			}
		}
	}
	while(q.size()>0)
	{
		child=q.front();
		q.pop_front();
		if(!actvis[child.ind][child.lite])
		{
			depth[child.ind][child.lite]=child.dist;
			bfs(child.ind, child.lite);
		}
	}
}
int main()
{
	cin >> n >> m >> k;
	bool endlit=false;
	for(int i = 0; i < k; i++)
	{
		cin >> arr[i].x >> arr[i].y;
		if(arr[i].x==1 && arr[i].y==1) strt=i;
		else if(arr[i].x==n && arr[i].y==m) endlit=true;
		row[arr[i].x].pb(i);
		col[arr[i].y].pb(i);
	}
	for(int i = 0; i <= k; i++)
	{
		for(int j = 0; j < 7; j++)
		{
			depth[i][j]=-1;
		}
	}
	if(!endlit)
	{
		arr[k].x=n; arr[k].y=m;
		row[n].pb(k);
		col[m].pb(k);
	}
	depth[strt][0]=0;
	visited[strt][0]=1;
	bfs(strt, 0);
	int mn=inf;
	if(endlit)
	{
		for(int i = 0; i < 7; i++)
		{
	//		cout << i << " = " << depth[k][i] << endl;
			if(depth[k-1][i]<mn && depth[k-1][i]!=-1)
			{
				mn=depth[k-1][i];
			}
		}
	}
	if(mn!=inf)
	{
		cout << mn << endl;
		return 0;
	}
	if(endlit) k--;
	if(depth[k][2]==-1)
	{
		cout << depth[k][5];
	}
	else if(depth[k][5]==-1)
	{
		cout << depth[k][2];
	}
	else cout << min(depth[k][2], depth[k][5]);
}


#include <bits/stdc++.h>
using namespace std;
int dir[4][2]= {0,1,0,-1,1,0,-1,0};
const int M=1e4+10;
int x[M],y[M];
int ans,k,n,m, ccnntt=0;
map<int,int>ycq[M];
struct aa
{
    int u,v,w,next;
} edge[20*M];
int pre[3*M];
bool vis[3*M];
int dis[3*M];
void add(int u,int v,int w)
{
    edge[ans].u=u;
    edge[ans].v=v;
    edge[ans].w=w;
    edge[ans].next=pre[u];
    pre[u]=ans++;
}
void dij()
{
    dis[1]=0;
    queue<int>ycqq;
    ycqq.push(1);
    vis[1]=0;
    while(!ycqq.empty())
    {
        int u=ycqq.front();
        ycqq.pop();
        vis[u]=0;
        for(int i=pre[u]; i!=-1; i=edge[i].next)
        {
            int v=edge[i].v;
            int w=edge[i].w;
            if(dis[v]>dis[u]+w)
            {
                dis[v]=dis[u]+w;

                if(!vis[v])
                {
                    vis[v]=1;
                    ycqq.push(v);
                }
            }
        }
    }
    int minn=0x3f3f3f3f;
   for(int i=1; i<=k; i++)
    {
        if(x[i]==n&&y[i]==m)
        {
            minn=min(minn,dis[i]);
        }
        else
        {
            if(abs(x[i]-n)<=1||abs(y[i]-m)<=1)
            {
                minn=min(minn,dis[i]+1);
            }
        }
    }
    if(minn==0x3f3f3f3f)
        printf("%d\n",-1);
    else
        printf("%d\n",minn);

}
int main()
{
    scanf("%d%d%d",&n,&m,&k);
        ans=1;
        for(int i=1; i<=(m+n+k); i++)
        {
            vis[i]=0;
            dis[i]=0x3f3f3f3f;
            pre[i]=-1;
        }
        for(int i=1; i<=k; i++)
        {
            scanf("%d%d",&x[i],&y[i]);
            ycq[x[i]][y[i]]=i;
        }
        for(int i=1; i<=k; i++)
        {
            int xx=x[i],yy=y[i];
            for(int j=0; j<4; j++)
            {
                int xxx=xx+dir[j][0];
                int yyy=yy+dir[j][1];
                if(ycq[xxx][yyy]>0)
                {
                    add(i,ycq[xxx][yyy],0);
                }
            }
            if(xx+1>=1&&xx+1<=n)
                add(i,xx+1+k,1);
            add(i,xx+0+k,1);
            if(xx-1>=1&&xx-1<=n)
                add(i,xx-1+k,1);
            if(yy+1>=1&&yy+1<=m)
                add(i,yy+1+n+k,1);
            add(i,yy+0+n+k,1);
            if(yy-1>=1&&yy-1<=m)
                add(i,yy-1+n+k,1);
            if(xx+1>=1&&xx+1<=n)
                add(xx+1+k,i,0);
            add(xx+0+k,i,0);
            if(xx-1>=1&&xx-1<=n)
                add(xx-1+k,i,0);
            if(yy+1>=1&&yy+1<=m)
                add(yy+1+n+k,i,0);
            add(yy+0+n+k,i,0);
            if(yy-1>=1&&yy-1<=m)
                add(yy-1+n+k,i,0);
        }
        dij();
}


#include <bits/stdc++.h>
using namespace std;
#define ref(i,x,y)for(int i=x;i<=y;++i)
#define def(i,x,y)for(int i=x;i>=y;--i)
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define SZ(x) ((int)x.size())
typedef pair<int,int> PII;
typedef vector<int> vint;
int read(){
	char c=getchar();int d=0,f=1;
	for(;c<'0'||c>'9';c=getchar())if(c=='-')f=-1;
	for(;c>='0'&&c<='9';d=d*10+c-48,c=getchar());
	return d*f;
}
const int N=2010;
const int mod=1e9+7;
int n,m,k;char s[N][N];
PII p[N];int ans;
int dp[N][N],t1[N][N],t2[N][N];
void init(){
	ref(i,1,n)ref(j,1,m)if(s[i][j]!='X')
		dp[i][j]=min(min(dp[i-1][j],dp[i][j-1]),dp[i-1][j-1])+1;
	ref(j,1,m)t1[0][j]=0,t2[n+1][j]=n+1;
	ref(i,1,n)ref(j,1,m)if(s[i][j]=='X')t1[i][j]=i;else t1[i][j]=t1[i-1][j];
	def(i,n,1)ref(j,1,m)if(s[i][j]=='X')t2[i][j]=i;else t2[i][j]=t2[i+1][j];
	ref(i,1,n)ref(j,1,m)ans=max(ans,dp[i][j]);
}
vint Ans;
PII a1[N],a2[N];
bool check(int x,int y,int le){
	int h1=1,ta1=0,h2=1,ta2=0;
	int op=max(1,y-le+1),ed=min(y,m-le+1);
	ref(i,op,op+le-1){
		PII w=mp(t1[x][i],i);
		while(h1<=ta1&&a1[ta1]<w)ta1--;a1[++ta1]=w;
		w=mp(t2[x][i],i);
		while(h2<=ta2&&a2[ta2]>w)ta2--;a2[++ta2]=w;
	}
	ref(i,op,ed){
		if(a2[h2].fi-a1[h1].fi-1>=le)return 1;
		while(h1<=ta1&&a1[h1].se==i)h1++;
		while(h2<=ta2&&a2[h2].se==i)h2++;
		if(i+le<=m){
			PII w=mp(t1[x][i+le],i+le);
			while(h1<=ta1&&a1[ta1]<w)ta1--;a1[++ta1]=w;
			w=mp(t2[x][i+le],i+le);
			while(h2<=ta2&&a2[ta2]>w)ta2--;a2[++ta2]=w;
		}
	}
	return 0;
}
int main(){
	n=read(),m=read(),k=read();
	ref(i,1,n)scanf("%s",s[i]+1);
	ref(i,1,k)p[i].fi=read(),p[i].se=read();
	ref(i,1,k)s[p[i].fi][p[i].se]='X';
	init();
	Ans.pb(ans);
	def(I,k,2){
		int x=p[I].fi,y=p[I].se;
		s[x][y]='.';
		ref(i,1,n)if(s[i][y]=='X')t1[i][y]=i;else t1[i][y]=t1[i-1][y];
		def(i,n,1)if(s[i][y]=='X')t2[i][y]=i;else t2[i][y]=t2[i+1][y];
		while(ans<min(n,m)&&check(x,y,ans+1))ans++;
		Ans.pb(ans);
	}
	def(i,SZ(Ans)-1,0)cout<<Ans[i]<<endl;
}


#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

int read(){
    int x=0;bool f=0;char c=getchar();
    while(c<'0'||c>'9') f|=c=='-',c=getchar();
    while(c>='0'&&c<='9') x=x*10+(c^48),c=getchar();
    return f?-x:x;
}

#define MAXN 2000
int N,M,Q;
int Ans[MAXN+5];
int dp[MAXN+5][MAXN+5],Up[MAXN+5][MAXN+5],LLen[MAXN+5],RLen[MAXN+5];
struct Query{
    int x,y;
}A[MAXN+5];
char Park[MAXN+5][MAXN+5];

struct UnionFind{
    int fa[MAXN+5];
    int Find(int x){
        return fa[x]==x?x:fa[x]=Find(fa[x]);
    }
}LMost[MAXN+5],RMost[MAXN+5];
void Add(int i,int j){
    LMost[i].fa[j]=LMost[i].Find(j-1);
    RMost[i].fa[j]=RMost[i].Find(j+1);
}

int main(){
    N=read(),M=read(),Q=read();
    for(int i=1;i<=N;i++)
        for(int j=0;j<=M+1;j++){
            Up[i][j]=i;
            LMost[i].fa[j]=j,
            RMost[i].fa[j]=j;
        }
    for(int i=1;i<=N;i++)
        scanf("%s",Park[i]+1);
    for(int i=1;i<=Q;i++)
        A[i].x=read(),A[i].y=read(),
        Park[A[i].x][A[i].y]='X';
    for(int i=1;i<=N;i++)
        for(int j=1;j<=M;j++)
            if(Park[i][j]=='.')
                Add(i,j);
    int tmp=0;
    for(int i=1;i<=N;i++)
        for(int j=1;j<=M;j++)
            if(Park[i][j]=='.'){
                Up[i][j]=Up[i-1][j];
                dp[i][j]=min(dp[i-1][j-1]+1,min(i-Up[i][j],j-LMost[i].Find(j)));
                tmp=max(tmp,dp[i][j]);
            }
    for(int i=Q;i>=1;i--){
        Ans[i]=tmp;
        Add(A[i].x,A[i].y);
        for(int j=1;j<=N;j++){
            LLen[j]=A[i].y-LMost[j].Find(A[i].y);
            RLen[j]=RMost[j].Find(A[i].y)-A[i].y;
        }
        for(int j=A[i].x-1;j>=1;j--)
            LLen[j]=min(LLen[j],LLen[j+1]),
            RLen[j]=min(RLen[j],RLen[j+1]);
        for(int j=A[i].x+1;j<=N;j++)
            LLen[j]=min(LLen[j],LLen[j-1]),
            RLen[j]=min(RLen[j],RLen[j-1]);
        for(int j=1;j<=A[i].x;j++)
            while(j+tmp<=N&&min(RLen[j],RLen[j+tmp])+min(LLen[j],LLen[j+tmp])-1>tmp)
                tmp++;
    }
    for(int i=1;i<=Q;i++)
        printf("%d\n",Ans[i]);
}


#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <cmath>
#include <set>
#include <map>
#include <cstdio>
#include <stdlib.h>
#include <stack>
#include <numeric>
#include <bitset>
#include <cctype>
#include <list>
#include <assert.h>
#include <random>
#include <unordered_map>
typedef long long ll;
#define MAX 2105
using namespace std;
#define INF 2147483647
#define infL (1LL<<60)
#define inf (1000005)
#define MOD 1000000007
#define EPS 1e-9
#define Gr 9.8
#define PI acos(-1)
#define REP(i,n) for(int (i)=0;(i)<(int)(n);(i)++)
#define REQ(i,n) for(int (i)=1;(i)<=(int)(n);(i)++)
#define lch (rt<<1)
#define rch (rt<<1|1)
bool debug = 1;
int n,m,k;
int dx[4] = {0,1,0,-1}, dy[4] = {1,0,-1,0};
ll ln, lk, lm;

char mp[MAX][MAX];
int x[MAX], y[MAX], ans[MAX], A;
set<int> line[MAX];
int L[MAX], R[MAX];
bool cal(int i, int j) {
    int top = max(i - (A-1), 1);
    int bot = min(i+A-1,n);

    multiset<int> sl,sr;
    for(int p=top;p<=bot;p++) {
        auto it = line[p].lower_bound(j);
        if (*it == j) {
            L[p]=j+1; R[p]=j-1; continue;
        }
        R[p] = (*it) - 1; it--; L[p] = (*it)+1;
    }
    for(int p=top;p<=bot;p++) {
        sl.insert(L[p]);
        sr.insert(R[p]);
        if (p-top>=A) {
            int q = p-A;
            sl.erase(sl.find(L[q])); sr.erase(sr.find(R[q]));
        }
        if (sl.size() == A) {
            int dis = *sr.begin() - (*sl.rbegin());
            if (dis+1>=A) return 1;
        }
    }
    return 0;
}
int a[MAX][MAX];
int main() {
    scanf("%d%d%d", &n, &m, &k);
    REQ(i,n) {
        scanf("%s", mp[i]+1);
    }
    REQ(i,k) {
        scanf("%d%d",x+i,y+i); mp[x[i]][y[i]]='X';
    }
    REQ(i,n) {
        line[i].insert(0); line[i].insert(m+1);
        REQ(j,m) if(mp[i][j]=='X')line[i].insert(j);
        else  {
            a[i][j] = min(min(a[i-1][j], a[i][j-1]), a[i-1][j-1])+1;
            A = max(A, a[i][j]);
        }
    }
    ans[k]=A;
    A++;
    for(int i=k;i>1;i--) {
        line[x[i]].erase(y[i]);
        while(cal(x[i],y[i])) A++;
        ans[i-1]=A-1;
    }
    REQ(i,k) printf("%d\n", ans[i]);
    return 0;
}


#include <bits/stdc++.h>
#if defined(__GNUC__)
#pragma GCC optimize ("Ofast")
#endif
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<pii, int> piii;
typedef pair<ll, ll> pll;
#define debug(x) cerr<<#x<<'='<<(x)<<endl;
#define debugp(x) cerr<<#x<<"= {"<<(x.first)<<", "<<(x.second)<<"}"<<endl;
#define debug2(x, y) cerr<<"{"<<#x<<", "<<#y<<"} = {"<<(x)<<", "<<(y)<<"}"<<endl;
#define debugv(v) cerr<<#v<<" : ";for (auto x:v) cerr<<x<<' ';cerr<<endl;
#define all(x) x.begin(), x.end()
#define pb push_back
#define kill(x) return cout<<x<<'\n', 0;

const ld eps=1e-7;
const int inf=1000000010;
const ll INF=10000000000000010LL;
const int mod = 1000000007;
const int MAXN = 2010, LOG=20;

int n, m, k, u, v, x, y, t, a, b, ans;
int A[MAXN][MAXN+1];
int X[MAXN], Y[MAXN], out[MAXN];
int L[MAXN][MAXN+2];
int R[MAXN][MAXN+3];
char ch;

void process(int i){
	for (int j=1; j<=m; j++){
		L[i][j]=max(L[i][j-1], 1);
		if (A[i][j]) L[i][j]=j+1;
	}
	R[i][m]=m+1;
	for (int j=m-1; j; j--){
		R[i][j]=R[i][j+1];
		if (A[i][j]) R[i][j]=j;
	}
}

void go_col(int y){
	debug(y)
	deque<int> dl, dr;
	for (int i=1, j=1; i<=n; i++){
		while (dl.size() && L[dl.front()][y]<L[i][y]) dl.pop_front();
		dl.push_front(i);
		while (dr.size() && R[dr.front()][y]>R[i][y]) dr.pop_front();
		dr.push_front(i);

		while (j<=i && dr.size() && dl.size() && R[dr.back()][y]-L[dl.back()][y]<i-j+1){
			if (dr.back()<=j) dr.pop_back();
			if (dl.back()<=j) dl.pop_back();
			j++;
		}
		ans=max(ans, min(i-j+1, R[dr.back()][y]-L[dl.back()][y]));
	}
}

int main(){
	ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	cin>>n>>m>>k;
	for (int i=1; i<=n; i++) for (int j=1; j<=m; j++){
		cin>>ch;
		A[i][j]=(ch=='X');
	}
	for (int i=1; i<=k; i++){
		cin>>X[i]>>Y[i];
		A[X[i]][Y[i]]=1;
	}
	for (int i=1; i<=n; i++) for (int j=1; j<=m; j++){
		L[i][j]=max(L[i][j-1], 1);
		if (A[i][j]) L[i][j]=j+1;
	}
	for (int i=1; i<=n; i++) process(i);
	for (int i=1; i<=m; i++) go_col(i);
	debug(ans)

	for (int i=k; i; i--){
		out[i]=ans;
		A[X[i]][Y[i]]=0;
		process(X[i]);
		go_col(Y[i]);
	}
	for (int i=1; i<=k; i++) cout<<out[i]<<'\n';

	return 0;
}
/*
7 8 4
........
X.....X.
........
........
.X......
........
........
1 5
6 4
3 5
4 6
*/


#include <bits/stdc++.h>
#if defined(__GNUC__)
#pragma GCC optimize ("Ofast")
#endif
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<pii, int> piii;
typedef pair<ll, ll> pll;
#define debug(x) cerr<<#x<<'='<<(x)<<endl;
#define debugp(x) cerr<<#x<<"= {"<<(x.first)<<", "<<(x.second)<<"}"<<endl;
#define debug2(x, y) cerr<<"{"<<#x<<", "<<#y<<"} = {"<<(x)<<", "<<(y)<<"}"<<endl;
#define debugv(v) cerr<<#v<<" : ";for (auto x:v) cerr<<x<<' ';cerr<<endl;
#define all(x) x.begin(), x.end()
#define pb push_back
#define kill(x) return cout<<x<<'\n', 0;

const ld eps=1e-7;
const int inf=1000000010;
const ll INF=10000000000000010LL;
const int mod = 1000000007;
const int MAXN = 2010, LOG=20;

int n, m, k, u, v, x, y, t, a, b, ans;
int A[MAXN][MAXN+1];
int X[MAXN], Y[MAXN], out[MAXN];
int L[MAXN][MAXN+2];
int R[MAXN][MAXN+3];
char ch;

void process(int i){
	for (int j=1; j<=m; j++){
		L[i][j]=max(L[i][j-1], 1);
		if (A[i][j]) L[i][j]=j+1;
	}
	R[i][m]=m+1;
	for (int j=m-1; j; j--){
		R[i][j]=R[i][j+1];
		if (A[i][j]) R[i][j]=j;
	}
}

void go_col(int y){
	debug(y)
	deque<int> dl, dr;
	for (int i=1, j=1; i<=n; i++){
		while (dl.size() && L[dl.front()][y]<L[i][y]) dl.pop_front();
		dl.push_front(i);
		while (dr.size() && R[dr.front()][y]>R[i][y]) dr.pop_front();
		dr.push_front(i);

		while (j<i && R[dr.back()][y]-L[dl.back()][y]<i-j+1){
			if (dr.back()<=j) dr.pop_back();
			if (dl.back()<=j) dl.pop_back();
			j++;
		}
		ans=max(ans, min(i-j+1, R[dr.back()][y]-L[dl.back()][y]));
	}
}

int main(){
	ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	cin>>n>>m>>k;
	for (int i=1; i<=n; i++) for (int j=1; j<=m; j++){
		cin>>ch;
		A[i][j]=(ch=='X');
	}
	for (int i=1; i<=k; i++){
		cin>>X[i]>>Y[i];
		A[X[i]][Y[i]]=1;
	}
	for (int i=1; i<=n; i++) for (int j=1; j<=m; j++){
		L[i][j]=max(L[i][j-1], 1);
		if (A[i][j]) L[i][j]=j+1;
	}
	for (int i=1; i<=n; i++) process(i);
	for (int i=1; i<=m; i++) go_col(i);
	debug(ans)

	for (int i=k; i; i--){
		out[i]=ans;
		A[X[i]][Y[i]]=0;
		process(X[i]);
		go_col(Y[i]);
	}
	for (int i=1; i<=k; i++) cout<<out[i]<<'\n';

	return 0;
}
/*
7 8 4
........
X.....X.
........
........
.X......
........
........
1 5
6 4
3 5
4 6
*/


#include <bits/stdc++.h>
#if defined(__GNUC__)
#pragma GCC optimize ("Ofast")
#endif
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<pii, int> piii;
typedef pair<ll, ll> pll;
#define debug(x) cerr<<#x<<'='<<(x)<<endl;
#define debugp(x) cerr<<#x<<"= {"<<(x.first)<<", "<<(x.second)<<"}"<<endl;
#define debug2(x, y) cerr<<"{"<<#x<<", "<<#y<<"} = {"<<(x)<<", "<<(y)<<"}"<<endl;
#define debugv(v) cerr<<#v<<" : ";for (auto x:v) cerr<<x<<' ';cerr<<endl;
#define all(x) x.begin(), x.end()
#define pb push_back
#define kill(x) return cout<<x<<'\n', 0;

const ld eps=1e-7;
const int inf=1000000010;
const ll INF=10000000000000010LL;
const int mod = 1000000007;
const int MAXN = 2010, LOG=20;

int n, m, k, u, v, x, y, t, a, b, ans;
int A[MAXN][MAXN];
int X[MAXN], Y[MAXN], out[MAXN];
int L[MAXN][MAXN];
int R[MAXN][MAXN];
char ch;

void process(int i){
	for (int j=1; j<=m; j++){
		L[i][j]=max(L[i][j-1], 1);
		if (A[i][j]) L[i][j]=j+1;
	}
	R[i][m]=m+1;
	if (A[i][m]) R[i][m]=m;
	for (int j=m-1; j; j--){
		R[i][j]=R[i][j+1];
		if (A[i][j]) R[i][j]=j;
	}
}

void go_col(int y){
	deque<int> dl, dr;
	for (int i=1, j=1; i<=n; i++){
		while (dl.size() && L[dl.front()][y]<L[i][y]) dl.pop_front();
		dl.push_front(i);
		while (dr.size() && R[dr.front()][y]>R[i][y]) dr.pop_front();
		dr.push_front(i);

		while (j<i && R[dr.back()][y]-L[dl.back()][y]<i-j+1){
			if (dr.back()<=j) dr.pop_back();
			if (dl.back()<=j) dl.pop_back();
			j++;
		}
		ans=max(ans, min(i-j+1, R[dr.back()][y]-L[dl.back()][y]));
	}
}

int main(){
	ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	cin>>n>>m>>k;
	for (int i=1; i<=n; i++) for (int j=1; j<=m; j++){
		cin>>ch;
		A[i][j]=(ch=='X');
	}
	for (int i=1; i<=k; i++){
		cin>>X[i]>>Y[i];
		A[X[i]][Y[i]]=1;
	}

	for (int i=1; i<=n; i++) process(i);
	for (int i=1; i<=m; i++) go_col(i);

	for (int i=k; i; i--){
		out[i]=ans;
		A[X[i]][Y[i]]=0;
		process(X[i]);
		go_col(Y[i]);
	}
	for (int i=1; i<=k; i++) cout<<out[i]<<'\n';

	return 0;
}


#include <bits/stdc++.h>
#if defined(__GNUC__)
#pragma GCC optimize ("Ofast")
#endif
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<pii, int> piii;
typedef pair<ll, ll> pll;
#define debug(x) cerr<<#x<<'='<<(x)<<endl;
#define debugp(x) cerr<<#x<<"= {"<<(x.first)<<", "<<(x.second)<<"}"<<endl;
#define debug2(x, y) cerr<<"{"<<#x<<", "<<#y<<"} = {"<<(x)<<", "<<(y)<<"}"<<endl;
#define debugv(v) cerr<<#v<<" : ";for (auto x:v) cerr<<x<<' ';cerr<<endl;
#define all(x) x.begin(), x.end()
#define pb push_back
#define kill(x) return cout<<x<<'\n', 0;

const ld eps=1e-7;
const int inf=1000000010;
const ll INF=10000000000000010LL;
const int mod = 1000000007;
const int MAXN = 2010, LOG=20;

int n, m, k, u, v, x, y, t, a, b, ans;
int A[MAXN][MAXN+1];
int X[MAXN], Y[MAXN], out[MAXN];
int L[MAXN][MAXN+2];
int R[MAXN][MAXN+3];
char ch;

void process(int i){
	for (int j=1; j<=m; j++){
		L[i][j]=max(L[i][j-1], 1);
		if (A[i][j]) L[i][j]=j+1;
	}
	R[i][m]=m+1;
	for (int j=m-1; j; j--){
		R[i][j]=R[i][j+1];
		if (A[i][j]) R[i][j]=j;
	}
}

void go_col(int y){
	deque<int> dl, dr;
	for (int i=1, j=1; i<=n; i++){
		while (dl.size() && L[dl.front()][y]<L[i][y]) dl.pop_front();
		dl.push_front(i);
		while (dr.size() && R[dr.front()][y]>R[i][y]) dr.pop_front();
		dr.push_front(i);

		debug(dr.back())
		debug(dl.back())

		if (dl.back()<0) return ;
		if (dr.back()<0) return ;

		while (j<=n && R[dr.back()][y]-L[dl.back()][y]<i-j+1){
			if (dr.back()<=j) dr.pop_back();
			if (dl.back()<=j) dl.pop_back();
			j++;
		}
		ans=max(ans, min(i-j+1, R[dr.back()][y]-L[dl.back()][y]));
	}
}

int main(){
	ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	cin>>n>>m>>k;
	for (int i=1; i<=n; i++) for (int j=1; j<=m; j++){
		cin>>ch;
		A[i][j]=(ch=='X');
	}
	for (int i=1; i<=k; i++){
		cin>>X[i]>>Y[i];
		A[X[i]][Y[i]]=1;
	}
	for (int i=1; i<=n; i++) for (int j=1; j<=m; j++){
		L[i][j]=max(L[i][j-1], 1);
		if (A[i][j]) L[i][j]=j+1;
	}
	for (int i=1; i<=n; i++) process(i);
	for (int i=1; i<=m; i++) go_col(i);
	debug(ans)

	for (int i=k; i; i--){
		out[i]=ans;
		A[X[i]][Y[i]]=0;
		process(X[i]);
		//go_col(Y[i]);
	}
	for (int i=1; i<=k; i++) cout<<out[i]<<'\n';

	return 0;
}
/*
7 8 4
........
X.....X.
........
........
.X......
........
........
1 5
6 4
3 5
4 6
*/


#include <bits/stdc++.h>
#if defined(__GNUC__)
#pragma GCC optimize ("Ofast")
#endif
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<pii, int> piii;
typedef pair<ll, ll> pll;
#define debug(x) cerr<<#x<<'='<<(x)<<endl;
#define debugp(x) cerr<<#x<<"= {"<<(x.first)<<", "<<(x.second)<<"}"<<endl;
#define debug2(x, y) cerr<<"{"<<#x<<", "<<#y<<"} = {"<<(x)<<", "<<(y)<<"}"<<endl;
#define debugv(v) cerr<<#v<<" : ";for (auto x:v) cerr<<x<<' ';cerr<<endl;
#define all(x) x.begin(), x.end()
#define pb push_back
#define kill(x) return cout<<x<<'\n', 0;

const ld eps=1e-7;
const int inf=1000000010;
const ll INF=10000000000000010LL;
const int mod = 1000000007;
const int MAXN = 2010, LOG=20;

int n, m, k, u, v, x, y, t, a, b, ans;
int A[MAXN][MAXN];
int X[MAXN], Y[MAXN], out[MAXN];
int L[MAXN][MAXN];
int R[MAXN][MAXN];
char ch;

void process(int i){
	for (int j=1; j<=m; j++){
		L[i][j]=max(L[i][j-1], 1);
		if (A[i][j]) L[i][j]=j+1;
	}
	R[i][m]=m+1;
	for (int j=m-1; j; j--){
		R[i][j]=R[i][j+1];
		if (A[i][j]) R[i][j]=j;
	}
}

void go_col(int y){
	deque<int> dl, dr;
	for (int i=1, j=1; i<=n; i++){
		while (dl.size() && L[dl.front()][y]<L[i][y]) dl.pop_front();
		dl.push_front(i);
		while (dr.size() && R[dr.front()][y]>R[i][y]) dr.pop_front();
		dr.push_front(i);

		while (j<=n && R[dr.back()][y]-L[dl.back()][y]<i-j+1){
			if (dr.back()<=j) dr.pop_back();
			if (dl.back()<=j) dl.pop_back();
			j++;
		}
		ans=max(ans, min(i-j+1, R[dr.back()][y]-L[dl.back()][y]));
	}
}

int main(){
	ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	cin>>n>>m>>k;
	for (int i=1; i<=n; i++) for (int j=1; j<=m; j++){
		cin>>ch;
		A[i][j]=(ch=='X');
	}
	for (int i=1; i<=k; i++){
		cin>>X[i]>>Y[i];
		A[X[i]][Y[i]]=1;
	}
	for (int i=1; i<=n; i++) for (int j=1; j<=m; j++){
		L[i][j]=max(L[i][j-1], 1);
		if (A[i][j]) L[i][j]=j+1;
	}
	for (int i=1; i<=n; i++) process(i);
	for (int i=1; i<=m; i++) go_col(i);
	debug(ans)

	for (int i=k; i; i--){
		out[i]=ans;
		A[X[i]][Y[i]]=0;
		process(X[i]);
		go_col(Y[i]);
	}
	for (int i=1; i<=k; i++) cout<<out[i]<<'\n';

	return 0;
}
/*
7 8 0
........
X.....X.
........
........
.X......
........
........
*/


#include <bits/stdc++.h>
#if defined(__GNUC__)
#pragma GCC optimize ("Ofast")
#endif
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<pii, int> piii;
typedef pair<ll, ll> pll;
#define debug(x) cerr<<#x<<'='<<(x)<<endl;
#define debugp(x) cerr<<#x<<"= {"<<(x.first)<<", "<<(x.second)<<"}"<<endl;
#define debug2(x, y) cerr<<"{"<<#x<<", "<<#y<<"} = {"<<(x)<<", "<<(y)<<"}"<<endl;
#define debugv(v) cerr<<#v<<" : ";for (auto x:v) cerr<<x<<' ';cerr<<endl;
#define all(x) x.begin(), x.end()
#define pb push_back
#define kill(x) return cout<<x<<'\n', 0;

const ld eps=1e-7;
const int inf=1000000010;
const ll INF=10000000000000010LL;
const int mod = 1000000007;
const int MAXN = 2010, LOG=20;

int n, m, k, u, v, x, y, t, a, b, ans;
int A[MAXN][MAXN];
int X[MAXN], Y[MAXN], out[MAXN];
int L[MAXN][MAXN];
int R[MAXN][MAXN];
char ch;

void process(int i){
	for (int j=1; j<=m; j++){
		L[i][j]=max(L[i][j-1], 1);
		if (A[i][j]) L[i][j]=j+1;
	}
	R[i][m]=m+1;
	for (int j=m-1; j; j--){
		R[i][j]=R[i][j+1];
		if (A[i][j]) R[i][j]=j;
	}
}

void go_col(int y){
	deque<int> dl, dr;
	for (int i=1, j=1; i<=n; i++){
		while (dl.size() && L[dl.front()][y]<L[i][y]) dl.pop_front();
		dl.push_front(i);
		while (dr.size() && R[dr.front()][y]>R[i][y]) dr.pop_front();
		dr.push_front(i);

		debug(dr.back())
		debug(dl.back())

		if (dl.empty()) return ;
		if (dr.empty()) return ;

		while (j<=n && R[dr.back()][y]-L[dl.back()][y]<i-j+1){
			if (dr.back()<=j) dr.pop_back();
			if (dl.back()<=j) dl.pop_back();
			j++;
		}
		ans=max(ans, min(i-j+1, R[dr.back()][y]-L[dl.back()][y]));
	}
}

int main(){
	ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	cin>>n>>m>>k;
	for (int i=1; i<=n; i++) for (int j=1; j<=m; j++){
		cin>>ch;
		A[i][j]=(ch=='X');
	}
	for (int i=1; i<=k; i++){
		cin>>X[i]>>Y[i];
		A[X[i]][Y[i]]=1;
	}
	for (int i=1; i<=n; i++) for (int j=1; j<=m; j++){
		L[i][j]=max(L[i][j-1], 1);
		if (A[i][j]) L[i][j]=j+1;
	}
	for (int i=1; i<=n; i++) process(i);
	for (int i=1; i<=m; i++) go_col(i);
	debug(ans)

	for (int i=k; i; i--){
		out[i]=ans;
		A[X[i]][Y[i]]=0;
		process(X[i]);
		//go_col(Y[i]);
	}
	for (int i=1; i<=k; i++) cout<<out[i]<<'\n';

	return 0;
}
/*
7 8 4
........
X.....X.
........
........
.X......
........
........
1 5
6 4
3 5
4 6
*/


#include <iostream>
#include <fstream>
#include <iomanip>
#include <math.h>
#include <limits.h>
#include <algorithm>
#include <vector>
#include <list>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <bitset>
#include <string>
#include <string.h>
#include <sstream>
#include <ctime>

using namespace std;

#define eps 1e-12
#define pi 3.14159265358979323846
#define pb push_back
#define mp make_pair
#define st first
#define nd second
#define bgn begin
#define ll long long
#define ld long double
#define ull unsigned long long
#define ii pair<ll,ll>;

const int N=1e5+10;
ll n,u[N],v[N],c[N];
vector<int>adj[N];
bool vld;

void dfs(int u,int p)
{
	for(int i=0,v;i<adj[u].size();i++)
	{
		v=adj[u][i];
		if(v==p)continue;
		dfs(v,u);
		if(c[v]!=c[u])vld=0;
	}
}

void solve()
{
	cin>>n;
	for(int i=1;i<n;i++)
	{
		cin>>u[i]>>v[i];
		adj[u[i]].pb(v[i]);
		adj[v[i]].pb(u[i]);
	}
	for(int i=1;i<=n;i++)
	{
		cin>>c[i];
	}
	for(int i=1;i<n;i++)
	{
		if(c[u[i]]!=c[v[i]])
		{
			vld=1;
			for(int j=0;j<adj[u[i]].size();j++)
			{
				dfs(adj[u[i]][j],u[i]);
				if(!vld)break;
			}
			if(vld)cout<<"YES\n"<<u[i]<<"\n";
			else
			{
				vld=1;
				for(int j=0;j<adj[v[i]].size();j++)
				{
					dfs(adj[v[i]][j],v[i]);
					if(!vld)break;
				}
				if(vld)cout<<"YES\n"<<v[i]<<"\n";
				else cout<<"NO\n";
			}
			return;
		}
	}
	cout<<"YES\n1\n";
}

int main()
{
	std::ios::sync_with_stdio(0);
	cin.tie(0);
#ifndef ONLINE_JUDGE
	freopen("in.txt","r",stdin);
#endif
	solve();
	return 0;
}


#include <cstdio>
#include <iostream>
#include <sstream>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <list>
#include <stack>
#include <string>
#include <unordered_map>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <limits>
#include <iomanip>
#pragma comment(linker, "/STACK:256000000")

#define ull unsigned long long
#define ll long long
#define ld long double
#define forl(i,n) for(ll i = 0; i < n; ++i)
#define dforl(i,n) for(ll i = n - 1; i >= 0; --i)
#define INF numeric_limits<ll>::max();
#define MINF numeric_limits<ll>::min();

using namespace std;

vector<vector<ll>> g;
vector<ll> c;
vector<ll> used;

bool mono(ll v, ll color)
{
	if (c[v] != color)
		return false;
	used[v] = true;
	for(ll i = 0; i < g[v].size(); ++i)
	{
		if(used[g[v][i]])
			continue;
		ll to = g[v][i];
		bool ok = mono(to,color);
		if (!ok)
		{
			used[v] = false;
			return false;
		}
	}
	used[v] = false;
	return true;
}
bool check(ll v)
{
	used[v] = true;
	for(ll i = 0; i < g[v].size(); ++i)
	{
		if(used[g[v][i]])
			continue;
		ll color = c[g[v][i]];
		bool ok = mono(g[v][i], color);
		if (!ok)
		{
			used[v] = false;
			return false;
		}
	}
	used[v] = false;
	return true;
}

pair<ll,ll> dfs(ll v)
{
	used[v] = true;
	pair<ll,ll> buf;
	for(ll i = 0; i < g[v].size(); ++i)
	{
		if(used[g[v][i]])
			continue;
		ll to = g[v][i];
		if(c[v] != c[to])
		{
			used[v] = false;
			return make_pair(v,to);
		}
		buf = dfs(to);
		if (buf.first != -1 && buf.second != -1)
		{
			used[v] = false;
			return buf;
		}
	}
	used[v] = false;
	return make_pair(-1, -1);
}
int main()
{
#ifdef MY_DEBUG
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
#endif

	ll n;
	cin >> n;
	g.resize(n);
	c.resize(n);
	used.resize(n);
	forl(i,n - 1)
	{
		ll u, v;
		cin >> u >> v;
		--u;
		--v;
		g[u].push_back(v);
		g[v].push_back(u);
	}
	forl(i,n)
		cin >> c[i];

	pair<ll,ll> vert = dfs(0);
	if (vert.first == -1 && vert.second == -1)
	{
		cout << "YES" << endl;
		cout << 1 << endl;
		return 0;
	}

	bool v1 = check(vert.first);
	if (v1)
	{
		cout << "YES" << endl;
		cout << vert.first + 1 << endl;
		return 0;
	}
	bool v2 = check(vert.second);
	if (v2)
	{
		cout << "YES" << endl;
		cout << vert.second + 1 << endl;
		return 0;
	}

	cout << "NO" << endl;
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <map>
#include <algorithm>
#include <cmath>
#include <iterator>
#include <set>
#include <cassert>
#include <queue>

using namespace std;

typedef vector<int> vi;
typedef vector<vector<int> > vvi;
typedef vector<char> vc;
typedef vector< vector<char> > vvc;
typedef vector<long long> vll;
typedef vector< vector<long long> > vvll;

typedef long long ll;

template<typename T>
void print(T v) {
	for (int i = 0; i < v.size(); i++)
		cout << v[i] << " ";
	cout << endl;
}

template<typename T>
void print2(T v) {
	for (int i = 0; i < v.size(); i++) {
		for (int j = 0; j < v[i].size(); j++)
			cout << v[i][j] << " ";
		cout << endl;
	}
	cout << endl;
}

template<typename T>
void print_pair(T v) {
	for (int i = 0; i < v.size(); i++)
		cout << v[i].first << " " << v[i].second << endl;
	cout << endl;
}

//ifstream fi("input.txt");
//ofstream fo("output.txt

//FILE *fi = fopen("input.txt", "r");
//FILE *fo = fopen("output.txt", "w");

struct Edge {
	int u, v;
};

vector<Edge> e;
vvi g;
vi c;

int main() {
	int n;
	cin >> n;
	//g.assign(n, vi());
	c.assign(n, 0);
	Edge tmp;
	for (int i = 0; i < n - 1; i++) {
		cin >> tmp.u >> tmp.v;
		tmp.u--;
		tmp.v--;
		if (tmp.v > tmp.u)
			swap(tmp.v, tmp.u);
		e.push_back(tmp);
	}
	for (int i = 0; i < n; i++)
		cin >> c[i];

	int flag = 0;
	Edge root_edge = {-1, -1};
	int root = -1;
	for (int i = 0; i < n - 1; i++) {
		if (c[e[i].u] != c[e[i].v]) {
			if (root_edge.v == -1) {
				root_edge = e[i];
				continue;
			}
			if (c[root_edge.v] != c[e[i].v] && c[root_edge.u] != c[e[i].u] &&
				c[root_edge.u] != c[e[i].v] && c[root_edge.v] != c[e[i].u]) {
				flag = -1;
				break;
			}
			if (c[root_edge.v] == c[e[i].v] && c[root_edge.u] == c[e[i].u]) {
				if (root_edge.v != e[i].v && root_edge.u != e[i].u) {
					flag = -1;
					break;
				}
				if (root_edge.v == e[i].v) {
					if (root != -1 && root != e[i].v) {
						flag = -1;
						break;
					}
					root = e[i].v;
				}
				if (root_edge.u == e[i].u) {
					if (root != -1 && root != e[i].u) {
						flag = -1;
						break;
					}
					root = e[i].u;
				}
				continue;
			}

			if (c[root_edge.u] == c[e[i].v] && c[root_edge.v] == c[e[i].u]) {
				if (root_edge.u != e[i].v && root_edge.v != e[i].u) {
					flag = -1;
					break;
				}
				if (root_edge.u == e[i].v) {
					if (root != -1 && root != e[i].v) {
						flag = -1;
						break;
					}
					root = e[i].v;
				}
				if (root_edge.v == e[i].u) {
					if (root != -1 && root != e[i].u) {
						flag = -1;
						break;
					}
					root = e[i].u;
				}
				continue;
			}

			if (c[root_edge.v] == c[e[i].v]) {
				if (root_edge.v != e[i].v) {
					flag = -1;
					break;
				}
				root = e[i].v;
				continue;
			}
			if (c[root_edge.u] == c[e[i].u]) {
				if (root_edge.u != e[i].u) {
					flag = -1;
					break;
				}
				root = e[i].u;
				continue;
			}
			if (c[root_edge.v] == c[e[i].u]) {
				if (root_edge.v != e[i].u) {
					flag = -1;
					break;
				}
				root = e[i].u;
				continue;
			}
			if (c[root_edge.u] == c[e[i].v]) {
				if (root_edge.u != e[i].v) {
					flag = -1;
					break;
				}
				root = e[i].v;
				continue;
			}
		}
	}

	if (flag == -1) {
		cout << "NO";
		return 0;
	}
	if (root == -1) {
		if (root_edge.u == -1)
			root_edge.u = 0;
		cout << "YES\n" << root_edge.u + 1;
		return 0;
	}
	cout << "YES\n";
	cout << root + 1;
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <map>
#include <algorithm>
#include <cmath>
#include <iterator>
#include <set>
#include <cassert>
#include <queue>

using namespace std;

typedef vector<int> vi;
typedef vector<vector<int> > vvi;
typedef vector<char> vc;
typedef vector< vector<char> > vvc;
typedef vector<long long> vll;
typedef vector< vector<long long> > vvll;

typedef long long ll;

template<typename T>
void print(T v) {
	for (int i = 0; i < v.size(); i++)
		cout << v[i] << " ";
	cout << endl;
}

template<typename T>
void print2(T v) {
	for (int i = 0; i < v.size(); i++) {
		for (int j = 0; j < v[i].size(); j++)
			cout << v[i][j] << " ";
		cout << endl;
	}
	cout << endl;
}

template<typename T>
void print_pair(T v) {
	for (int i = 0; i < v.size(); i++)
		cout << v[i].first << " " << v[i].second << endl;
	cout << endl;
}

//ifstream fi("input.txt");
//ofstream fo("output.txt

//FILE *fi = fopen("input.txt", "r");
//FILE *fo = fopen("output.txt", "w");

struct Edge {
	int u, v;
};

vector<Edge> e;
vvi g;
vi c;

int main() {
	int n;
	cin >> n;
	//g.assign(n, vi());
	c.assign(n, 0);
	Edge tmp;
	for (int i = 0; i < n - 1; i++) {
		cin >> tmp.u >> tmp.v;
		tmp.u--;
		tmp.v--;
		if (tmp.v > tmp.u)
			swap(tmp.v, tmp.u);
		e.push_back(tmp);
	}
	for (int i = 0; i < n; i++)
		cin >> c[i];

	int flag = 0;
	Edge root_edge = {-1, -1};
	int root = -1;
	for (int i = 0; i < n - 1; i++) {
		if (c[e[i].u] != c[e[i].v]) {
			if (root_edge.v == -1) {
				root_edge = e[i];
				continue;
			}
			if (c[root_edge.v] != c[e[i].v] && c[root_edge.u] != c[e[i].u] &&
				c[root_edge.u] != c[e[i].v] && c[root_edge.v] != c[e[i].u]) {
				flag = -1;
				break;
			}
			if (c[root_edge.v] == c[e[i].v] && c[root_edge.u] == c[e[i].u]) {
				if (root_edge.v != e[i].v && root_edge.u != e[i].u) {
					flag = -1;
					break;
				}
				if (root_edge.v == e[i].v) {
					if (root != -1 && root != e[i].v) {
						flag = -1;
						break;
					}
					root = e[i].v;
				}
				if (root_edge.u == e[i].u) {
					if (root != -1 && root != e[i].u) {
						flag = -1;
						break;
					}
					root = e[i].u;
				}
				continue;
			}

			if (c[root_edge.u] == c[e[i].v] && c[root_edge.v] == c[e[i].u]) {
				if (root_edge.u != e[i].v && root_edge.v != e[i].u) {
					flag = -1;
					break;
				}
				if (root_edge.u == e[i].v) {
					if (root != -1 && root != e[i].v) {
						flag = -1;
						break;
					}
					root = e[i].v;
				}
				if (root_edge.v == e[i].u) {
					if (root != -1 && root != e[i].u) {
						flag = -1;
						break;
					}
					root = e[i].u;
				}
				continue;
			}

			if (c[root_edge.v] == c[e[i].v]) {
				if (root_edge.v != e[i].v) {
					flag = -1;
					break;
				}
				root = e[i].v;
				continue;
			}
			if (c[root_edge.u] == c[e[i].u]) {
				if (root_edge.u != e[i].u) {
					flag = -1;
					break;
				}
				root = e[i].u;
				continue;
			}
			if (c[root_edge.v] == c[e[i].u]) {
				if (root_edge.v != e[i].u) {
					flag = -1;
					break;
				}
				root = e[i].u;
				continue;
			}
			if (c[root_edge.u] == c[e[i].v]) {
				if (root_edge.u != e[i].v) {
					flag = -1;
					break;
				}
				root = e[i].v;
				continue;
			}
		}
	}

	if (flag == -1) {
		cout << "NO";
		return 0;
	}
	if (root == -1) {
		cout << "YES\n" << root_edge.u + 1;
		return 0;
	}
	cout << root + 1;
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <map>
#include <algorithm>
#include <cmath>
#include <iterator>
#include <set>
#include <cassert>
#include <queue>

using namespace std;

typedef vector<int> vi;
typedef vector<vector<int> > vvi;
typedef vector<char> vc;
typedef vector< vector<char> > vvc;
typedef vector<long long> vll;
typedef vector< vector<long long> > vvll;

typedef long long ll;

template<typename T>
void print(T v) {
	for (int i = 0; i < v.size(); i++)
		cout << v[i] << " ";
	cout << endl;
}

template<typename T>
void print2(T v) {
	for (int i = 0; i < v.size(); i++) {
		for (int j = 0; j < v[i].size(); j++)
			cout << v[i][j] << " ";
		cout << endl;
	}
	cout << endl;
}

template<typename T>
void print_pair(T v) {
	for (int i = 0; i < v.size(); i++)
		cout << v[i].first << " " << v[i].second << endl;
	cout << endl;
}

//ifstream fi("input.txt");
//ofstream fo("output.txt

//FILE *fi = fopen("input.txt", "r");
//FILE *fo = fopen("output.txt", "w");

struct Edge {
	int u, v;
};

vector<Edge> e;
vvi g;
vi c;

int main() {
	int n;
	cin >> n;
	//g.assign(n, vi());
	c.assign(n, 0);
	Edge tmp;
	for (int i = 0; i < n - 1; i++) {
		cin >> tmp.u >> tmp.v;
		tmp.u--;
		tmp.v--;
		if (tmp.v > tmp.u)
			swap(tmp.v, tmp.u);
		e.push_back(tmp);
	}
	for (int i = 0; i < n; i++)
		cin >> c[i];

	int flag = 0;
	Edge root_edge = {-1, -1};
	int root = -1;
	for (int i = 0; i < n - 1; i++) {
		if (c[e[i].u] != c[e[i].v]) {
			if (root_edge.v == -1) {
				root_edge = e[i];
				continue;
			}
			if (c[root_edge.v] != c[e[i].v] && c[root_edge.u] != c[e[i].u] &&
				c[root_edge.u] != c[e[i].v] && c[root_edge.v] != c[e[i].u]) {
				flag = -1;
				break;
			}
			if (c[root_edge.v] == c[e[i].v] && c[root_edge.u] == c[e[i].u]) {
				if (root_edge.v != e[i].v && root_edge.u != e[i].u) {
					flag = -1;
					break;
				}
				if (root_edge.v == e[i].v) {
					if (root != -1 && root != e[i].v) {
						flag = -1;
						break;
					}
					root = e[i].v;
				}
				if (root_edge.u == e[i].u) {
					if (root != -1 && root != e[i].u) {
						flag = -1;
						break;
					}
					root = e[i].u;
				}
				continue;
			}

			if (c[root_edge.u] == c[e[i].v] && c[root_edge.v] == c[e[i].u]) {
				if (root_edge.u != e[i].v && root_edge.v != e[i].u) {
					flag = -1;
					break;
				}
				if (root_edge.u == e[i].v) {
					if (root != -1 && root != e[i].v) {
						flag = -1;
						break;
					}
					root = e[i].v;
				}
				if (root_edge.v == e[i].u) {
					if (root != -1 && root != e[i].u) {
						flag = -1;
						break;
					}
					root = e[i].u;
				}
				continue;
			}

			if (c[root_edge.v] == c[e[i].v]) {
				if (root_edge.v != e[i].v) {
					flag = -1;
					break;
				}
				root = e[i].v;
				continue;
			}
			if (c[root_edge.u] == c[e[i].u]) {
				if (root_edge.u != e[i].u) {
					flag = -1;
					break;
				}
				root = e[i].u;
				continue;
			}
			if (c[root_edge.v] == c[e[i].u]) {
				if (root_edge.v != e[i].u) {
					flag = -1;
					break;
				}
				root = e[i].u;
				continue;
			}
			if (c[root_edge.u] == c[e[i].v]) {
				if (root_edge.u != e[i].v) {
					flag = -1;
					break;
				}
				root = e[i].v;
				continue;
			}
		}
	}

	if (flag == -1) {
		cout << "NO";
		return 0;
	}
	if (root == -1) {
		cout << "YES\n" << root_edge.u + 1;
		return 0;
	}
	cout << "YES\n";
	cout << root + 1;
	return 0;
}


#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<stdio.h>
#include<queue>
#include<vector>
#include<stack>
#include<map>
#include<set>
#include<time.h>
#include<cmath>
using namespace std;
#define x first
#define y second
#define pb push_back
#define mp make_pair
typedef long long int LL;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3fLL;
const int MAXN = 1e5 + 10;
int c[MAXN];
bool flag;
vector<int>G[MAXN];
void dfs(int u, int fa, int col){
    if (!flag){ return; }
    for (int i = 0; i < G[u].size(); i++){
        if (G[u][i] != fa){
            if (c[G[u][i]] != col){
                flag = false;
            }
            else{
                dfs(G[u][i], u, col);
            }
        }
    }
}
int solve(int x){
    flag = true;
    for (int i = 0; i < G[x].size()&&flag; i++){
        dfs(G[x][i], x, c[G[x][i]]);
    }
    if (flag){
        return x;
    }
    else{
        return -1;
    }
}
int main(){
//#ifdef kirito
//    freopen("in.txt", "r", stdin);
//    freopen("out.txt", "w", stdout);
//#endif
//    int start = clock();
    int n;
    while (~scanf("%d", &n)){
        for (int i = 0; i <= n; i++){
            G[i].clear();
        }
        for (int i = 1; i < n; i++){
            int u, v;
            scanf("%d%d", &u, &v);
            G[u].push_back(v);
            G[v].push_back(u);
        }
        for (int i = 1; i <= n; i++){
            scanf("%d", &c[i]);
        }
        int ans = -1; bool k = false;
        for (int i = 1; i <= n&&k==false; i++){
            for (int j = 0; j < G[i].size(); j++){
                if (c[i] != c[G[i][j]]){ //有边并且颜色不一样
                    ans = solve(i);
                    if (ans == -1){
                        ans = solve(G[i][j]);
                    }
                    k = true;
                    break;
                }
            }
        }
        if (k == false){
            ans = 1;
        }
        if (ans != -1){
            printf("YES\n%d\n", ans);
        }
        else{
            printf("NO\n");
        }
    }
//#ifdef LOCAL_TIME
//    cout << "[Finished in " << clock() - start << " ms]" << endl;
//#endif
    return 0;
}


#include<stdio.h>
const int maxn=100020;
int nxt[maxn*2],to[maxn*2],fst[maxn],cnt,color;
int a[maxn];
bool real_dfs(int n,int pre){
	for(int t=fst[n];t;t=nxt[t])if(to[t]!=pre){
		if(!real_dfs(to[t],n))return 0;
		if(a[to[t]]!=color)return 0;
	}
	return 1;
}
bool dfs(int n){
	for(int t=fst[n];t;t=nxt[t]){
		color=a[to[t]];
		if(!real_dfs(to[t],n))return 0;
	}
	return 1;
}
int main(){
	int N,u,v,A,B;
	scanf("%d",&N);
	for(int i=1;i<N;i++){
		scanf("%d%d",&u,&v);
		nxt[++cnt]=fst[u],to[cnt]=v,fst[u]=cnt;
		nxt[++cnt]=fst[v],to[cnt]=u,fst[v]=cnt;
	}
	for(int i=1;i<=N;i++)
		scanf("%d",a+i);
	bool node=0;
	for(int i=1;i<=N;i++)
		for(int t=fst[i];t;t=nxt[t]){
			if(a[i]!=a[to[t]]){
				node=1;
				A=i;
				B=to[t];
				break;
			}
		};
	if(!node)
		printf("YES\n1");
	else if(dfs(A))
		printf("YES\n%d",A);
	else if(dfs(B))
		printf("YES\n%d",B);
	else
		printf("NO");
	return 0;
}

/*
8
1 2
1 3
2 4
2 5
2 6
6 7
3 8
1 2 3 2 2 2 2 3

*/


#pragma comment(linker, "/STACK:216000000")
#include <iostream>
#include <cstdio>
#include <fstream>
#include <vector>
#include <string>
#include <queue>
#include <map>
#include <algorithm>
#include <numeric>
#include <set>
#include <bitset>
#include <sstream>
#include <array>
#include <iomanip>

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
const ll MAX = numeric_limits<ll>::max();
const ll MIN = numeric_limits<ll>::min();
const double PI = 3.1415926535;
const ll MOD = 1000000007;

template<class T>
ostream& operator<<(ostream& out, vector<T>& v) {
	for (int i = 0; i < v.size(); ++i)
		out << v[i] << " ";
	out << endl;
	return out;
}

template<class T>
istream& operator>>(istream& in, vector<T>& v) {
	for (int i = 0; i < v.size(); ++i)
		in >> v[i];
	return in;
}

template<class R, class L>
istream& operator>>(istream& in, pair<R, L>& p) {
	in >> p.first >> p.second;
	return in;
}

ll gcd(ll a, ll b) {
	if (b == 0)
		return a;
	return gcd(b, a % b);
}

ll ceil(ll a, ll b) {
	if (a % b)
		return a / b + 1;
	return a / b;
}

int ans;

void dfs1(int v, vector<vector<int>>&G, vector<bool>& used, vector<int>& c, vector<int>& can) {
	used[v] = true;
	map<int, int> ch;
	ch[c[v]]++;
	for (int i = 0; i < G[v].size(); ++i) {
		if (!used[G[v][i]]) {
			dfs1(G[v][i], G, used, c, can);
			ch[can[G[v][i]]]++;
		}
	}
	if (ch.size() > 1) {
		can[v] = -1;
	}
	else {
		can[v] = c[v];
	}
}

void dfs2(int v, vector<vector<int>>&G, vector<bool>& used, vector<int>& c, int color, vector<int>& can) {
	used[v] = true;
	map<int, int> ch;
	ch[color]++;
	for (int i = 0; i < G[v].size(); ++i) {
		if (!used[G[v][i]]) {
			ch[can[G[v][i]]]++;
		}
	}
	if (ch.find(-1) == ch.end()) {
		ans = v;
		return;
	}
	ch[c[v]]++;
	for (int i = 0; i < G[v].size(); ++i) {
		if (!used[G[v][i]]) {
			int chc = can[G[v][i]];
			if (ch[chc] == 1 && ch.size() == 2) {
				ch.erase(ch.find(chc));
				dfs2(G[v][i], G, used, c, ch.begin()->first, can);
				ch[chc] = 1;
			}
			else if (ch.size() == 1) {
				dfs2(G[v][i], G, used, c, chc, can);
			}
			else
				dfs2(G[v][i], G, used, c, -1, can);
		}
	}
}

int main() {
	ios_base::sync_with_stdio(false);
#ifdef DEBUG
	ifstream cin{ "input.txt" };
	ofstream cout{ "output.txt" };
#endif
	int n;
	cin >> n;
	vector<vector<int>> G(n);
	for (int i = 0; i < n - 1; ++i) {
		int x, y;
		cin >> x >> y;
		x--, y--;
		G[x].push_back(y);
		G[y].push_back(x);
	}
	vector<int> c(n);
	cin >> c;
	vector<bool> used(n, false);
	ans = -1;
	vector<int> can(n, -1);
	for (int i = 0; i < n; ++i) {
		if (G[i].size() == 1) {
			dfs1(i, G, used, c, can);
			used = vector<bool>(n, false);
			dfs2(i, G, used, c, c[i], can);
			break;
		}
	}
	if (ans == -1)
		cout << "NO";
	else
		cout << "YES" << endl << ans + 1;
}


#pragma comment(linker, "/STACK:216000000")
#include <iostream>
#include <cstdio>
#include <fstream>
#include <vector>
#include <string>
#include <queue>
#include <map>
#include <algorithm>
#include <numeric>
#include <set>
#include <bitset>
#include <sstream>
#include <array>
#include <iomanip>

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
const ll MAX = numeric_limits<ll>::max();
const ll MIN = numeric_limits<ll>::min();
const double PI = 3.1415926535;
const ll MOD = 1000000007;

template<class T>
ostream& operator<<(ostream& out, vector<T>& v) {
	for (int i = 0; i < v.size(); ++i)
		out << v[i] << " ";
	out << endl;
	return out;
}

template<class T>
istream& operator>>(istream& in, vector<T>& v) {
	for (int i = 0; i < v.size(); ++i)
		in >> v[i];
	return in;
}

template<class R, class L>
istream& operator>>(istream& in, pair<R, L>& p) {
	in >> p.first >> p.second;
	return in;
}

ll gcd(ll a, ll b) {
	if (b == 0)
		return a;
	return gcd(b, a % b);
}

ll ceil(ll a, ll b) {
	if (a % b)
		return a / b + 1;
	return a / b;
}

int ans;

int dfs(int v, vector<vector<int>>&G, vector<bool>& used, int color, vector<int>& c) {
	used[v] = true;
	map<int, int> ch;
	for (int i = 0; i < G[v].size(); ++i) {
		if (!used[G[v][i]]) {
			int newc = (color == c[v]) ? color : -1;
			ch[dfs(G[v][i], G, used, newc, c)]++;
		}
	}
	if (ch.find(-1) == ch.end() && color != -1) {
		ans = v;
	}
	if (ch.size() == 0)
		return c[v];
	if (ch.size() == 1 && ch.begin()->first == c[v]) {
		return c[v];
	}
	return -1;
}

int main() {
	ios_base::sync_with_stdio(false);
#ifdef DEBUG
	ifstream cin{ "input.txt" };
	ofstream cout{ "output.txt" };
#endif
	int n;
	cin >> n;
	vector<vector<int>> G(n);
	for (int i = 0; i < n - 1; ++i) {
		int x, y;
		cin >> x >> y;
		x--, y--;
		G[x].push_back(y);
		G[y].push_back(x);
	}
	vector<int> c(n);
	cin >> c;
	vector<bool> used(n, false);
	ans = -1;
	for (int i = 0; i < n; ++i) {
		if (G[i].size() == 1) {
			dfs(i, G, used, c[i], c);
			break;
		}
	}
	if (ans == -1)
		cout << "NO";
	else
		cout << "YES" << endl << ans + 1;
}


#include <algorithm>
#include <iostream>
#include <string>
#include <cstring>
#include <math.h>
#include <cmath>
#include <vector>
#include <utility>
#include <cstdlib>
#include <deque>
#include <queue>
#include <iomanip>
#include <stack>
#include <map>
#include <set>
#include <cmath>
#include <list>
#define mp make_pair
#define pb push_back
#define sqrt(a) sqrt((ld)a)
#define pow(a, b) pow((ld)a, (ld)b)
#define preturn(a) return cout << a << endl, 0
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef unsigned int uint;
const int nn = (int)2000000;
const ld eps = 3 * 10e-8;
const ld eps1 = 5 * 10e-8;
const ull INF = (ull)(1e19 * 1.7);
const ll mod = 10;

int t[200000], y[200000], color[200000], e = -1, r = -1, color2[200000];
vector<vector<int>> a(100005);
bool us[200000];

bool dfs(int u)
{
	bool bol = 1;
	us[u] = 1;
	for (int i = 0; i < a[u].size(); i++)
		if (!us[a[u][i]])
			bol = bol && dfs(a[u][i]) && (color[u] == color[a[u][i]] || !color[u]);
	return bol;
}

int main()
{
	int n;
	cin >> n;
	for (int i = 0; i < n - 1; i++)
	{
		cin >> t[i] >> y[i];
		t[i]--;y[i]--;
		a[t[i]].pb(y[i]);
		a[y[i]].pb(t[i]);
	}
	for (int i = 0; i < n; i++) cin >> color[i];
	for (int i = 0; i < n - 1; i++)
		if (color[t[i]] != color[y[i]]) e = t[i], r = y[i];
	if (e == -1) preturn("YES\n1");
	int colr = color[r], cole = color[e];
	color[r] = 0;
	if (dfs(r))
	{
		cout << "YES\n";
		preturn(r + 1);
	}
	color[r] = colr;
	for (int i = 0; i <= 100000; i++) us[i] = 0;
	color[e] = 0;
	if (dfs(e))
	{
		cout << "YES\n";
		preturn(e + 1);
	}
	cout << "NO";
	return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
#define N 100005

struct Node
{
    int p,d;
} deg[N];
vector<int> gra[N];
int col[N];

bool cmp(Node a,Node b)
{
    return a.d<b.d;
}

int father[N];
int wei[N];
int Find(int a)
{
    if(father[a]!=a)
        father[a]=Find(father[a]);
    return father[a];
}

void Merge(int a,int b)
{
    int fa=Find(a);
    int fb=Find(b);
    if(fa!=fb)
    {
        father[fa]=fb;
        wei[fb]+=wei[fa];
    }
}

int main()
{
    int n;
    scanf("%d",&n);
    int a,b;
    for(int i=1; i<=n; i++)
    {
        deg[i].p=i;
        father[i]=i;
        wei[i]=1;
    }
    for(int i=0; i<n-1; i++)
    {
        scanf("%d%d",&a,&b);
        deg[a].d++;
        deg[b].d++;
        gra[a].push_back(b);
        gra[b].push_back(a);
    }
    for(int i=1; i<=n; i++)
        scanf("%d",&col[i]);
    for(int i=1; i<=n; i++)
        for(int j=0; j<gra[i].size(); j++)
        {
            if(col[i]==col[gra[i][j]])
                Merge(i,gra[i][j]);
        }
    int cnt=0;
    for(int i=1;i<=n;i++)
        if(father[i]==i)
            cnt++;
    for(int i=1;i<=n;i++)
    {
        if(cnt<=deg[i].d+1)
        {
            printf("YES\n%d\n",i);
            return 0;
        }
    }
    printf("NO\n");
    return 0;
}


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
#define N 100005

struct Node
{
    int p,d;
} deg[N];

vector<int> gra[N];
int col[N];

bool cmp(Node a,Node b)
{
    return a.d<b.d;
}

int father[N];
int wei[N];
int Find(int a)
{
    if(father[a]!=a)
        father[a]=Find(father[a]);
    return father[a];
}

void Merge(int a,int b)
{
    int fa=Find(a);
    int fb=Find(b);
    if(fa!=fb)
    {
        father[fa]=fb;
        wei[fb]+=wei[fa];
    }
}

int main()
{
    int n;
    scanf("%d",&n);
    int a,b;
    for(int i=1; i<=n; i++)
    {
        deg[i].p=i;
        father[i]=i;
        wei[i]=1;
    }
    for(int i=0; i<n-1; i++)
    {
        scanf("%d%d",&a,&b);
        deg[a].d++;
        deg[b].d++;
        gra[a].push_back(b);
        gra[b].push_back(a);
    }
    for(int i=1; i<=n; i++)
        scanf("%d",&col[i]);
    for(int i=1; i<=n; i++)
        for(int j=0; j<gra[i].size(); j++)
        {
            if(col[i]==col[gra[i][j]])
            {
                if(Find(i)!=Find(gra[i][j]))
                {Merge(i,gra[i][j]);
                deg[i].d--;
                deg[gra[i][j]].d--;}
            }
        }
    int cnt=0;
    for(int i=1; i<=n; i++)
        if(father[i]==i)
            cnt++;
    /*printf("%d\n",cnt);
    for(int i=1; i<=n; i++)
    {
        printf("%d",deg[i].d);
        if(i==n)
            printf("\n");
        else
            printf(" ");
    }*/
    for(int i=1; i<=n; i++)
    {
        if(cnt<=deg[i].d+1)
        {
            printf("YES\n%d\n",i);
            return 0;
        }
    }
    printf("NO\n");
    return 0;
}

/*
6
1 2
2 3
2 4
4 5
4 6
1 1 1 2 2 3
*/


#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif
#include <stdio.h>
#include <algorithm>
#include <functional>
#include <cstring>
#include <map>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <set>
#include <list>
#include <string>
#include <iostream>
#include <fstream>
#include <vector>
#include <stack>

#define setzero(arr) memset(arr, 0, sizeof(arr))

#define fi(i, from, to) for(int i = (from); i < (to); i++)
#define fri(i, from, to) for(int i = (from); i >= (to); i--)
#define fa(it, col) for(auto (it) = (col).begin(); (it) != (col).end(); (it)++)
#define fra(it, col) for(auto (it) = (col).rbegin(); (it) != (col).rend(); (it)++)
#define getBit(vv, bit) ((vv & (lol)(1LL << (31 - bit))) == 0LL ? 0 : 1)
#define getBit64(vv, bit) ((vv & (lol)(1LL << (63 - bit))) == 0LL ? 0 : 1)
#define allFrom(obj) obj.begin(), obj.end()
#define MOD 1000000007
#define PI 3.14159265
#define INF 10000000

typedef unsigned long long lol;
typedef  long long slol;
using namespace std;

bool visited[101000];
vector<int> vtx[101000];
int color[101000];

bool check(int v) {
	if (visited[v])
		return true;

	visited[v] = true;

	fa(it, vtx[v]) {
		if (visited[*it])
			continue;

		if (color[*it] != color[v] || !check(*it))
			return false;
	}

	return true;
}

bool checkRoot(int rid) {
	setzero(visited);
	visited[rid] = true;
	fa(it, vtx[rid]) {
		if (!check(*it))
			return false;
	}

	return true;
}

int colors(int vid, int &other) {
	set<int> ss;
	fa(it, vtx[vid]) {
		if (color[*it] == color[vid])
			continue;

		other = *it;
		ss.insert(color[*it]);
		if (ss.size() >= 2)
			break;
	}

	return ss.size();
}

int main() {

#if defined(_DEBUG)
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	int n;
	scanf("%d", &n);
	fi(i, 0, n-1) {
		int a, b;
		scanf("%d %d ", &a, &b);
		a--, b--;
		vtx[a].push_back(b);
		vtx[b].push_back(a);
	}

	fi(i, 0, n) {
		scanf("%d ", &color[i]);
	}

	fi(i, 0, n) {
		int other = 0;
		int cls = colors(i, other);
		if (cls >= 2) {
			if (checkRoot(i)) {
				printf("YES\n%d", i + 1);
			}
			else {
				printf("NO");
			}
			return 0;
		}

		if (cls == 1) {
			if (checkRoot(i)) {
				printf("YES\n%d", i + 1);
			}else if(checkRoot(other)){
				printf("YES\n%d", other + 1);
			}else{
				printf("NO");
			}
			return 0;
		}
	}

	printf("YES\n1");

	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<ctype.h>
#include<queue>
#include<stack>
#include<algorithm>
#include<map>
#include<set>
using namespace std;
const int inf=1e8;
const int MAXN=2e5+10;
#define pi acos(-1.0)
#define ms(x,y) memset(x,y,sizeof(x))
#define inone(x) scanf("%d",&x)
#define intwo(x,y) scanf("%d%d",&x,&y)
#define inthree(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define rep(i,j,k) for(int i=j;i<=k;i++)
#define per(i,j,k) for(int i=j;i>=k;i--)
int m,n,ans,res,tot,a,b,c,d,e,f,g,Max,Min,k;
int s[MAXN][2],s1[MAXN],flag[MAXN][2],cnt[2];
int main()
{
    inone(n);flag[0][0]=flag[0][1]=1;
    rep(i,1,n-1) intwo(s[i][0],s[i][1]);
    rep(i,1,n) inone(s1[i]);
    rep(i,1,n-1)
    {
        a=s[i][0],b=s[i][1];
        if(s1[a]!=s1[b])
            flag[tot][0]=a,flag[tot++][1]=b;
    }
    c=flag[0][0],d=flag[0][1];
    rep(i,0,tot-1)
    {
        if(flag[i][0]==c||flag[i][1]==c) cnt[0]++;
        if(flag[i][0]==d||flag[i][1]==d) cnt[1]++;
    }
    if(cnt[0]==tot) printf("YES\n%d",c);
    else if(cnt[1]==tot) printf("YES\n%d",d);
    else printf("NO");
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<ctype.h>
#include<queue>
#include<stack>
#include<algorithm>
#include<map>
#include<set>
using namespace std;
const int inf=1e8;
const int MAXN=2e5+10;
#define pi acos(-1.0)
#define ms(x,y) memset(x,y,sizeof(x))
#define inone(x) scanf("%d",&x)
#define intwo(x,y) scanf("%d%d",&x,&y)
#define inthree(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define rep(i,j,k) for(int i=j;i<=k;i++)
#define per(i,j,k) for(int i=j;i>=k;i--)
int m,n,ans,res,tot,a,b,c,d,e,f,g,Max,Min,k;
int s[MAXN][2],s1[MAXN],flag[MAXN][2],cnt[2];
int main()
{
    inone(n);
    rep(i,1,n-1) intwo(s[i][0],s[i][1]);
    rep(i,1,n) inone(s1[i]);
    rep(i,1,n-1)
    {
        a=s[i][0],b=s[i][1];
        if(s1[a]!=s1[b])
            flag[tot][0]=a,flag[tot++][1]=b;
    }
    c=flag[0][0],d=flag[0][1];
    rep(i,0,tot-1)
    {
        if(flag[i][0]==c||flag[i][1]==c) cnt[0]++;
        if(flag[i][0]==d||flag[i][1]==d) cnt[1]++;
    }
    if(cnt[0]*2==tot) printf("YES\n%d",flag[c][0]);
    else if(cnt[1]*2==tot) printf("YES\n%d",flag[d][1]);
    else printf("NO");
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<ctype.h>
#include<queue>
#include<stack>
#include<algorithm>
#include<map>
#include<set>
using namespace std;
const int inf=1e8;
const int MAXN=2e5+10;
#define pi acos(-1.0)
#define ms(x,y) memset(x,y,sizeof(x))
#define inone(x) scanf("%d",&x)
#define intwo(x,y) scanf("%d%d",&x,&y)
#define inthree(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define rep(i,j,k) for(int i=j;i<=k;i++)
#define per(i,j,k) for(int i=j;i>=k;i--)
int m,n,ans,res,tot,a,b,c,d,e,f,g,Max,Min,k;
int s[MAXN][2],s1[MAXN],flag[MAXN][2],cnt[2];
int main()
{
    inone(n);c=d=1;
    rep(i,1,n-1) intwo(s[i][0],s[i][1]);
    rep(i,1,n) inone(s1[i]);
    rep(i,1,n-1)
    {
        a=s[i][0],b=s[i][1];
        if(s1[a]!=s1[b])
            flag[tot][0]=a,flag[tot++][1]=b;
    }
    c=flag[0][0],d=flag[0][1];
    rep(i,0,tot-1)
    {
        if(flag[i][0]==c||flag[i][1]==c) cnt[0]++;
        if(flag[i][0]==d||flag[i][1]==d) cnt[1]++;
    }
    if(cnt[0]==tot) printf("YES\n%d",c);
    else if(cnt[1]==tot) printf("YES\n%d",d);
    else printf("NO");
    return 0;
}


#include <iostream>
#include <iomanip>
#include <vector>
#include <string>
#include <algorithm>
#include <utility>
#include <cmath>
using namespace std;
int gcd(int a, int b)
{
	return b ? gcd(b, a % b) : a;
}
int NOK(int a, int b)
{
	return a*b / gcd(a, b);
}
int main()
{
	int n, i;
	cin >> n;
	bool possible = true;
	vector<int> C(n);
	vector < pair < int, int > > E(n-1);
	vector < pair < int, int > > D;
	for (i = 0; i < n-1; ++i)
	{
		cin >> E[i].first;
		E[i].first--;
		cin >> E[i].second;
		E[i].second--;
	}
	for (i = 0; i < n; ++i) cin >> C[i];
	for (i = 0; i < n - 1; ++i)
	{
		if (C[E[i].first] != C[E[i].second]) D.push_back(make_pair(E[i].first, E[i].second));
	}
	if (D.size() == 0)
	{
		cout << "YES" << endl;
		cout << "1" << endl;
	}
	else
	{
		pair <int, int> Cand;
		Cand.first = D[0].first;
		Cand.second = D[0].second;
		for (i = 0; i < D.size(); ++i)
		{
			if (Cand.first != D[i].first && Cand.first != D[i].second)
			{
				Cand.first = Cand.second;
				if (Cand.second != D[i].first && Cand.second != D[i].second) possible = false;
			}
			if (Cand.second != D[i].first && Cand.second != D[i].second)
			{
				Cand.second = Cand.first;
				if (Cand.first != D[i].first && Cand.first != D[i].second) possible = false;
			}

		}
		if (possible)
		{
			cout << "YES" << endl;
			cout << Cand.first +1 << endl;
		}
		else cout << "NO" << endl;
	}

	return 0;
}


#include <algorithm>
#include <bitset>
#include <cassert>
#include <deque>
#include <queue>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <float.h>
#include <limits>
#include <list>
#include <map>
#include <math.h>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<int, int> pi;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pi> vp;
typedef vector<string> vs;
typedef set<int> si;
typedef map<int, int> mi;
typedef multimap<int, int> mmi;

vi groups;

int get_group(int u)
{
	while (u != groups[u])
		u = groups[u];
	return u;
}

int main()
{
	ios::sync_with_stdio(false);

	int n;
	cin >> n;

	vp e(n - 1);
	vi* al = new vi[n];
	for (int i = 0; i < n - 1; ++i)
	{
		cin >> e[i].first >> e[i].second;
		--e[i].first;
		--e[i].second;
		al[e[i].first].push_back(e[i].second);
		al[e[i].second].push_back(e[i].first);
	}

	vi c(n);
	for (int i = 0; i < n; ++i)
		cin >> c[i];

	groups.resize(n);
	for (int i = 0; i < n; ++i)
		groups[i] = i;

	for (int i = 0; i < n - 1; ++i)
	{
		int u = e[i].first;
		int v = e[i].second;
		if (c[u] == c[v])
		{
			int u_group = get_group(u);
			int v_group = get_group(v);
			groups[max(u_group, v_group)] = min(u_group, v_group);
		}
	}

	vi cnt(n);
	for (int i = 0; i < n; ++i)
	{
		int group_i = get_group(i);
		for (int j = 0, m = al[i].size(); j < m; ++j)
		{
			int group_j = get_group(al[i][j]);
			if (group_j != group_i)
				++cnt[i];
		}
	}
	int res = -1;
	int total = 0;
	bool ok = true;
	for (int i = 0; i < n; ++i)
	{
		if (cnt[i] > 1)
		{
			if (res == -1)
			{
				res = i;
			}
			else
			{
				ok = false;
				break;
			}
		}
		total += cnt[i];
	}

	if (!ok)
	{
		cout << "NO" << endl;
		return 0;
	}

	if (res == -1)
	{
		for (int i = 0; i < n; ++i)
		{
			if (cnt[i] == 1)
			{
				res = i;
				break;
			}
		}
	}

	if (res != -1 && cnt[res] * 2 != total)
	{
		cout << "NO" << endl;
		return 0;
	}

	cout << "YES" << endl;
	cout << (res == -1 ? 1 : res + 1) << endl;

	return 0;
}


#include <algorithm>
#include <bitset>
#include <cassert>
#include <deque>
#include <queue>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <float.h>
#include <limits>
#include <list>
#include <map>
#include <math.h>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<int, int> pi;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pi> vp;
typedef vector<string> vs;
typedef set<int> si;
typedef map<int, int> mi;
typedef multimap<int, int> mmi;

vi groups;
//int group_edges[MAX];

int get_group(int u)
{
	while (u != groups[u])
		u = groups[u];
	return u;
}

int main()
{
	ios::sync_with_stdio(false);

	int n;
	cin >> n;

	vp e(n - 1);
	vi* al = new vi[n];
	for (int i = 0; i < n - 1; ++i)
	{
		cin >> e[i].first >> e[i].second;
		--e[i].first;
		--e[i].second;
		al[e[i].first].push_back(e[i].second);
		al[e[i].second].push_back(e[i].first);
	}

	vi c(n);
	for (int i = 0; i < n; ++i)
		cin >> c[i];

	groups.resize(n);
	for (int i = 0; i < n; ++i)
		groups[i] = i;

	for (int i = 0; i < n - 1; ++i)
	{
		int u = e[i].first;
		int v = e[i].second;
		if (c[u] == c[v])
		{
			int u_group = get_group(u);
			int v_group = get_group(v);
			groups[max(u_group, v_group)] = min(u_group, v_group);
		}
	}

	int res = -1;
	bool ok = true;
	for (int i = 0; i < n; ++i)
	{
		int others = 0;
		for (int j = 0, m = al[i].size(); j < m; ++j)
		{
			if (get_group(al[i][j]) != get_group(i))
			{
				++others;
				if (others > 1)
				{
					if (res == -1)
					{
						res = i;
					}
					else
					{
						ok = false;
					}
					break;
				}
			}
		}
		if (!ok)
			break;
	}

	if (ok)
	{
		cout << "YES" << endl;
		cout << res + 1 << endl;
	}
	else
	{
		cout << "NO" << endl;
	}
	return 0;
}


#include <algorithm>
#include <bitset>
#include <cassert>
#include <deque>
#include <queue>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <float.h>
#include <limits>
#include <list>
#include <map>
#include <math.h>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<int, int> pi;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pi> vp;
typedef vector<string> vs;
typedef set<int> si;
typedef map<int, int> mi;
typedef multimap<int, int> mmi;

vi groups;

int get_group(int u)
{
	while (u != groups[u])
		u = groups[u];
	return u;
}

int main()
{
	ios::sync_with_stdio(false);

	int n;
	cin >> n;

	vp e(n - 1);
	vi* al = new vi[n];
	for (int i = 0; i < n - 1; ++i)
	{
		cin >> e[i].first >> e[i].second;
		--e[i].first;
		--e[i].second;
		al[e[i].first].push_back(e[i].second);
		al[e[i].second].push_back(e[i].first);
	}

	vi c(n);
	for (int i = 0; i < n; ++i)
		cin >> c[i];

	groups.resize(n);
	for (int i = 0; i < n; ++i)
		groups[i] = i;

	for (int i = 0; i < n - 1; ++i)
	{
		int u = e[i].first;
		int v = e[i].second;
		if (c[u] == c[v])
		{
			int u_group = get_group(u);
			int v_group = get_group(v);
			groups[max(u_group, v_group)] = min(u_group, v_group);
		}
	}

	int res = -1;
	bool ok = true;
	for (int i = 0; i < n; ++i)
	{
		int others = 0;
		for (int j = 0, m = al[i].size(); j < m; ++j)
		{
			if (get_group(al[i][j]) != get_group(i))
			{
				++others;
				if (others > 1)
				{
					if (res == -1)
					{
						res = i;
					}
					else
					{
						ok = false;
					}
					break;
				}
			}
		}
		if (!ok)
			break;
	}

	if (ok)
	{
		cout << "YES" << endl;
		cout << (res == -1 ? 1 : res + 1) << endl;
	}
	else
	{
		cout << "NO" << endl;
	}
	return 0;
}


#include <algorithm>
#include <bitset>
#include <cassert>
#include <deque>
#include <queue>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <float.h>
#include <limits>
#include <list>
#include <map>
#include <math.h>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<int, int> pi;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pi> vp;
typedef vector<string> vs;
typedef set<int> si;
typedef map<int, int> mi;
typedef multimap<int, int> mmi;

vi groups;

int get_group(int u)
{
	while (u != groups[u])
		u = groups[u];
	return u;
}

int main()
{
	ios::sync_with_stdio(false);

	int n;
	cin >> n;

	vp e(n - 1);
	vi* al = new vi[n];
	for (int i = 0; i < n - 1; ++i)
	{
		cin >> e[i].first >> e[i].second;
		--e[i].first;
		--e[i].second;
		al[e[i].first].push_back(e[i].second);
		al[e[i].second].push_back(e[i].first);
	}

	vi c(n);
	for (int i = 0; i < n; ++i)
		cin >> c[i];

	groups.resize(n);
	for (int i = 0; i < n; ++i)
		groups[i] = i;

	for (int i = 0; i < n - 1; ++i)
	{
		int u = e[i].first;
		int v = e[i].second;
		if (c[u] == c[v])
		{
			int u_group = get_group(u);
			int v_group = get_group(v);
			groups[max(u_group, v_group)] = min(u_group, v_group);
		}
	}

	vi cnt(n);
	for (int i = 0; i < n; ++i)
	{
		int group_i = get_group(i);
		for (int j = 0, m = al[i].size(); j < m; ++j)
		{
			int group_j = get_group(al[i][j]);
			if (group_j != group_i)
				++cnt[i];
		}
	}
	int res = -1;
	int total = 0;
	bool ok = true;
	for (int i = 0; i < n; ++i)
	{
		if (cnt[i] > 1)
		{
			if (res == -1)
			{
				res = i;
			}
			else
			{
				ok = false;
				break;
			}
		}
		total += cnt[i];
	}

	if (!ok)
	{
		cout << "NO" << endl;
		return 0;
	}

	if (res != -1 && cnt[res] * 2 != total)
	{
		cout << "NO" << endl;
		return 0;
	}

	if (res == -1)
	{
		for (int i = 0; i < n; ++i)
		{
			if (cnt[i] == 1)
			{
				res = i;
				break;
			}
		}
	}

	cout << "YES" << endl;
	cout << (res == -1 ? 1 : res + 1) << endl;

	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include<cstdio>
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<numeric>
#include<set>
#include<map>
#include<time.h>
#include<cmath>
#include<queue>
#define li long long
#define mp make_pair

using namespace std;

int main() {
	//freopen("subgraph.in", "r", stdin);
	//freopen("subgraph.out", "w", stdout);
	int n;
	cin >> n;
	vector<pair<int, int>> e(n - 1);
	vector<int> c(n);
	for (int i = 0; i < n - 1; i++) {
		cin >> e[i].first >> e[i].second;
		e[i].first--;
		e[i].second--;
	}
	for (int i = 0; i < n; i++)
		cin >> c[i];
	int f = 0,a,b;
	for (int i = 0; i < n - 1; i++)
		if (c[e[i].first] != c[e[i].second]) {
			if (!f) {
				a = e[i].first;
				b = e[i].second;
				f++;
			}
			else if (f == 1) {
				if (a == e[i].first || a == e[i].second)
					f = 2;
				else if (b == e[i].first || b == e[i].second) {
					f = 2;
					a = b;
				}
				else
					f = 4;
			}
			else if (f == 2) {
				if (a == e[i].first || a == e[i].second)
					f = 2;
				else
					f = 4;
			}
		}
	if (f < 4)
		cout << "YES" << endl << a+1;
	else
		cout << "NO";
}


#define _CRT_SECURE_NO_WARNINGS
#include<cstdio>
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<numeric>
#include<set>
#include<map>
#include<time.h>
#include<cmath>
#include<queue>
#define li long long
#define mp make_pair

using namespace std;

int main() {
	//freopen("subgraph.in", "r", stdin);
	//freopen("subgraph.out", "w", stdout);
	int n;
	cin >> n;
	vector<pair<int, int>> e(n - 1);
	vector<int> c(n);
	for (int i = 0; i < n - 1; i++) {
		cin >> e[i].first >> e[i].second;
		e[i].first--;
		e[i].second--;
	}
	for (int i = 0; i < n; i++)
		cin >> c[i];
	int f = 0,a = 0,b;
	for (int i = 0; i < n - 1; i++)
		if (c[e[i].first] != c[e[i].second]) {
			if (!f) {
				a = e[i].first;
				b = e[i].second;
				f++;
			}
			else if (f == 1) {
				if (a == e[i].first || a == e[i].second)
					f = 2;
				else if (b == e[i].first || b == e[i].second) {
					f = 2;
					a = b;
				}
				else
					f = 4;
			}
			else if (f == 2) {
				if (a == e[i].first || a == e[i].second)
					f = 2;
				else
					f = 4;
			}
		}
	if (f < 4)
		cout << "YES" << endl << a+1;
	else
		cout << "NO";
}


#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;

struct tree
{
	int a,b;
};

struct tree ed[100100];
int cnt[100100],color[100100];
int main()
{
	int n;
	cin >> n;
	int sum = 0;
	for(int i = 1;i < n;i++)
	{
		cin >> ed[i].a >> ed[i].b;
	}
	for(int i = 1;i <= n;i++)
	{
		cin >> color[i];
	}
	memset(cnt,0,sizeof(cnt));
	for(int i = 1;i < n;i++)
	{
		if(color[ed[i].a] != color[ed[i].b])
		{
			cnt[ed[i].a]++;
			cnt[ed[i].b]++;
			sum++;
		}
	}
	int flag = 0;
	for(int i = 1;i <= n;i++)
	{
		if(cnt[i] == sum)
		{
			printf("YES\n%d\n",i);
			flag = 1;
			break;
		}
	}
	if(!flag) printf("NO\n");
	return 0;
}


#include <assert.h>
#include <map>
#include <limits>
#include <iostream>
#include <cstdlib>
#include <vector>
#include <cmath>
#include <math.h>
#include <stdio.h>
#include <string>
#include <cstring>
#include <set>
#include <sstream>
#include <algorithm>
#include <utility>
#include <ctime>
#include <stack>
using namespace std;
const long long INF = 1000000000;
const double eps = 0.000000001;
void dfs(int s, vector<char>& used, vector<vector<int>> gr, vector <int>c,int pred,const int start,bool &can){
	if (used[s] != 0){
		return ;
	}
	++used[s];
	if (pred != 0 && pred != start){
		if (c[s - 1] != c[pred - 1]){
			can = false;
			//return;
		}
	}
	for (int i = 0; i < gr[s].size(); ++i){

		dfs(gr[s][i], used, gr, c,s,start,can);

	}
}
int main(){
	int n, m,z,u,v;
	cin >> n;
	vector <vector<int> > gr(n + 13);
	vector <int> c;
	for (int i = 1; i < n; ++i){
		cin >> u >> v;
		gr[v].push_back(u);
		gr[u].push_back(v);
	}
	vector <char> used(n + 13, 0);
	for (int i = 0; i < n; ++i){
		cin >> z;
		c.push_back(z);
	}
	pair <int, int> diff;
	bool IsSame = true;
	for (int i = 1; i < n; ++i){
		for (int j = 0; j < gr[i].size(); ++j){
			if (c[i - 1] != c[gr[i][j] - 1]){
				IsSame = false;
				diff.first = i;
				diff.second = gr[i][j];
				break;
			}
		}
		if (!IsSame)break;
	}
	if (IsSame){
		cout << "YES" << endl;
		cout << 1;
		return 0;
	}

	bool can1 = true;
	bool can2 = true;
	IsSame = false;
	vector <char> used1(n + 13, 0);
	vector <char> used2(n + 13, 0);
	dfs(diff.first, used1, gr, c, 0, diff.first, can1);
	dfs(diff.second, used2, gr, c, 0, diff.second, can2);
	if (can1||can2){
		cout << "YES"<<endl;
		if (can1)cout << diff.first;
		else cout << diff.second;
		return 0;
	}
	cout << "NO";

	return 0;
}


/*#include <assert.h>
#include <map>
#include <limits>
#include <iostream>
#include <cstdlib>
#include <vector>
#include <cmath>
#include <math.h>
#include <stdio.h>
#include <string>
#include <cstring>
#include <set>
#include <sstream>
#include <algorithm>
#include <utility>
#include <ctime>
#include <stack>
using namespace std;
const long long INF = 1000000000;
const double eps = 0.00000000001;
bool can(int speed,int ltime, int time, int x){
	if (((x + 0.0) / speed) <= time && ((x + 0.0) / speed) >= ltime){
			return true;
		}
		else
		return false;
}
int main(){
	int a, b, x;
	cin >> a >> b >> x;
	double res1 = 10010.0;
	double res2 = 10010.0;
	int time1 = 0;
	int time2 = 0;
	int it1 = 0;
	int it2 = 0;
	double l = 0.0;
	double r = 1000.0;
	int ltime1 = 0;
	int ltime2 = 0;
	while (res1 - 1000>eps && time1<=100){
		if (it1 != 0){
			ltime1 += time1;
		}
		if (it1 % 2 == 0){
			time1 += a;
		}
		else{
			time1 += b;
		}
		++it1;
		bool f1 = false;
		while (r - l > eps){
			double m = (l + r) / 2;
			if (can(m, ltime1, time1, x)){
				r = m;
				f1 = true;
			}
			else{
				l = m;
			}
		}
		if (f1){
			res1 = (l + r) / 2;
		}

	}
	l = 0.0;
	r = 1000.0;
	while (res2- 1000>eps && time2<=100){
		if (it2 != 0){
			ltime2 += time2;
		}
		if (it2 % 2 == 0){
			time2 += b;
		}
		else{
			time2 += a;
		}
		++it2;
		bool f2 = false;
		while (r - l > eps){
			double m = (l + r) / 2;
			if (can(m, ltime2, time2, x)){
				r = m;
				f2 = true;
			}
			else{
				l = m;
			}
		}
		if (f2){
			res2 = (l + r) / 2;
		}
	}
	cout.setf(ios::fixed);

	if (res1 > 1000){
		res1 = 0;
	}
	if (res2 > 1000){
		res2 = 0;
	}
	if (res1 < res2){
		cout.precision(10);
		cout << res2;
	}
	else{
		cout.precision(10);
		cout << res1;
	}
	return 0;
	/*int a, b, x;
	cin >> a >> b >> x;
	double res1 = 10010.0;
	double res2 = 10010.0;
	int time1 = 0;
	int time2 = 0;
	int it1 = 0;
	int it2 = 0;
	while (res1 > 1000){
		if (it1 % 2 == 0){
			time1 += a;
		}
		else{
			time1 += b;
		}
		++it1;
		res1 = (x + 0.0) / time1;
	}
	while (res2 > 1000){
		if (it2 % 2 == 0){
			time2 += b;
		}
		else{
			time2 += a;
		}
		++it2;
		res2 = (x + 0.0) / time2;
	}
	cout.setf(ios::fixed);

	if (res1 > res2){
		cout.precision(10);
		cout << res1;
	}
	else{
		cout.precision(10);
		cout << res2;
	}
	return 0;*/
//}
#include <assert.h>
#include <map>
#include <limits>
#include <iostream>
#include <cstdlib>
#include <vector>
#include <cmath>
#include <math.h>
#include <stdio.h>
#include <string>
#include <cstring>
#include <set>
#include <sstream>
#include <algorithm>
#include <utility>
#include <ctime>
#include <stack>
using namespace std;
const long long INF = 1000000000;
const double eps = 0.000000001;
void dfs(int s, vector<char>& used, vector<vector<int>> gr, vector <int>c,int pred,const int start,bool &can){
	if (used[s] != 0){
		return ;
	}
	++used[s];
	if (pred != 0 && pred != start){
		if (c[s - 1] != c[pred - 1]){
			can = false;
			return;
		}
	}
	for (int i = 0; i < gr[s].size(); ++i){

		dfs(gr[s][i], used, gr, c,s,start,can);

	}

}
int main(){
	int n, m,z,u,v;
	cin >> n;
	vector <vector<int> > gr(n + 13);
	vector <int> c;
	for (int i = 1; i < n; ++i){
		cin >> u >> v;
		gr[v].push_back(u);
		gr[u].push_back(v);
	}
	vector <char> used(n + 13, 0);
	for (int i = 0; i < n; ++i){
		cin >> z;
		c.push_back(z);
	}
	int l = 1, r = n;
	bool IsSame = true;
	for (int i = 2; i <= n; ++i){
		bool can = true;
		vector <char> used(n + 13, 0);
		if (c[i] != c[i - 1]){
			IsSame = false;
			dfs(i, used, gr, c, 0, i, can);
		}
		if (can){
			cout << "YES"<<endl;
			cout << i;
			return 0;
		}
		if (!IsSame){
			cout << "NO";
			return 0;
		}
	}
	if (IsSame){
		cout << "YES"<<endl;
		cout << 1;
		return 0;
	}
	cout << "NO";

	return 0;
}
/*
4
1 2
2 3
3 4
1 2 1 2
*/


#include <assert.h>
#include <map>
#include <limits>
#include <iostream>
#include <cstdlib>
#include <vector>
#include <cmath>
#include <math.h>
#include <stdio.h>
#include <string>
#include <cstring>
#include <set>
#include <sstream>
#include <algorithm>
#include <utility>
#include <ctime>
#include <stack>
using namespace std;
const long long INF = 1000000000;
const double eps = 0.000000001;
vector <vector <int> > gr(100013);
vector <char> used1(100013);
vector <char> used2(100013);
vector <int> c;
void dfs1(int s, int pred, const int start, bool &can){

	if (used1[s] != 0){
		return;
	}
	++used1[s];
	if (pred != 0 && pred != start){
		if (c[s - 1] != c[pred - 1]){
			can = false;
			//return;
		}
	}
	for (int i = 0; i < gr[s].size(); ++i){

		dfs1(gr[s][i],  s, start, can);

	}
}
void dfs2(int s, int pred, const int start, bool &can){

	if (used2[s] != 0){
		return;
	}
	++used2[s];
	if (pred != 0 && pred != start){
		if (c[s - 1] != c[pred - 1]){
			can = false;
			//return;
		}
	}
	for (int i = 0; i < gr[s].size(); ++i){

		dfs2(gr[s][i], s, start, can);

	}
}
int main(){
	int n, m, z, u, v;
	cin >> n;

	for (int i = 1; i < n; ++i){
		cin >> u >> v;
		gr[v].push_back(u);
		gr[u].push_back(v);
	}
	vector <char> used(n + 13);
	for (int i = 0; i < n; ++i){
		cin >> z;
		c.push_back(z);
	}
	pair <int, int> diff;
	bool IsSame = true;
	for (int i = 1; i < n; ++i){
		for (int j = 0; j < gr[i].size(); ++j){
			if (c[i - 1] != c[gr[i][j] - 1]){
				IsSame = false;
				diff.first = i;
				diff.second = gr[i][j];
				break;
			}
		}
		if (!IsSame)break;
	}
	if (IsSame){
		cout << "YES" << endl;
		cout << 1;
		return 0;
	}

	bool can1 = true;
	bool can2 = true;
	IsSame = false;

	dfs1(diff.first,  0, diff.first, can1);
	dfs2(diff.second,  0, diff.second, can2);
	if (can1 || can2){
		cout << "YES" << endl;
		if (can1)cout << diff.first;
		else cout << diff.second;
		return 0;
	}
	cout << "NO";

	return 0;
}


/*#include <assert.h>
#include <map>
#include <limits>
#include <iostream>
#include <cstdlib>
#include <vector>
#include <cmath>
#include <math.h>
#include <stdio.h>
#include <string>
#include <cstring>
#include <set>
#include <sstream>
#include <algorithm>
#include <utility>
#include <ctime>
#include <stack>
using namespace std;
const long long INF = 1000000000;
const double eps = 0.00000000001;
bool can(int speed,int ltime, int time, int x){
	if (((x + 0.0) / speed) <= time && ((x + 0.0) / speed) >= ltime){
			return true;
		}
		else
		return false;
}
int main(){
	int a, b, x;
	cin >> a >> b >> x;
	double res1 = 10010.0;
	double res2 = 10010.0;
	int time1 = 0;
	int time2 = 0;
	int it1 = 0;
	int it2 = 0;
	double l = 0.0;
	double r = 1000.0;
	int ltime1 = 0;
	int ltime2 = 0;
	while (res1 - 1000>eps && time1<=100){
		if (it1 != 0){
			ltime1 += time1;
		}
		if (it1 % 2 == 0){
			time1 += a;
		}
		else{
			time1 += b;
		}
		++it1;
		bool f1 = false;
		while (r - l > eps){
			double m = (l + r) / 2;
			if (can(m, ltime1, time1, x)){
				r = m;
				f1 = true;
			}
			else{
				l = m;
			}
		}
		if (f1){
			res1 = (l + r) / 2;
		}

	}
	l = 0.0;
	r = 1000.0;
	while (res2- 1000>eps && time2<=100){
		if (it2 != 0){
			ltime2 += time2;
		}
		if (it2 % 2 == 0){
			time2 += b;
		}
		else{
			time2 += a;
		}
		++it2;
		bool f2 = false;
		while (r - l > eps){
			double m = (l + r) / 2;
			if (can(m, ltime2, time2, x)){
				r = m;
				f2 = true;
			}
			else{
				l = m;
			}
		}
		if (f2){
			res2 = (l + r) / 2;
		}
	}
	cout.setf(ios::fixed);

	if (res1 > 1000){
		res1 = 0;
	}
	if (res2 > 1000){
		res2 = 0;
	}
	if (res1 < res2){
		cout.precision(10);
		cout << res2;
	}
	else{
		cout.precision(10);
		cout << res1;
	}
	return 0;
	/*int a, b, x;
	cin >> a >> b >> x;
	double res1 = 10010.0;
	double res2 = 10010.0;
	int time1 = 0;
	int time2 = 0;
	int it1 = 0;
	int it2 = 0;
	while (res1 > 1000){
		if (it1 % 2 == 0){
			time1 += a;
		}
		else{
			time1 += b;
		}
		++it1;
		res1 = (x + 0.0) / time1;
	}
	while (res2 > 1000){
		if (it2 % 2 == 0){
			time2 += b;
		}
		else{
			time2 += a;
		}
		++it2;
		res2 = (x + 0.0) / time2;
	}
	cout.setf(ios::fixed);

	if (res1 > res2){
		cout.precision(10);
		cout << res1;
	}
	else{
		cout.precision(10);
		cout << res2;
	}
	return 0;*/
//}
#include <assert.h>
#include <map>
#include <limits>
#include <iostream>
#include <cstdlib>
#include <vector>
#include <cmath>
#include <math.h>
#include <stdio.h>
#include <string>
#include <cstring>
#include <set>
#include <sstream>
#include <algorithm>
#include <utility>
#include <ctime>
#include <stack>
using namespace std;
const long long INF = 1000000000;
const double eps = 0.000000001;

void dfs(int s, vector<char>& used, vector<vector<int>>& gr, vector <int>c, int pred, const int start, bool &can){

	if (used[s] != 0){
		return ;
	}
	++used[s];
	if (pred != 0 && pred != start){
		if (c[s - 1] != c[pred - 1]){
			can = false;
			//return;
		}
	}
	for (int i = 0; i < gr[s].size(); ++i){

		dfs(gr[s][i], used, gr, c,s,start,can);

	}
}
int main(){
	int n, m,z,u,v;
	cin >> n;
	vector <int> c;
	for (int i = 1; i < n; ++i){
		cin >> u >> v;
		gr[v].push_back(u);
		gr[u].push_back(v);
	}
	vector <char> used(n + 13);
	for (int i = 0; i < n; ++i){
		cin >> z;
		c.push_back(z);
	}
	pair <int, int> diff;
	bool IsSame = true;
	for (int i = 1; i < n; ++i){
		for (int j = 0; j < gr[i].size(); ++j){
			if (c[i - 1] != c[gr[i][j] - 1]){
				IsSame = false;
				diff.first = i;
				diff.second = gr[i][j];
				break;
			}
		}
		if (!IsSame)break;
	}
	if (IsSame){
		cout << "YES" << endl;
		cout << 1;
		return 0;
	}

	bool can1 = true;
	bool can2 = true;
	IsSame = false;
	vector <char> used1(n + 13);
	vector <char> used2(n + 13);
	dfs(diff.first, used1, gr, c, 0, diff.first, can1);
	dfs(diff.second, used2, gr, c, 0, diff.second, can2);
	if (can1||can2){
		cout << "YES"<<endl;
		if (can1)cout << diff.first;
		else cout << diff.second;
		return 0;
	}
	cout << "NO";

	return 0;
}
/*
4
1 2
2 3
3 4
1 2 1 2
*/


/*#include <assert.h>
#include <map>
#include <limits>
#include <iostream>
#include <cstdlib>
#include <vector>
#include <cmath>
#include <math.h>
#include <stdio.h>
#include <string>
#include <cstring>
#include <set>
#include <sstream>
#include <algorithm>
#include <utility>
#include <ctime>
#include <stack>
using namespace std;
const long long INF = 1000000000;
const double eps = 0.00000000001;
bool can(int speed,int ltime, int time, int x){
	if (((x + 0.0) / speed) <= time && ((x + 0.0) / speed) >= ltime){
			return true;
		}
		else
		return false;
}
int main(){
	int a, b, x;
	cin >> a >> b >> x;
	double res1 = 10010.0;
	double res2 = 10010.0;
	int time1 = 0;
	int time2 = 0;
	int it1 = 0;
	int it2 = 0;
	double l = 0.0;
	double r = 1000.0;
	int ltime1 = 0;
	int ltime2 = 0;
	while (res1 - 1000>eps && time1<=100){
		if (it1 != 0){
			ltime1 += time1;
		}
		if (it1 % 2 == 0){
			time1 += a;
		}
		else{
			time1 += b;
		}
		++it1;
		bool f1 = false;
		while (r - l > eps){
			double m = (l + r) / 2;
			if (can(m, ltime1, time1, x)){
				r = m;
				f1 = true;
			}
			else{
				l = m;
			}
		}
		if (f1){
			res1 = (l + r) / 2;
		}

	}
	l = 0.0;
	r = 1000.0;
	while (res2- 1000>eps && time2<=100){
		if (it2 != 0){
			ltime2 += time2;
		}
		if (it2 % 2 == 0){
			time2 += b;
		}
		else{
			time2 += a;
		}
		++it2;
		bool f2 = false;
		while (r - l > eps){
			double m = (l + r) / 2;
			if (can(m, ltime2, time2, x)){
				r = m;
				f2 = true;
			}
			else{
				l = m;
			}
		}
		if (f2){
			res2 = (l + r) / 2;
		}
	}
	cout.setf(ios::fixed);

	if (res1 > 1000){
		res1 = 0;
	}
	if (res2 > 1000){
		res2 = 0;
	}
	if (res1 < res2){
		cout.precision(10);
		cout << res2;
	}
	else{
		cout.precision(10);
		cout << res1;
	}
	return 0;
	/*int a, b, x;
	cin >> a >> b >> x;
	double res1 = 10010.0;
	double res2 = 10010.0;
	int time1 = 0;
	int time2 = 0;
	int it1 = 0;
	int it2 = 0;
	while (res1 > 1000){
		if (it1 % 2 == 0){
			time1 += a;
		}
		else{
			time1 += b;
		}
		++it1;
		res1 = (x + 0.0) / time1;
	}
	while (res2 > 1000){
		if (it2 % 2 == 0){
			time2 += b;
		}
		else{
			time2 += a;
		}
		++it2;
		res2 = (x + 0.0) / time2;
	}
	cout.setf(ios::fixed);

	if (res1 > res2){
		cout.precision(10);
		cout << res1;
	}
	else{
		cout.precision(10);
		cout << res2;
	}
	return 0;*/
//}
#include <assert.h>
#include <map>
#include <limits>
#include <iostream>
#include <cstdlib>
#include <vector>
#include <cmath>
#include <math.h>
#include <stdio.h>
#include <string>
#include <cstring>
#include <set>
#include <sstream>
#include <algorithm>
#include <utility>
#include <ctime>
#include <stack>
using namespace std;
const long long INF = 1000000000;
const double eps = 0.000000001;
void dfs(int s, vector<char>& used, vector<vector<int>> gr, vector <int>c,int pred,const int start,bool &can){
	if (used[s] != 0){
		return ;
	}
	++used[s];
	if (pred != 0 && pred != start){
		if (c[s - 1] != c[pred - 1]){
			can = false;
			return;
		}
	}
	for (int i = 0; i < gr[s].size(); ++i){

		dfs(gr[s][i], used, gr, c,s,start,can);

	}

}
int main(){
	int n, m,z,u,v;
	cin >> n;
	vector <vector<int> > gr(n + 13);
	vector <int> c;
	for (int i = 1; i < n; ++i){
		cin >> u >> v;
		gr[v].push_back(u);
		gr[u].push_back(v);
	}
	vector <char> used(n + 13, 0);
	for (int i = 0; i < n; ++i){
		cin >> z;
		c.push_back(z);
	}
	int l = 1, r = n;
	bool IsSame = true;
	for (int i = 2; i <= n; ++i){
		bool can = true;
		vector <char> used(n + 13, 0);
		if (c[i] != c[i - 1]){
			IsSame = false;
			dfs(i, used, gr, c, 0, i, can);
			dfs(i-1, used, gr, c, 0, i, can);
		}
		if (can){
			cout << "YES"<<endl;
			cout << i;
			return 0;
		}
		if (!IsSame){
			cout << "NO";
			return 0;
		}
	}
	if (IsSame){
		cout << "YES"<<endl;
		cout << 1;
		return 0;
	}
	cout << "NO";

	return 0;
}
/*
4
1 2
2 3
3 4
1 2 1 2
*/


/*#include <assert.h>
#include <map>
#include <limits>
#include <iostream>
#include <cstdlib>
#include <vector>
#include <cmath>
#include <math.h>
#include <stdio.h>
#include <string>
#include <cstring>
#include <set>
#include <sstream>
#include <algorithm>
#include <utility>
#include <ctime>
#include <stack>
using namespace std;
const long long INF = 1000000000;
const double eps = 0.00000000001;
bool can(int speed,int ltime, int time, int x){
	if (((x + 0.0) / speed) <= time && ((x + 0.0) / speed) >= ltime){
			return true;
		}
		else
		return false;
}
int main(){
	int a, b, x;
	cin >> a >> b >> x;
	double res1 = 10010.0;
	double res2 = 10010.0;
	int time1 = 0;
	int time2 = 0;
	int it1 = 0;
	int it2 = 0;
	double l = 0.0;
	double r = 1000.0;
	int ltime1 = 0;
	int ltime2 = 0;
	while (res1 - 1000>eps && time1<=100){
		if (it1 != 0){
			ltime1 += time1;
		}
		if (it1 % 2 == 0){
			time1 += a;
		}
		else{
			time1 += b;
		}
		++it1;
		bool f1 = false;
		while (r - l > eps){
			double m = (l + r) / 2;
			if (can(m, ltime1, time1, x)){
				r = m;
				f1 = true;
			}
			else{
				l = m;
			}
		}
		if (f1){
			res1 = (l + r) / 2;
		}

	}
	l = 0.0;
	r = 1000.0;
	while (res2- 1000>eps && time2<=100){
		if (it2 != 0){
			ltime2 += time2;
		}
		if (it2 % 2 == 0){
			time2 += b;
		}
		else{
			time2 += a;
		}
		++it2;
		bool f2 = false;
		while (r - l > eps){
			double m = (l + r) / 2;
			if (can(m, ltime2, time2, x)){
				r = m;
				f2 = true;
			}
			else{
				l = m;
			}
		}
		if (f2){
			res2 = (l + r) / 2;
		}
	}
	cout.setf(ios::fixed);

	if (res1 > 1000){
		res1 = 0;
	}
	if (res2 > 1000){
		res2 = 0;
	}
	if (res1 < res2){
		cout.precision(10);
		cout << res2;
	}
	else{
		cout.precision(10);
		cout << res1;
	}
	return 0;
	/*int a, b, x;
	cin >> a >> b >> x;
	double res1 = 10010.0;
	double res2 = 10010.0;
	int time1 = 0;
	int time2 = 0;
	int it1 = 0;
	int it2 = 0;
	while (res1 > 1000){
		if (it1 % 2 == 0){
			time1 += a;
		}
		else{
			time1 += b;
		}
		++it1;
		res1 = (x + 0.0) / time1;
	}
	while (res2 > 1000){
		if (it2 % 2 == 0){
			time2 += b;
		}
		else{
			time2 += a;
		}
		++it2;
		res2 = (x + 0.0) / time2;
	}
	cout.setf(ios::fixed);

	if (res1 > res2){
		cout.precision(10);
		cout << res1;
	}
	else{
		cout.precision(10);
		cout << res2;
	}
	return 0;*/
//}
#include <assert.h>
#include <map>
#include <limits>
#include <iostream>
#include <cstdlib>
#include <vector>
#include <cmath>
#include <math.h>
#include <stdio.h>
#include <string>
#include <cstring>
#include <set>
#include <sstream>
#include <algorithm>
#include <utility>
#include <ctime>
#include <stack>
using namespace std;
const long long INF = 1000000000;
const double eps = 0.000000001;
void dfs(int s, vector<char>& used, vector<vector<int>> gr, vector <int>c,int pred,const int start,bool &can){
	if (used[s] != 0){
		return ;
	}
	++used[s];
	if (pred != 0 && pred != start){
		if (c[s - 1] != c[pred - 1]){
			can = false;
		}
	}
	for (int i = 0; i < gr[s].size(); ++i){

		dfs(gr[s][i], used, gr, c,s,start,can);

	}

}
int main(){
	int n, m,z,u,v;
	cin >> n;
	vector <vector<int> > gr(n + 13);
	vector <int> c;
	for (int i = 1; i < n; ++i){
		cin >> u >> v;
		gr[v].push_back(u);
		gr[u].push_back(v);
	}
	vector <char> used(n + 13, 0);
	for (int i = 0; i < n; ++i){
		cin >> z;
		c.push_back(z);
	}
	for (int i = 1; i <= n; ++i){
		bool can = true;
		vector <char> used(n + 13, 0);
		dfs(i, used, gr, c, 0, i, can);
		if (can){
			cout << "YES"<<endl;
			cout << i;
			return 0;
		}
	}
	cout << "NO";

	return 0;
}
/*
4
1 2
2 3
3 4
1 2 1 2
*/