☆【CodeForces - 764C】Timofey and a tree (思维题,树的性质)

题干:

Each New Year Timofey and his friends cut down a tree of n vertices and bring it home. After that they paint all the n its vertices, so that the i-th vertex gets color ci.

Now it's time for Timofey birthday, and his mother asked him to remove the tree. Timofey removes the tree in the following way: he takes some vertex in hands, while all the other vertices move down so that the tree becomes rooted at the chosen vertex. After that Timofey brings the tree to a trash can.

Timofey doesn't like it when many colors are mixing together. A subtree annoys him if there are vertices of different color in it. Timofey wants to find a vertex which he should take in hands so that there are no subtrees that annoy him. He doesn't consider the whole tree as a subtree since he can't see the color of the root vertex.

A subtree of some vertex is a subgraph containing that vertex and all its descendants.

Your task is to determine if there is a vertex, taking which in hands Timofey wouldn't be annoyed.

Input

The first line contains single integer n (2 ≤ n ≤ 105) — the number of vertices in the tree.

Each of the next n - 1 lines contains two integers u and v (1 ≤ u, v ≤ nu ≠ v), denoting there is an edge between vertices u and v. It is guaranteed that the given graph is a tree.

The next line contains n integers c1, c2, ..., cn (1 ≤ ci ≤ 105), denoting the colors of the vertices.

Output

Print "NO" in a single line, if Timofey can't take the tree in such a way that it doesn't annoy him.

Otherwise print "YES" in the first line. In the second line print the index of the vertex which Timofey should take in hands. If there are multiple answers, print any of them.

Examples

Input

4
1 2
2 3
3 4
1 2 1 1

Output

YES
2

Input

3
1 2
2 3
1 2 3

Output

YES
2

Input

4
1 2
2 3
3 4
1 2 1 2

Output

NO

题目大意:

    给定一棵每个顶点都标记颜色的树(无根树),问能否从一个顶点出发(以他为根节点),使得它的子树都是一样颜色(不包括这个顶点本身的颜色)详见第二个样例。

题解:因条件要求,我们可以知道不同的颜色的顶点中其中一个必须是答案顶点,我们统计一下不同颜色顶点个数(设为m),和不同颜色顶点中顶点出现次数。最后如果有一个顶点出现次数等于m,那么这个顶点就是答案顶点。

解题报告:

    思维题啊害死我、、、

    第一反应子树,我擦dfs序吧?

    第二反应不行啊总不能对每一个顶点建树吧?要不试试、、

    第三反应woc超时了,,,    

    关于图论的问题,可以考虑cnt计数、、、这题sum表示每一条边中有多少条边是两节点颜色不一样的。如果正好和节点 i 的边数相同,那 i 就是答案。

AC代码:

#include<bits/stdc++.h>

using namespace std;
const int MAX = 1e5 + 5 ;
int u[MAX],v[MAX],cnt[MAX],col[MAX];
int main()
{
	int n,sum=0;
	cin>>n;
	for(int i = 1; i<n; i++) {
		scanf("%d%d",&u[i],&v[i]);
	}
	for(int i = 1; i<=n; i++) {
		scanf("%d",&col[i]);
	}
	for(int i = 1; i<n; i++) {
		if(col[u[i]]!=col[v[i]]) {
			sum++;cnt[u[i]]++;cnt[v[i]]++;
		}
	}
	for(int i = 1; i<=n; i++) {
		if(cnt[i] == sum) {
			printf("YES\n%d\n",i);
			return 0 ;
		}
	}
	printf("NO\n");
	return 0 ;
}

dfs序超时代码:(刚开始是o(n)的注释掉那一部分,后来改成logn的线段树,但是还是超时,看来时间都花在dfs序上了)

#include<bits/stdc++.h>

using namespace std;
const int MAX = 100000 +5;
const int INF = 0x3f3f3f3f;
int cnt,id;
int st[MAX],ed[MAX],col[MAX],q[MAX];
int head[MAX];
struct Edge{
	int to,ne;
}e[1000000 +  5];
struct TREE {
	int l,r;
	int maxx,minn;
} tree[MAX * 4];
void dfs(int x,int fa)
{
    q[++id]=x;
    st[x]=id;
    for(int i=head[x];i!=-1;i=e[i].ne)
        if(e[i].to!=fa)
            dfs(e[i].to,x);
    ed[x]=id;
}
void pushup(int cur) {
	tree[cur].maxx = max(tree[cur*2].maxx,tree[cur*2+1].maxx);
	tree[cur].minn = min(tree[cur*2].minn,tree[cur*2+1].minn);
}
void build(int l,int r,int cur) {
	tree[cur].l=l;
	tree[cur].r=r;
	if(l == r) {
		tree[cur].maxx = col[q[r]];
		tree[cur].minn = col[q[r]];
		return ;
	}
	int m = (l + r)/2;
	build(l,m,cur*2);
	build(m+1,r,cur*2+1);
	pushup(cur);
}
int qmax(int pl,int pr,int cur) {
	if(pl <= tree[cur].l && pr >= tree[cur].r) return tree[cur].maxx;
	int ll=0,rr=0;
	if(pl <= tree[cur*2].r) ll = qmax(pl,pr,cur*2);
	if(pr >= tree[cur*2+1].l) rr = qmax(pl,pr,cur*2+1);
	return max(rr,ll);
}
int qmin(int pl,int pr,int cur) {
	if(pl <= tree[cur].l && pr >= tree[cur].r) return tree[cur].minn;
	int ll=INF,rr=INF;
	if(pl <= tree[cur*2].r) ll = qmin(pl,pr,cur*2);
	if(pr >= tree[cur*2+1].l) rr = qmin(pl,pr,cur*2+1);
	return min(rr,ll);
}
void add(int u,int v) {
	e[++cnt].to = v;
	e[cnt].ne = head[u];
	head[u] = cnt;
}
int main()
{
	int n,u,v;
	scanf("%d",&n);
	memset(head,-1,sizeof head);
	for(int i = 1; i<n; i++) {
		scanf("%d%d",&u,&v);
		add(u,v);add(v,u);
	}
	for(int i = 1; i<=n; i++) {
		scanf("%d",&col[i]);
	}
	//枚举每一个顶点
	
	int flag = 1; 
	for(int i = 1; i<=n; i++) {
		id=0;dfs(i,i);flag = 1;
		build(1,n,1);
		for(int j = head[i]; j!=-1; j=e[j].ne) {
			int tmp = col[e[j].to];
			int maxx = qmax(st[e[j].to],ed[e[j].to],1);
			int minn = qmin(st[e[j].to],ed[e[j].to],1);
			if(maxx != tmp || minn != tmp) flag=0;
//			for(int k = st[e[j].to]; k<=ed[e[j].to]; k++) {
//				if(col[q[k]] != tmp) {
//					flag=0;break;
//				}
//			}
			if(flag == 0) break;
		}
		if(flag == 1) {
			printf("YES\n");
			printf("%d\n",i);
			return 0;
		}
	}
	printf("NO\n");
	return 0 ;
}

或者用并查集缩图https://www.cnblogs.com/jasonlixuetao/p/6401831.html

并查集思路:
先建图,遍历每个结点,统计每个结点的度,对结点i,若其相邻的结点j与其颜色相同,那么将其合并,并将结点i和结点j的度都减1
,合并完后,所有颜色相同且相互邻接的点合并为一个集合,统计集合个数cnt,将一个集合看作一个点,在这个虚图中满足条件的点i
一定满足cnt-1==deg[i],在合并后的原图中所有点中寻找满足cnt-1==deg[i]的点。
#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++;
	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;
}

 

引用\[1\]中提到了一种形动态规划的方法来解决CodeForces - 982C问。在这个问中,subtree指的是子连通块,而不是子。为了使cnt_white - cnt_black尽可能大,可以使用两次形动态规划来求解。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的链所代表的子。在第二次遍历中,需要维护一个sum变量,用于存储链所代表的子的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。如果ans\[u\]为正,则减去dp\[v\]就是链所代表的子的权值。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] 问: CodeForces - 982C 形DP是什么问?如何解决? 回答: CodeForces - 982C是一个形动态规划问。在这个问中,需要求解子连通块的最大权值和,使得cnt_white - cnt_black尽可能大。解决这个问的方法是使用两次形动态规划。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的链所代表的子。在第二次遍历中,需要维护一个sum变量,用于存储链所代表的子的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] #### 引用[.reference_title] - *1* *2* [CodeForces - 1324F Maximum White Subtree(形dp)](https://blog.csdn.net/qq_45458915/article/details/104831678)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值