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改动默认的环境的字符集为utf-8
mysql改动环境的默认字符集为utf-8(当然你也能够设置成别的,国际点还是utf-8好) 假设不把mysql字符集统一下.后面还是有点麻烦的 首先得在服务里关掉mysql的服务(一定要先关掉mys ...
- iOS UITableViewCell 几个方法的优先级
#第一组 - (void)setDataDict:(NSDictionary *)dataDict;这种方法优先运行 - (id)initWithStyle:(UITableViewCellSty ...
- Android学习笔记(14):相对布局RelativeLayout
相对布局RelativeLayout,继承自ViewGroup.相对布局的子组件的位置总是相对于兄弟组件或者父容器决定的. RelativeLayout支持的XML属性: android:gravit ...
- shell脚本自动更新git
gitpull.sh #!/bin/bash cd /home/wwwroot/default/mouse && git pull cd /home/wwwroot/default/s ...
- oracle存储过程和游标的使用
oracle存储过程和游标的使用 (2011-04-19 14:52:47) 转载▼ 游标: 用来查询数据库,获取记录集合(结果集)的指针,我们所说的游标通常是指显式游标,因此从现在起没有特别指明的情 ...
- 【BZOJ 5165】 树上倍增
[题目链接] 点击打开链接 [算法] 树上倍增,时间复杂度 : O(qklog(n)) [代码] #include<bits/stdc++.h> using namespace std; ...
- POJ 2104 HDU 2665 主席树 解决区间第K大
两道题都是区间第K大询问,数据规模基本相同. 解决这种问题, 可以采用平方划分(块状表)复杂度也可以接受,但是实际表现比主席树差得多. 这里大致讲一下我对主席树的理解. 首先,如果对于某个区间[L,R ...
- 深入理解JMM(Java内存模型) --(七)总结
JMM 掌管着一个线程对内存的动作 (读和写)影响其他线程对内存的动作的方式.由于使用处理器寄存器和预处理 cache 来提高内存访问速度带来的性能提升,Java 语言规范(JLS)允许一些内存操作并 ...
- Java IO --ByteArrayOutputStream (六)***
Java提供了很丰富的io接口,已经可以满足我们大部分读取数据的需求,这个在C读取数据需要自己定义缓冲区数组大小,而且要小心翼翼的防止缓冲区溢出的情况相当不同.一般情况下我们读取的数据都是直接读取成为 ...
- HTML5牛刀小试
第一周的HTML5苦逼之路,就这么简单,充实,忙碌的开始了,丝毫不敢有一丢丢懈怠,压力是有的,但更多的是对自己的信心,更是对自己的踏上苦逼之路的意志的肯定. 简单回顾了下这周所学内容.从认识HTML基 ...