CodeForces 764C Timofey and a tree
来源:互联网 发布:梦幻西游频繁网络错误 编辑:程序博客网 时间:2024/05/17 05:05
题目链接:http://codeforces.com/contest/764/problem/C
题意:给你一棵树,每个节点的颜色为col[i],让你从这棵树中任意选择一个节点为根节点,使得所有的子树,在子树内部的所有颜色都是一样的,如果存在这样一个点那就数输出YES和这点,如果答案有多个,则输出任意一个,否则输出NO
解析:假设有cnt条边的两个端点颜色不一样,所以这cnt条边肯定是全都连在根节点上的,那么我们只需要记录,端点颜色不同的边的每个端点出现了多少次,如果有一顶点出现的次数和颜色不同的边出现的次数一样多,那么我们就能找到这一个顶点达到题目要求
#include <iostream>#include <cstdio>#include <algorithm>#include <vector>#include <cstring>#include <queue>#include <map>using namespace std;const int maxn = 2e5+100;const int inf = 0x7fffffff;int u[maxn],v[maxn];int col[maxn];int du[maxn];int main(void){ int n; scanf("%d",&n); memset(du,0,sizeof(du)); for(int i=0;i<n-1;i++) scanf("%d %d",&u[i],&v[i]); for(int i=1;i<=n;i++) scanf("%d",&col[i]); int cnt = 0; for(int i=0;i<n-1;i++) { if(col[u[i]]!=col[v[i]]) { cnt++; du[u[i]]++; du[v[i]]++; } } int flag = 1; for(int i=1;i<=n;i++) { if(du[i]==cnt) { flag = 0; puts("YES"); printf("%d\n",i); break; } } if(flag) puts("NO"); return 0;}
0 0
- 【codeforces 764C】Timofey and a tree
- CodeForces 764C Timofey and a tree
- Codeforces 764C - Timofey and a tree(dfs)
- Codeforces 764C Timofey and a tree 树+思维
- 【codeforces 764C】Timofey and a tree 题解
- Codeforces 764C Timofey and a tree(思路)
- codeforces 764C Timofey and a tree(树+思维)
- codeforces 764C Timofey and a tree (思维题)
- 764C Timofey and a tree[思维]
- C. Timofey and a tree codeforces 395 div2 C
- 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 C Timofey and a tree 树
- 【Codeforces Round #395 (Div. 2)】Codeforces 764C Timofey and a tree
- Codeforces 764C或763A Timofey and a tree 【好题】
- Codeforces 763A-Timofey and a tree
- CodeForces 763A Timofey and a tree
- Universal Online Judge Documents
- nyoj ACM:士兵杀敌(四)(树状数组)
- 最简单易用的Qt 界面库插件 FTStyle (二)最新版本
- 【mysql】FATAL ERROR: Could not find ./bin/my_print_defaults 解决方法
- 方格填数-用next_permutation()全排列函数做的
- CodeForces 764C Timofey and a tree
- [DP] BZOJ 4758 [Usaco2017 Jan]Subsequence Reversal
- 方程的解
- 写给初学者01_android_什么是安卓
- 那年,我们走过的安卓开发中的那些坑(19:23:50 Generate Signed APK: Errors while building APK. You can find the errors )
- 九宫格
- 数据库的简单操作指令
- 秒杀系统DAO层设计
- 网络安全:包过滤防火墙和代理防火墙(应用网关防火墙)