☆【CodeForces - 764C】Timofey and a tree (思维题,树的性质)

题干:

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

题目大意:

    给定一棵每个顶点都标记颜色的树(无根树),问能否从一个顶点出发(以他为根节点),使得它的子树都是一样颜色(不包括这个顶点本身的颜色)详见第二个样例。

题解:因条件要求,我们可以知道不同的颜色的顶点中其中一个必须是答案顶点,我们统计一下不同颜色顶点个数(设为m),和不同颜色顶点中顶点出现次数。最后如果有一个顶点出现次数等于m,那么这个顶点就是答案顶点。

解题报告:

    思维题啊害死我、、、

    第一反应子树,我擦dfs序吧?

    第二反应不行啊总不能对每一个顶点建树吧?要不试试、、

    第三反应woc超时了,,,    

    关于图论的问题,可以考虑cnt计数、、、这题sum表示每一条边中有多少条边是两节点颜色不一样的。如果正好和节点 i 的边数相同,那 i 就是答案。

AC代码:

#include<bits/stdc++.h>using namespace std;
const int MAX = 1e5 + 5 ;
int u[MAX],v[MAX],cnt[MAX],col[MAX];
int main()
{int n,sum=0;cin>>n;for(int i = 1; i<n; i++) {scanf("%d%d",&u[i],&v[i]);}for(int i = 1; i<=n; i++) {scanf("%d",&col[i]);}for(int i = 1; i<n; i++) {if(col[u[i]]!=col[v[i]]) {sum++;cnt[u[i]]++;cnt[v[i]]++;}}for(int i = 1; i<=n; i++) {if(cnt[i] == sum) {printf("YES\n%d\n",i);return 0 ;}}printf("NO\n");return 0 ;
}

dfs序超时代码:(刚开始是o(n)的注释掉那一部分,后来改成logn的线段树,但是还是超时,看来时间都花在dfs序上了)

#include<bits/stdc++.h>using namespace std;
const int MAX = 100000 +5;
const int INF = 0x3f3f3f3f;
int cnt,id;
int st[MAX],ed[MAX],col[MAX],q[MAX];
int head[MAX];
struct Edge{int to,ne;
}e[1000000 +  5];
struct TREE {int l,r;int maxx,minn;
} tree[MAX * 4];
void dfs(int x,int fa)
{q[++id]=x;st[x]=id;for(int i=head[x];i!=-1;i=e[i].ne)if(e[i].to!=fa)dfs(e[i].to,x);ed[x]=id;
}
void pushup(int cur) {tree[cur].maxx = max(tree[cur*2].maxx,tree[cur*2+1].maxx);tree[cur].minn = min(tree[cur*2].minn,tree[cur*2+1].minn);
}
void build(int l,int r,int cur) {tree[cur].l=l;tree[cur].r=r;if(l == r) {tree[cur].maxx = col[q[r]];tree[cur].minn = col[q[r]];return ;}int m = (l + r)/2;build(l,m,cur*2);build(m+1,r,cur*2+1);pushup(cur);
}
int qmax(int pl,int pr,int cur) {if(pl <= tree[cur].l && pr >= tree[cur].r) return tree[cur].maxx;int ll=0,rr=0;if(pl <= tree[cur*2].r) ll = qmax(pl,pr,cur*2);if(pr >= tree[cur*2+1].l) rr = qmax(pl,pr,cur*2+1);return max(rr,ll);
}
int qmin(int pl,int pr,int cur) {if(pl <= tree[cur].l && pr >= tree[cur].r) return tree[cur].minn;int ll=INF,rr=INF;if(pl <= tree[cur*2].r) ll = qmin(pl,pr,cur*2);if(pr >= tree[cur*2+1].l) rr = qmin(pl,pr,cur*2+1);return min(rr,ll);
}
void add(int u,int v) {e[++cnt].to = v;e[cnt].ne = head[u];head[u] = cnt;
}
int main()
{int n,u,v;scanf("%d",&n);memset(head,-1,sizeof head);for(int i = 1; i<n; i++) {scanf("%d%d",&u,&v);add(u,v);add(v,u);}for(int i = 1; i<=n; i++) {scanf("%d",&col[i]);}//枚举每一个顶点int flag = 1; for(int i = 1; i<=n; i++) {id=0;dfs(i,i);flag = 1;build(1,n,1);for(int j = head[i]; j!=-1; j=e[j].ne) {int tmp = col[e[j].to];int maxx = qmax(st[e[j].to],ed[e[j].to],1);int minn = qmin(st[e[j].to],ed[e[j].to],1);if(maxx != tmp || minn != tmp) flag=0;
//			for(int k = st[e[j].to]; k<=ed[e[j].to]; k++) {
//				if(col[q[k]] != tmp) {
//					flag=0;break;
//				}
//			}if(flag == 0) break;}if(flag == 1) {printf("YES\n");printf("%d\n",i);return 0;}}printf("NO\n");return 0 ;
}

或者用并查集缩图https://www.cnblogs.com/jasonlixuetao/p/6401831.html

并查集思路:
先建图,遍历每个结点,统计每个结点的度,对结点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=1; i<=n; i++) {deg[i].p=i;father[i]=i;wei[i]=1;}for(int i=0; i<n-1; 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=1; i<=n; i++)scanf("%d",&col[i]);for(int i=1; i<=n; i++)for(int j=0; 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=0;for(int i=1; i<=n; i++)if(father[i]==i)cnt++;for(int i=1; i<=n; i++) {if(cnt==deg[i].d+1) {printf("YES\n%d\n",i);return 0;}}printf("NO\n");return 0;
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/441715.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

java中实现线程互斥的关键词_简单的互斥同步方式——synchronized关键字详解

2. synchronized的原理和实现细节2.1 synchronized可以用在那些地方静态方法,锁对象为当前类的class对象,不用显式指定实例方法,锁对象为当前实例对象,不用显式指定同步代码块,锁对象为括号中指定的对象&#xff0c;必须显式指定被synchronized修饰的方法或者代码块,同一时刻只…

【51nod - 1875】 丢手绢(约瑟夫问题,可打表,用STL模拟)

题干&#xff1a; 六一儿童节到了&#xff0c;小朋友们在玩丢手绢的游戏。总共有C个小朋友&#xff0c;编号从1到C&#xff0c;他们站成一个圈&#xff0c;第i(1<i<C)个人的左边是i-1&#xff0c;第1个人的左边是C。第i(1<i<C)个人的右边是i1&#xff0c;第C个人的…

mysql server远程连接_本地远程连接 MySQL server

问题MySql Server 出于安全方面考虑默认只允许本机(localhost, 127.0.0.1)来连接访问。如果想远程访问&#xff0c;需要额外做下操作。配置修改定位文件/etc/mysql/mysql.conf.d/mysqld.cnf定位属性skip-networking #注释掉 因为它是屏蔽掉一切TCP/IP连接bind-address 127.0.0…

关闭VS警告#pragma warning(disable:4996)

代码实现&#xff1a; #pragma warning(disable:4996) 1. #pragma warning只对当前文件有效&#xff08;对于.h&#xff0c;对包含它的cpp也是有效的&#xff09;&#xff0c;而不是对整个工程的所有文件有效。当该文件编译结束&#xff0c;设置也就失去作用。 2. #pragma wa…

【CodeForces - 608C】Chain Reaction (二分 或 dp ,思维)

题干&#xff1a; 题目大意&#xff1a; 题意是在一条直线上坐落着不同位置的灯塔&#xff0c;每一个灯塔有自己的power level&#xff0c;当作是射程范围。现在从最右边的灯塔开始激发&#xff0c;如果左边的灯塔在这个灯塔的范围之内&#xff0c;那么将会被毁灭。否则会被激…

java关闭文本_如何更优雅的关闭java文本、网络等资源

通常在 java 中对文本、网络资源等操作起来是很繁杂的&#xff0c;要声明&#xff0c;读取&#xff0c;关闭三个阶段&#xff0c;还得考虑异常情况。假设我们要读取一段文本显示到控制台&#xff0c;通常会有如下的代码&#xff1a;public static void main(String[] args) {Fi…

java类匿名类实例_Java匿名类,匿名内部类实例分析

本文实例讲述了Java匿名类&#xff0c;匿名内部类。分享给大家供大家参考&#xff0c;具体如下&#xff1a;本文内容&#xff1a;内部类匿名类首发日期 &#xff1a;2018-03-25内部类&#xff1a;在一个类中定义另一个类&#xff0c;这样定义的类称为内部类。【包含内部类的类可…

【HDU - 6441】Find Integer (费马大定理 + 奇偶数列法构造勾股定理)

题干&#xff1a; people in USSS love math very much, and there is a famous math problem . give you two integers nn,aa,you are required to find 22 integers bb,cc such that ananbncnbncn. Input one line contains one integer TT;(1≤T≤1000000)(1≤T≤100000…

dart与java互调_Dart与Java不同的地方

数据类型基类是num数值型的操作运算符&#xff1a; 、 - 、* 、/ 、 ~/ 、 %/ 除法 整数余数~/ 除法 取整% 取余常用属性&#xff1a; isNaN、isEven、isOdd (是否非数字、奇偶)常用方法&#xff1a;abs()、round()、floorl()、ceil()、toInt()、toDouble()double nan 0.0 / 0…

算法学习 母函数

母函数又称生成函数。定义是给出序列:a0,a1,a2,.......ak,......,那么函数G(x)a0a1*xa2*x2......ak*xk称为序列a0,a1,a2,.......ak,......的母函数(即生成函数)。 例如&#xff1a;序列1,2,3.......n的生成函数为&#xff1a;G(x)x2x23x3........nxn。点此链接:百度百科 特别…

plsq卸载 删除注册表、_win10操作系统下oracle11g客户端/服务端的下载安装配置卸载总结...

win10操作系统下oracle11g客户端/服务端的下载安装配置卸载总结一&#xff1a;前提注意&#xff1a;现在有两种安装的方式1. oracle11g服务端(64位)oracle客户端(32位)plsql(32位)2. oracle11g服务端(32位)plsql(32位)这里我选择的是第二种 原因是 &#xff1a;首先需要明确ora…

【HDU -1568】 Fibonacci(斐波那契通项公式+取对数)

Fibonacci Problem Description 2007年到来了。经过2006年一年的修炼&#xff0c;数学神童zouyu终于把0到100000000的Fibonacci数列 (f[0]0,f[1]1;f[i] f[i-1]f[i-2](i>2))的值全部给背了下来。 接下来&#xff0c;CodeStar决定要考考他&#xff0c;于是每问他一个数字&a…

java sqlserver分页查询_实现sqlserver分页查询语句

实现sqlserver分页查询语句sqlServer&#xff1a;一次查询&#xff0c;数据库只返回一页的数据。而不是取出所有的数据。pagesize&#xff1a; 每页显示记录数cureentpage&#xff1a;当前页数select * from ( select TOP pagesize * from ( SELECT TOP pagesize*cureentpage…

【HDU - 1269】迷宫城堡 (tarjan算法模板)

题干&#xff1a; 为了训练小希的方向感&#xff0c;Gardon建立了一座大城堡&#xff0c;里面有N个房间(N<10000)和M条通道(M<100000)&#xff0c;每个通道都是单向的&#xff0c;就是说若称某通道连通了A房间和B房间&#xff0c;只说明可以通过这个通道由A房间到达B房间…

php5.4 curl,PHP5.0~5.6 各版本兼容性cURL文件上传功能实例分析

本文实例分析了PHP5.0~5.6 各版本兼容性cURL文件上传功能。分享给大家供大家参考&#xff0c;具体如下&#xff1a;最近做的一个需求&#xff0c;要通过PHP调用cURL&#xff0c;以multipart/form-data格式上传文件。踩坑若干&#xff0c;够一篇文章了。重要警告没事不要读PHP的…

【HDU - 1518】Square (经典的dfs + 剪枝)

题干&#xff1a; Given a set of sticks of various lengths, is it possible to join them end-to-end to form a square? Input The first line of input contains N, the number of test cases. Each test case begins with an integer 4 < M < 20, the number …

php发扑克牌,php 扑克牌代码的简单例子

本文分享下&#xff0c;一段可模拟扑克牌玩法的php代码&#xff0c;有需要的朋友参考下。php 扑克牌代码&#xff0c;如下&#xff1a;cut();* www: jbxue.com*/class cards{/**** Declare our deck variable**/private $deck;/**** Constructor.. duh!**/function __construct…

【HDU - 1026 】Ignatius and the Princess I (bfs + 记录路径)

题干&#xff1a; The Princess has been abducted by the BEelzebub feng5166, our hero Ignatius has to rescue our pretty Princess. Now he gets into feng5166s castle. The castle is a large labyrinth. To make the problem simply, we assume the labyrinth is a N*…

php多线程模拟请求,浅谈php使用curl模拟多线程发送请求

每个PHP文件的执行是单线程的&#xff0c;但是php本身也可以用一些别的技术实现多线程并发比如用php-fpm进程&#xff0c;这里用curl模拟多线程发送请求。php的curl多线程是通过不断调用curl_multi_exec来获取内容&#xff0c;这里举一个demo来模拟一次curl多线程并发操作。//设…

【HDU - 1455】Sticks (dfs + 剪枝)

题干&#xff1a; George took sticks of the same length and cut them randomly until all parts became at most 50 units long. Now he wants to return sticks to the original state, but he forgot how many sticks he had originally and how long they were original…