C. Timofey and a tree
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Each New Year Timofey and his friends cut down a tree of n vertices and bring it home. After that they paint all the nits 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

题意:给定一棵树,找一个点作为根节点,使得其所有子树,同一颗子树上的所有结点为同一个颜色。问是否能找到这样的结点。

看几个同学和官方题解都是dfs。结果比赛时不知咋的,硬是用并查集做出来了。。。

并查集思路:
先建图,遍历每个结点,统计每个结点的度,对结点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=; i<=n; i++)
{
deg[i].p=i;
father[i]=i;
wei[i]=;
}
for(int i=; i<n-; 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=; i<=n; i++)
scanf("%d",&col[i]);
for(int i=; i<=n; i++)
for(int j=; 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=;
for(int i=; 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=; i<=n; i++)
{
if(cnt==deg[i].d+)
{
printf("YES\n%d\n",i);
return ;
}
}
printf("NO\n");
return ;
}
dfs:感觉写的不好,用的vector,用邻接表感觉好些,一开始超时,然后开了个has数组,记忆化搜索,空间换时间。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cstring>
using namespace std;
#define N 100005 vector<int> eage[N];
int col[N];
bool vis[N],vis1[N];
int has[N]; bool dfs(int node,int color)
{
if(col[node]!=color)
return ;
//cout<<node<<'*'<<col[node]<<endl;
vis[node]=;
if(has[node]==)
return ;
if(has[node]==-)
return ;
for(int i=; i<eage[node].size(); i++)
{
if(vis[eage[node][i]]==)
if(dfs(eage[node][i],color)==)
{
has[node]=-;
return ;
}
}
has[node]=;
return ;
} bool legal(int node)
{
memset(vis,,sizeof(vis));
vis[node]=;
for(int i=; i<eage[node].size(); i++)
{
if(dfs(eage[node][i],col[eage[node][i]])==)
{
//cout<<node<<":wrong"<<endl;
return ;
}
}
//cout<<node<<":correct"<<endl;
return ;
} int main()
{
int n;
scanf("%d",&n);
int a,b;
for(int i=; i<n-; i++)
{
scanf("%d%d",&a,&b);
eage[a].push_back(b);
eage[b].push_back(a);
}
for(int i=; i<=n; i++)
scanf("%d",&col[i]);
int allsame=;
for(int i=; i<=n; i++)
for(int j=; j<eage[i].size(); j++)
{
if(col[i]!=col[eage[i][j]])
{
allsame=;
if(legal(i)&&vis1[i]==)
{
printf("YES\n%d\n",i);
return ;
}
// has[i]=-1;
vis1[i]=;
if(legal(eage[i][j])&&vis1[eage[i][j]]==)
{
printf("YES\n%d\n",eage[i][j]);
return ;
}
// has[eage[i][j]]=-1;
vis1[eage[i][j]]=;
}
}
if(allsame==)
printf("NO\n");
else
printf("YES\n1\n");
return ;
}

邻接表:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cstring>
using namespace std;
#define N 100005 struct Eage
{
int v,next;
}eage[N<<];
int head[N],cnte; void addeage(int a,int b)
{
eage[cnte].v=b;
eage[cnte].next=head[a];
head[a]=cnte++;
} int col[N];
bool vis[N],vis1[N];
int has[N]; bool dfs(int node,int color)
{
if(col[node]!=color)
return ;
//cout<<node<<'*'<<col[node]<<endl;
vis[node]=;
if(has[node]==)
return ;
if(has[node]==-)
return ;
for(int i=head[node]; i>; i=eage[i].next)
{
if(vis[eage[i].v]==)
if(dfs(eage[i].v,color)==)
{
has[node]=-;
return ;
}
}
has[node]=;
return ;
} bool legal(int node)
{
memset(vis,,sizeof(vis));
vis[node]=;
for(int i=head[node]; i>; i=eage[i].next)
{
if(dfs(eage[i].v,col[eage[i].v])==)
{
//cout<<node<<":wrong"<<endl;
return ;
}
}
//cout<<node<<":correct"<<endl;
return ;
} int main()
{
int n;
cnte=;
scanf("%d",&n);
int a,b;
for(int i=; i<n-; i++)
{
scanf("%d%d",&a,&b);
addeage(a,b);
addeage(b,a);
}
for(int i=; i<=n; i++)
scanf("%d",&col[i]);
int allsame=;
for(int i=; i<=n; i++)
for(int j=head[i]; j>; j=eage[j].next)
{
if(col[i]!=col[eage[j].v])
{
allsame=;
if(legal(i)&&vis1[i]==)
{
printf("YES\n%d\n",i);
return ;
}
// has[i]=-1;
//vis1[i]=1;
if(legal(eage[j].v)&&vis1[eage[j].v]==)
{
printf("YES\n%d\n",eage[j].v);
return ;
}
// has[eage[i][j]]=-1;
//vis1[eage[j].v]=1;
}
}
if(allsame==)
printf("NO\n");
else
printf("YES\n1\n");
return ;
}

 

Codeforces_764_C. Timofey and a tree_(并查集)(dfs)的更多相关文章

  1. 第46届ICPC澳门站 K - Link-Cut Tree // 贪心 + 并查集 + DFS

    原题链接:K-Link-Cut Tree_第46屆ICPC 東亞洲區域賽(澳門)(正式賽) (nowcoder.com) 题意: 要求一个边权值总和最小的环,并从小到大输出边权值(2的次幂):若不存在 ...

  2. HDU 1232 并查集/dfs

    原题: http://acm.hdu.edu.cn/showproblem.php?pid=1232 我的第一道并查集题目,刚刚学会,我是照着<啊哈算法>这本书学会的,感觉非常通俗易懂,另 ...

  3. 1021.Deepest Root (并查集+DFS树的深度)

    A graph which is connected and acyclic can be considered a tree. The height of the tree depends on t ...

  4. POJ1291-并查集/dfs

    并查集 题意:找出给定的这些话中是否有冲突.若没有则最多有多少句是对的. /* 思路:如果第x句说y是对的,则x,y必定是一起的,x+n,y+n是一起的:反之x,y+n//y,x+n是一起的. 利用并 ...

  5. F2 - Spanning Tree with One Fixed Degree - 并查集+DFS

    这道题还是非常有意思的,题意很简单,就是给定一个图,和图上的双向边,要求1号节点的度(连接边的条数)等于K,求这棵树的生成树. 我们首先要解决,如何让1号节点的度时为k的呢???而且求的是生成树,意思 ...

  6. UVA208-Firetruck(并查集+dfs)

    Problem UVA208-Firetruck Accept:1733  Submit:14538 Time Limit: 3000 mSec  Problem Description The Ce ...

  7. 2018 计蒜之道复赛 贝壳找房魔法师顾问(并查集+dfs判环)

    贝壳找房在遥远的传奇境外,找到了一个强大的魔法师顾问.他有 22 串数量相同的法力水晶,每个法力水晶可能有不同的颜色.为了方便起见,可以将每串法力水晶视为一个长度不大于 10^5105,字符集不大于  ...

  8. Codeforces 455C Civilization(并查集+dfs)

    题目链接:Codeforces 455C Civilization 题目大意:给定N.M和Q,N表示有N个城市,M条已经修好的路,修好的路是不能改变的.然后是Q次操作.操作分为两种.一种是查询城市x所 ...

  9. hdu6370 并查集+dfs

    Werewolf Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total ...

随机推荐

  1. vuex资料

    vuex最简单.最详细的入门文档 链接:https://segmentfault.com/a/1190000009404727 https://www.jb51.net/article/138239. ...

  2. 调试 Android* x86 应用程序的方法以及要使用的工具

    作者:Xiaodong Wang 1.简单介绍 众所周知,Android* 开发者头顶很多称呼:设计员.程序员等,而且一般会不可避免地被称为故障检修工. 代码中的错误无法避免.因此不管您是否一開始就造 ...

  3. 写一个简单的Makefile

    all: osx .PHONY: osx linux run osx: kale.dylib linux : kale.so run: kale.bin CC = gcc OBJECTS = $(pa ...

  4. YTU 2530: 小勇玩lol

    2530: 小勇玩lol 时间限制: 1 Sec  内存限制: 128 MB 提交: 112  解决: 77 题目描述 小勇是一个忠实的lol玩家,他有自己的战斗力计算方法,每个星期他都会算一下自己的 ...

  5. loj 6034 线段游戏

    题目大意: 给出若干条线段,用 (x1,y2),(x2,y2) 表示其两端点坐标,现在要求支持两种操作: 0 x1 y1 x2 y2 表示加入一条新的线段 (x1,y2),(x2,y2) 1 x0 询 ...

  6. POJ1912 A highway and the seven dwarfs (判断凸包与直线相交 logn)

    POJ1912 给定n个点 和若干条直线,判断对于一条直线,是否存在两个点在直线的两侧. 显然原命题等价于 凸包与直线是否相交. O(n)的算法是显而易见的 但是直线数量太多 就会复杂到O(n^2)由 ...

  7. 51Nod 1967 路径定向 —— 欧拉回路

    题目:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1967 显然是欧拉回路问题,度数为奇数的点之间连边,跑欧拉回路就可以 ...

  8. 聚类-----KMeans

    package Spark_MLlib import org.apache.spark.ml.clustering.KMeans import org.apache.spark.sql.SparkSe ...

  9. Eclipse中直接执行sql语句(图文说明)

    转自:https://blog.csdn.net/changjyzzu/article/details/45487847 1.首先新建sql文件,然后打开文件 22.右键点击空白处,点击set-con ...

  10. C#面向过程之局部变量、成员变量、变量作用域、可变参数

    局部变量与成员变量:  局部变量:定义在方法里面的变量就叫做局部变量:没有默认的初始值,使用变量之前必须给它赋值成员变量:定义在类下面的变量叫做成员变量:如果是数值类型默认初始值为0 如果是引用类型默 ...