Codeforces Round 395 C Timofey and a tree 树
来源:互联网 发布:中国知网数据库检索 编辑:程序博客网 时间:2024/05/22 00:07
题意:一颗树,每个节点都有一个颜色值。如果能从中拿掉一个点后,所形成的深林中子树所有节点颜色相同,但不同子树颜色可以不同。
思路:把两端点颜色不同不同的线段选出来,然后记录边两端不同颜色出现的次数,某个点出边(不同颜色的边)的次数等于总的不同颜色的边数。那说明符合题意。
#include<cstdio>#include<queue>#include<iostream>#include<vector>#include<map>#include<cstring>#include<string>#include<set>#include<stack>#include<algorithm>#define cle(a) memset(a,0,sizeof(a))#define inf(a) memset(a,ox3f,sizeof(a))#define ll long long#define FOR(i,n) for(int i=0;i<n;i++)using namespace std;const int INF = ( 2e9 ) + 2;const int maxn = 1e5+10;int u[maxn],v[maxn],c[maxn];map<int,int> mp;int main(){int n;scanf("%d",&n);for(int i=0;i<n-1;i++)scanf("%d%d",&u[i],&v[i]);for(int i=1;i<=n;i++)scanf("%d",&c[i]);int cnt=0;for(int i=0;i<n-1;i++){if(c[u[i]]!=c[v[i]]){cnt++;mp[u[i]]++;mp[v[i]]++;}}int flag=0,ans=0;for(int i=1;i<=n;i++){if(mp[i]==cnt)flag=1,ans=i;}if(flag)printf("YES\n%d\n",ans);else printf("NO\n");}
阅读全文
0 0
- Codeforces Round 395 C Timofey and a tree 树
- Codeforces Round #395 (Div. 2) C. Timofey and a tree
- Codeforces Round #395 (Div. 2) C. Timofey and a tree
- Codeforces Round #395 (Div. 2) C. Timofey and a tree (树的基础应用)
- Codeforces Round #395 (Div. 2) C. Timofey and a tree(树的基础应用)
- 【Codeforces Round #395 (Div. 2)】Codeforces 764C Timofey and a tree
- Codeforces Round #395 (Div. 2)Timofey and a tree
- Codeforces Round #395 (Div. 2) -- C. Timofey and a tree(并查集+缩点)
- Codeforces Round #395(Div. 2)C. Timofey and a tree【思维+并查集缩点+判定】
- Codeforces Round #395 (Div.2) C-Timofey and a tree 树型dp
- C. Timofey and a tree codeforces 395 div2 C
- 【codeforces 764C】Timofey and a tree
- CodeForces 764C Timofey and a tree
- Codeforces Round #395 (Div. 1) D Timofey and a flat tree (树hash)
- Codeforces 764C Timofey and a tree 树+思维
- codeforces 764C Timofey and a tree(树+思维)
- Codeforces Round #395 Div1的A题Timofey and a tree
- C. Timofey and a tree
- 05. JavaMail 回复邮件
- Android中对图片高斯模糊的一些方法
- 常用文件类型
- LeetCode 206. Reverse Linked List 反转链表
- echarts3地图下钻省市
- Codeforces Round 395 C Timofey and a tree 树
- mysql存储过程实例(一)---------------图书管理系统部分查找
- (二) 安装docker------学习笔记
- 06. JavaMail 转发邮件
- 在配置ubuntu中eclipse 和TensorFlow中遇到的一些问题
- win10 安装 tomcat 7 (图文)
- vagrant应用场景
- iOS开发——Swift快速上手(知识篇)
- SGI STL(三)——C++ 一般概念