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 ...
随机推荐
- mule优缺点和MEL
优点1.开源 https://github.com/mulesoft/mule2.丰富的connector ,可以通过不同的形式来连接各个系统JMS.Web Service.JDBC.HTTP等3.c ...
- kubernetes CRD开发指南
扩展kubernetes两个最常用最需要掌握的东西:自定义资源CRD 和 adminsion webhook, 本文教你如何十分钟掌握CRD开发. kubernetes允许用户自定义自己的资源对象,就 ...
- android ——悬浮按钮及可交互提示
一.悬浮按钮 FloatingActionButton是Design Support中的一个控件,它会默认colorAccent作为按钮的颜色,还可以给按钮一个图标. 这是没有图标的,这是有图标的. ...
- SonarQube部署及代码质量扫描入门教程
一.前言 1.本文主要内容 CentOS7下SonarQube部署 Maven扫描Java项目并将扫描结果提交到SonarQube Server SonarQube扫描报表介绍 2.环境信息 工具/环 ...
- 使用mybatis实现分页查询示例代码分析
*******************************************分页查询开始*************************************************** ...
- (三)c#Winform自定义控件-有图标的按钮
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...
- Vue-Router中History模式
目录 history路由 官方示例 Express中间件 客户端兜底404 示例代码托管在:http://www.github.com/dashnowords/blogs 博客园地址:<大史住在 ...
- IDEA编辑器
一.打开含有jsx语法的文件都会显示红线,提示export declarations are not supported bu current javascript version 解决办法: 二.I ...
- hmac模块和hashlib模块
hmac模块和hashlib模块 一.hash是什么 hash是一种算法(Python3.版本里使用hashlib模块代替了md5模块和sha模块,主要提供 SHA1.SHA224.SHA256. ...
- SpringBoot:Java High Level REST Client 搜索 API
Springboot整合最新版elasticSearch参考之前的文章:SpingBoot:整合ElasticSearch 7.2.0 Search API SearchRequest用于与搜索文档, ...