Codeforces_764_C. Timofey and a tree_(并查集)(dfs)
2 seconds
256 megabytes
standard input
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.
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 ≤ n, u ≠ 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.
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.
4
1 2
2 3
3 4
1 2 1 1
YES
2
3
1 2
2 3
1 2 3
YES
2
4
1 2
2 3
3 4
1 2 1 2
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)的更多相关文章
- 第46届ICPC澳门站 K - Link-Cut Tree // 贪心 + 并查集 + DFS
原题链接:K-Link-Cut Tree_第46屆ICPC 東亞洲區域賽(澳門)(正式賽) (nowcoder.com) 题意: 要求一个边权值总和最小的环,并从小到大输出边权值(2的次幂):若不存在 ...
- HDU 1232 并查集/dfs
原题: http://acm.hdu.edu.cn/showproblem.php?pid=1232 我的第一道并查集题目,刚刚学会,我是照着<啊哈算法>这本书学会的,感觉非常通俗易懂,另 ...
- 1021.Deepest Root (并查集+DFS树的深度)
A graph which is connected and acyclic can be considered a tree. The height of the tree depends on t ...
- POJ1291-并查集/dfs
并查集 题意:找出给定的这些话中是否有冲突.若没有则最多有多少句是对的. /* 思路:如果第x句说y是对的,则x,y必定是一起的,x+n,y+n是一起的:反之x,y+n//y,x+n是一起的. 利用并 ...
- F2 - Spanning Tree with One Fixed Degree - 并查集+DFS
这道题还是非常有意思的,题意很简单,就是给定一个图,和图上的双向边,要求1号节点的度(连接边的条数)等于K,求这棵树的生成树. 我们首先要解决,如何让1号节点的度时为k的呢???而且求的是生成树,意思 ...
- UVA208-Firetruck(并查集+dfs)
Problem UVA208-Firetruck Accept:1733 Submit:14538 Time Limit: 3000 mSec Problem Description The Ce ...
- 2018 计蒜之道复赛 贝壳找房魔法师顾问(并查集+dfs判环)
贝壳找房在遥远的传奇境外,找到了一个强大的魔法师顾问.他有 22 串数量相同的法力水晶,每个法力水晶可能有不同的颜色.为了方便起见,可以将每串法力水晶视为一个长度不大于 10^5105,字符集不大于 ...
- Codeforces 455C Civilization(并查集+dfs)
题目链接:Codeforces 455C Civilization 题目大意:给定N.M和Q,N表示有N个城市,M条已经修好的路,修好的路是不能改变的.然后是Q次操作.操作分为两种.一种是查询城市x所 ...
- hdu6370 并查集+dfs
Werewolf Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total ...
随机推荐
- AutoCAD如何设置线宽
一般要求粗实线粗实线0.4,细实线0.2mm. 1 先打开图层特性管理器,新建一个图层,专门放粗实线(我起名叫"我的粗实线",颜色设置为紫色,线宽为0.4mm),此前的乱七八糟的图 ...
- YII数据流程浅析
MVC就不解释,直接上代码分析数据流程: 数据库图: 模型部分介绍: <?php /* * 前两个方法必须写 * 继承自CActiveRecord类 这个类位于 \framework\db\ar ...
- 86. LotusScript中的数组函数
R6对LotusScript有一些改进和增强,自那之后.Notes对象的接口时有补充和更新,但语言本身没有变化.那些改进就包括添加诸如ArrayGetIndex.ArrayUnique的实用函数. 但 ...
- 通过构建Cocoapods私有库进行组件化开发探索
专题一 一.创建私有索引库 选Github或者码云都可以,本例以Github为例.创建私有索引库用来作为自己组件库的索引: 二.本地添加私有索引库 添加:pod repo add 索引库名称 索引库地 ...
- web 开发之js---js 实现地址栏的表单提交加密编码
以前在做嵌入式web时,曾经想过cgi加密,原来js可以直接实现,太好了,这里js在客户端实现编码和解码 一.在地址栏直接输入:javascript:str=encodeURI("笃行天下& ...
- 【bzoj2654]】tree
给白色边都加上一个值,二分这个值,使得选取的白边数量减少 #include<algorithm> #include<iostream> #include<cstdlib& ...
- Codeforces Round #311 (Div. 2)C. Arthur and Table
C. Arthur and Table time limit per test 1 second memory limit per test 256 megabytes input standard ...
- UVA 10559 Blocks —— 区间DP
题目:https://www.luogu.org/problemnew/show/UVA10559 区间DP,有点难想: 为了方便,先把原来就是连续一段相同颜色的点看做一个点,记一下长度: f[i][ ...
- Ural 1158. Censored! 有限状态自动机+DP+大整数
Ural1158 看上去很困难的一道题. 原文地址 http://blog.csdn.net/prolightsfxjh/article/details/54729646 题意:给出n个不同的字符,用 ...
- Commons-FileUpload 文件上传(模板)
// 创建FileItem工厂函数 FileItemFactory FIF = new DiskFileItemFactory(); // 获取ServletFileUpload对象,使用工厂实例传入 ...