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 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 ≤ 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.

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
题目大意就是说是否存在这么一个点,使得去掉这个点后,所形成的子树在同一棵树上是否颜色相同,如果有不同的就输出NO,有就输出yes,并且输出该节点,答案可能不唯一。
思路:DFS ,邻接表,优化。
ps:该方法不是最优的方法,在这里我只写了这个大多数人都能想到的方法。

AC代码:
 1 #include<iostream>
2 #include<stdio.h>
3 #include<vector>
4 using namespace std;
5 struct Node{
6 int st;
7 int en;
8 } dis[100050];
9 vector<int > load[100050];//邻接表
10 int color[100050];
11 bool dfs(int root,int fa,int col,int t)
12 {
13 if(t!=2) t++;
14 else
15 {
16 if(col!=color[fa])//颜色不同,返回false
17 return false;
18 }
19 bool fl;
20 for(int i=0;i<load[root].size();i++)
21 {
22 int nxt=load[root][i];
23 if(nxt!=fa)
24 {
25 fl=dfs(nxt,root,color[nxt],t);
26 if(!fl)
27 return false;
28 }
29 }
30 return true;
31 }
32 int main()
33 {
34 int n,x,y;
35 cin>>n;
36 for(int i=0;i<n-1;i++)
37 {
38 scanf("%d%d",&x,&y);
39 dis[i].st=x;//这个数组是用来查找的时候优化的。
40 dis[i].en=y;
41 load[x].push_back(y);
42 load[y].push_back(x);
43 }
44 for(int i=1;i<=n;i++)
45 scanf("%d",&color[i]);
46 bool flag=false,fg=false;
47 int i,vis=0;
48 for(int i=0;i<n-1;i++)
49 if(color[dis[i].st]==color[dis[i].en])
50 vis++;
51 if(vis==n-1)//如果相等,说明都是同一种颜色。输出1就ok
52 {
53 cout<<"YES"<<endl<<1<<endl;
54 return 0;
55 }
56 if(n==2)//只有两个节点的时候
57 {
58 cout<<"YES"<<endl<<1<<endl;
59 return 0;
60 }
61 int r;
62 for(int j=0;j<n-1;j++)
63 {
64 int i=dis[j].st;
65 int b=dis[j].en;
66 if(color[i]!=color[b])//颜色不同,必有一个点是要去除的点。
67 {
68 flag=dfs(i,i,color[i],0);
69 if(flag)
70 {
71 r=i;
72 fg=true;
73 break;
74 }
75 flag=dfs(b,b,color[b],0);
76 if(flag)
77 {
78 fg=true;
79 r=b;
80 }
81 break;
82 }
83 }
84 if(fg)
85 cout<<"YES"<<endl<<r<<endl;
86 else //如果颜色不同,且fg为false,那么必不存在
87 cout<<"NO"<<endl;
88 return 0;
89 }
90


CodeForces - 764C的更多相关文章

  1. 【codeforces 764C】Timofey and a tree

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  2. 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 th ...

  3. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  4. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  5. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  6. 【Codeforces 738A】Interview with Oleg

    http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...

  7. CodeForces - 662A Gambling Nim

    http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...

  8. CodeForces - 274B Zero Tree

    http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...

  9. CodeForces - 261B Maxim and Restaurant

    http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...

  10. CodeForces - 696B Puzzles

    http://codeforces.com/problemset/problem/696/B 题目大意: 这是一颗有n个点的树,你从根开始游走,每当你第一次到达一个点时,把这个点的权记为(你已经到过不 ...

随机推荐

  1. CCF 202012-5星际旅行(20~100分)

    前置知识 线段树:通过懒惰标记,可实现区间处理,和区间询问皆为\(O(logn)\)时间复杂度的数据结构,是一种二叉树.因此对于一个节点\(st\),其左儿子节点为\(st*2\),右节点为\(st* ...

  2. MIMO雷达中波形复用/分离的方法------TDMA\FDMA\DDMA\CDMA

    最先接触到MIMO雷达的波形复用/分离的方法还是工作中负责的TI1843项目中了解的,主要还是时分多址波形(TDMA),当时刚接触时对这些很疑惑,再加上后面看到了频分多址波形(FDMA).码分多址波形 ...

  3. WebStrom提交代码到GitLab报错Error: Cannot find any-observable implementation nor global.Observable.

    项目场景: 前端代码完成后,提交代码 问题描述 提交代码到GitLab时,因自动检测机制导致项目提交失败 C:\D\insper\inspur_works\custom-manage-front\no ...

  4. LeetCode 周赛上分之旅 #45 精妙的 O(lgn) 扫描算法与树上 DP 问题

    ️ 本文已收录到 AndroidFamily,技术和职场问题,请关注公众号 [彭旭锐] 和 BaguTree Pro 知识星球提问. 学习数据结构与算法的关键在于掌握问题背后的算法思维框架,你的思考越 ...

  5. 深入解析HTTP请求:了解请求特征与报文格式的关键秘密

    引言 在上一章节中,我们详细探讨了超文本传输协议(HTTP)的基本概念,并且延伸讨论了HTTP请求响应的基本流程.在这个过程中,浏览器首先通过DNS解析来确定要访问的服务器的IP地址,然后与服务器建立 ...

  6. Solution Set -「ARC 124」

    「ARC 124A」LR Constraints Link. 我们可以把 \(1\sim n\) 个盒子里能放的球的编号集合全部求出来.然后就直接来. 注意题目已经给出了 \(k\) 个球的位置,所以 ...

  7. 简化 Go 开发:使用强大的工具提高生产力

    作为 Go 开发人员,应该都知道维持简洁高效开发工作流程的重要性.为了提高工作效率和代码质量,简化开发流程并自动执行重复性任务至关重要.在本文中,我们将探讨一些强大的工具和技术,它们将简化 Go 开发 ...

  8. 如何通过代码混淆绕过苹果机审,解决APP被拒问题

    目录 iOS代码混淆 功能分析 实现流程 类名修改 方法名修改 生成垃圾代码 替换png等静态资源MD5 info.plist文件添加垃圾字段 功能分析 实现流程 类名修改 方法名修改 生成垃圾代码 ...

  9. [ABC205F] Grid and Tokens 题解

    Grid and Tokens 题目大意 给定 \(n\) 个点和一个 \(H\times W\) 的网格,每个点可以放置在 \((A_i,B_i)\) 到 \((C_i,D_i)\) 的矩形中或不放 ...

  10. Kubernetes文档支持的版本

    简介 https://kubernetes.io/zh-cn/docs/ 官方中文网站一共是可以阅读5个k8s版本的文档.这5个版本包括k8s最新版和最近的4个版本.例如当前最新版是1.28,那么文档 ...