codeforces 764 C. Timofey and a tree(dfs+思维)
题目链接:http://codeforces.com/contest/764/problem/C
题意:给出一个树,然后各个节点有对应的颜色,问是否存在以一个点为根节点子树的颜色都一样。
这里的子树颜色都一样表示分出来的子树各自颜色一样。
就是随意找一个树叶节点然后一直遍历,直到找到一个和他颜色不一样的节点,那么要么取不一样的节点
为根或者取最后一个一样的节点为根。然后就dfs一遍。
#include <iostream>
#include <cstring>
#include <vector>
using namespace std;
const int M = 1e5 + 10;
int c[M] , ans , ans2;
vector<int>vc[M];
void dfs1(int sta , int pre , int co) {
if(c[sta] != co) {
ans = sta;
ans2 = pre;
return;
}
int len = vc[sta].size();
for(int i = 0 ; i < len ; i++) {
int v = vc[sta][i];
if(v != pre) {
dfs1(v , sta , co);
}
}
}
bool dfs(int sta , int pre , int co) {
if(c[sta] != co) {
return false;
}
int len = vc[sta].size();
int gg = true;
for(int i = 0 ; i < len ; i++) {
int v = vc[sta][i];
if(v != pre) {
if(!dfs(v , sta , co)) {
gg = false;
}
}
}
if(!gg) {
return false;
}
else {
return true;
}
}
int main() {
int n , u , v;
cin >> n;
for(int i = 0 ; i < n - 1 ; i++) {
cin >> u >> v;
vc[u].push_back(v);
vc[v].push_back(u);
}
for(int i = 1 ; i <= n ; i++) {
cin >> c[i];
}
int sta = 0;
for(int i = 1 ; i <= n ; i++) {
int len = vc[i].size();
if(len == 1) {
sta = i;
break;
}
}
ans = -1 , ans2 = -1;
dfs1(sta , sta , c[sta]);
if(ans == -1) {
cout << "YES" << endl;
cout << sta << endl;
}
else {
int len = vc[ans].size();
bool flag = true;
for(int i = 0 ; i < len ; i++) {
int v = vc[ans][i];
flag = dfs(v , ans , c[v]);
if(!flag)
break;
}
int flag1 = true;
len = vc[ans2].size();
for(int i = 0 ; i < len ; i++) {
int v = vc[ans2][i];
flag1 = dfs(v , ans2 , c[v]);
if(!flag1)
break;
}
if(flag || flag1) {
cout << "YES" << endl;
if(flag) {
cout << ans << endl;
return 0;
}
if(flag1) {
cout << ans2 << endl;
}
}
else {
cout << "NO" << endl;
}
}
return 0;
}
codeforces 764 C. Timofey and a tree(dfs+思维)的更多相关文章
- 【codeforces 764C】Timofey and a tree
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- Educational Codeforces Round 6 E. New Year Tree dfs+线段树
题目链接:http://codeforces.com/contest/620/problem/E E. New Year Tree time limit per test 3 seconds memo ...
- Codeforces Round #321 (Div. 2)C(tree dfs)
题意:给出一棵树,共有n个节点,其中根节点是Kefa的家,叶子是restaurant,a[i]....a[n]表示i节点是否有猫,问:Kefa要去restaurant并且不能连续经过m个有猫的节点有多 ...
- codeforces 682C C. Alyona and the Tree(dfs)
题目链接: C. Alyona and the Tree time limit per test 1 second memory limit per test 256 megabytes input ...
- Codeforces 760C:Pavel and barbecue(DFS+思维)
http://codeforces.com/problemset/problem/760/C 题意:有n个盘子,每个盘子有一块肉,当肉路过这个盘子的时候,当前朝下的这一面会被煎熟,每个盘子有两个数,p ...
- CodeForces 760 C. Pavel and barbecue(dfs+思维)
题目链接:http://codeforces.com/contest/760/problem/C 题意:一共有N个烤炉,有N个烤串,一开始正面朝上放在N个位子上.一秒之后,在位子i的串串会移动到pi位 ...
- Codeforces Round #395 (Div. 2) C. Timofey and a tree
地址:http://codeforces.com/contest/764/problem/C 题目: C. Timofey and a tree time limit per test 2 secon ...
- Codeforces 763A. Timofey and a tree
A. Timofey and a tree 题意:给一棵树,要求判断是否存在一个点,删除这个点后,所有连通块内颜色一样.$N,C \le 10^5$ 想法:这个叫换根吧.先求出一个点合法即其儿子的子树 ...
- 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 ...
随机推荐
- adb 下载安装
1.官网下载:选择自己电脑对应的版本 https://www.androiddevtools.cn/# SDK Tools, SDK platfrom Tools(解压在sdk 的根目录下) 2 ...
- Docker——理解好镜像和容器的关系
关注公众号,大家可以在公众号后台回复“博客园”,免费获得作者 Java 知识体系/面试必看资料. 镜像也是 docker 的核心组件之一,镜像时容器运行的基础,容器是镜像运行后的形态.前面我们介绍了 ...
- Danjgo学习笔记(一)
## 创建项目: 1. 通过命令行的方式:首先要进入到安装了django的虚拟环境中.然后执行命令: ``` django-admin startproject [项目的名称] ``` 这样就可以在当 ...
- SVG和canvas渲染的性能比较
1.什么是SVG? 描述: 一种使用XML描述的2D图形的语言 SVG基于XML意味着,SVG DOM中的每个元素都是可用的,可以为某个元素附加Javascript事件处理器. 在 SVG 中,每个被 ...
- JAVA基础知识(五)数据类型转换
当使用 +.-.*./.%.运算操作时,遵循如下规则: 1.只要两个操作数中有一个是double类型的,另一个将会被转换成double类型,并且结果也是double类型: 2.如果两个操作数中有一个 ...
- 100天搞定机器学习|Day21 Beautiful Soup
前情回顾 机器学习100天|Day1数据预处理 100天搞定机器学习|Day2简单线性回归分析 100天搞定机器学习|Day3多元线性回归 100天搞定机器学习|Day4-6 逻辑回归 100天搞定机 ...
- 8.12 day31 进程间通信 Queue队列使用 生产者消费者模型 线程理论 创建及对象属性方法 线程互斥锁 守护线程
进程补充 进程通信 要想实现进程间通信,可以用管道或者队列 队列比管道更好用(队列自带管道和锁) 管道和队列的共同特点:数据只有一份,取完就没了 无法重复获取用一份数据 队列特点:先进先出 堆栈特点: ...
- 防抖(debounce)和节流(throttle)
场景说明:一般我们在前端页面中会给元素绑定click.scroll.onmousemove.resize等事件,这些事件的执行函数如果是去发请求获取数据的话,我们无意识的连续点击或者连续滚动会给服务器 ...
- 从0到1发布一个npm包
从0到1发布一个npm包 author: @TiffanysBear 最近在项目业务中有遇到一些问题,一些通用的方法或者封装的模块在PC.WAP甚至是APP中都需要使用,但是对于业务的PC.WAP.A ...
- Python模块之ncclient
一.简介 此模块是是netconf协议的客户端,可与netconf服务端进行交互 二.实验环境 1.操作系统:win10 2.python版本:python3.6.6 3.ncclient模块版本:0 ...