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. JQuery编程demo练习

    JQuery练习demo:     编敲代码,实现:     1.选中当中一列的复选框时,该复选框所在行的背景色高亮显示(黄色). 2.取消选中复选框时,所在行的背景色恢复. ============ ...

  2. android:省市县三级联动(基于json和spring)

    一.请看效果图": 二.程序的代码: 1.MainActivity.java package com.loveplusplus.loader.demo.ui; import org.json ...

  3. R语言pdf输出中文乱码处理

    1.使用基础包.使用函数pdf()输出 在使用pdf()函数时,要输出中文,仅仅有一种字体可选. 样例: pdf("chinese.pdf",family="GB1&qu ...

  4. ACdream 1412 DP+排列组合

    2-3 Trees Problem Description 2-3 tree is an elegant data structure invented by John Hopcroft. It is ...

  5. ZOJ Design the city LCA转RMQ

    Design the city Time Limit: 1 Second      Memory Limit: 32768 KB Cerror is the mayor of city HangZho ...

  6. SpringMVC_2

    web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi=" ...

  7. HDU 5754Life Winner Bo

    Life Winner Bo Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) ...

  8. mysql与mongoDB的特点和优劣

    首先分析下mysql与mongoDB的特点和优劣 从图中分析: 再来分析下应用场景: a.如果需要将mongodb作为后端db来代替mysql使用,即这里mysql与mongodb 属于平行级别,那么 ...

  9. brew安装PHP7 swoole

    环境: 系统:mac os High Sierra 10.13.3 php版本:7.0.27_19 1.安装homebrew brew 又叫Homebrew,是Mac OSX上的软件包管理工具,能在M ...

  10. HDU2138 素数判定

    HDU2138 给定N个32位大于等于2的正整数 输出其中素数的个数 用Miller Rabin 素数判定法 效率很高 数学证明比较复杂,略过, 会使用这个接口即可. #include<iost ...