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 ...
随机推荐
- 小贝_mysql 存储过程
存储过程 简要: 1.什么是存储过程 2.使用存储过程 一.存储过程 概念类似于函数,就是把一段代码封装起来.当要行这段代码的时候,能够通过调用该存储过程来实现.在封装的语句体里面.能够用if/els ...
- HDU 4869 Turn the pokers (2014多校联合训练第一场1009) 解题报告(维护区间 + 组合数)
Turn the pokers Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- 图像处理之基础---yuv420及其rgb,bayer, yuv, RGB的相互转换详解
YUV格式解析1(播放器——project2) 根据板卡api设计实现yuv420格式的视频播放器 打开*.mp4;*.264类型的文件,实现其播放. 使用的视频格式是YUV420格式 YUV格式 ...
- UVa 11582 Colossal Fibonacci Numbers! 【大数幂取模】
题目链接:Uva 11582 [vjudge] watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fil ...
- ios30---pthread, NSThread, GCD, NSOperation
pthread(线程库,很早就有的技术,了解):一套通用的多线程API适用于Unix\Linux\Windows等系统(java开发也有pthread)跨平台\可移植使用难度大(全是C函数) C语言 ...
- Bootstrap4 网格系统
学习注意事项 col-*-* 第一个*是设备类型,第二个*是控件宽度的占比 屏幕被等分为12,col-1宽度是1/12,col-6宽度是50%,col-12宽度是100% 给应用了class的elem ...
- P1197 [JSOI2008]星球大战(并查集判断连通块+正难则反)
P1197 [JSOI2008]星球大战(并查集判断连通块+正难则反) 并查集本来就是连一对不同父亲的节点就的话连通块就少一个. 题目描述 很久以前,在一个遥远的星系,一个黑暗的帝国靠着它的超级武器统 ...
- CSU 1806 Toll 自适应simpson积分+最短路
分析:根据这个题学了一发自适应simpson积分(原来积分还可以这么求),然后就是套模板了 学习自适应simpson积分:http://blog.csdn.net/greatwall1995/arti ...
- 2018.09.08 DL24 Day1 总结
补一下之前的总结…… T1.restaurant 这道题还是很简单的嘛,子恒dalao非常良心.我们把招牌菜和所需要的菜品绑定在一起就成了完全背包,然后直接跑一遍完全背包即可. #include< ...
- ODB(C++ ORM)用Mingw的完整编译过程
用mingw官方的GCC4.7.2编译libodb后,并用odb compiler对hello示例生成odb的"包裹"代码,编译链接总是不能通过,下面是编译example/hell ...