Codeforces 764C或763A Timofey and a tree 【好题】
来源:互联网 发布:淘宝客导购网站有哪些 编辑:程序博客网 时间:2024/06/04 18:50
题目链接:http://codeforces.com/contest/764/problem/C
题意:
有一棵 n 结点的树,每个结点都有一个颜色(c[i]),现要求删除其中一个点,使得其它有边点的颜色相同。
题解:
刚开始认为是广搜,后来看了题解(QWQ),才发现好巧妙啊。
我们可以先将有边的,颜色不相同的点的度+1,计数器+1.
最后扫描一下数组,如果数组值与计数器的值相同,说明截去这个点其它的树的点的颜色相同。输出yes并输出点的index
否则输出 no
代码:
#include <cstdio>const int size = 1e5+5;int n;int c[size], degree[size], high;struct _conn{ int u, v;}conn[size];int main() {scanf("%d", &n);for ( int i = 0; i < n-1; i ++ ) scanf("%d %d", &conn[i].u, &conn[i].v);for( int i =1; i <= n; i ++ ) scanf("%d", &c[i]);for ( int i = 0; i < n-1; i ++ ) {if(c[conn[i].u] != c[conn[i].v]) {high ++;degree[conn[i].u] ++;degree[conn[i].v] ++;}}for ( int i = 1; i <= n; i ++ ) {if(degree[i] == high) { printf("YES\n%d\n", i); return 0;}}puts("NO");return 0;}
0 0
- Codeforces 764C或763A Timofey and a tree 【好题】
- 【codeforces 764C】Timofey and a tree
- CodeForces 764C Timofey and a tree
- codeforces 764C Timofey and a tree (思维题)
- Codeforces 763A-Timofey and a tree
- CodeForces 763A Timofey and a tree
- codeforces 763A-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(树+思维)
- 764C Timofey and a tree[思维]
- C. Timofey and a tree----思维题
- C. Timofey and a tree
- C. Timofey and a tree codeforces 395 div2 C
- codeforces 763A Timofey and a tree(DFS)
- Codeforces Round #395 (Div. 2) C. Timofey and a tree
- parseInt函数
- LeetCode 371. Sum of Two Integers
- Codeforces Round #395 (Div. 2) D. Timofey and rectangles
- 最简单的Ogre系列之二——画线
- unity中屏幕和世界坐标系区别
- Codeforces 764C或763A Timofey and a tree 【好题】
- GRUB启动分析之stage1
- Java细粒度锁实现的3种方式
- Centos7 ifconfig这个命令没找到的解决方法
- Codeforces Round #395 (Div. 2)D. Timofey and rectangles
- GRUB启动分析之stage1.5
- 为什么硬盘要分区?
- Android 自定义手机输入框
- DB Utils框架的使用