乍看之下感觉有点无从下手,,其实是个很简单的水题,陷入僵局

题目大意:给一棵树,树上每个节点都染色,问能否取下一个节点,使得剩余所有子树上的点的颜色都相同。能输出YES和取下的节点编号,否则输出NO。

解题思路:感觉无从下手,结果xjb找找情况画画图脑补个策略就行了——

  随便从一个点开始进行搜索,如果和它颜色不同的相邻节点个数(除去父节点)大于等于2,那答案肯定是它没跑了,如果在这之前已经确定下了一个答案并且这个答案不是自己,咖喱给给。

  如果和它颜色不同的相邻节点个数(除去父节点)为1,那么令答案为该相邻节点。同样如果已经确定答案但是答案不是自己,gg。

  随便测了几组数据没问题就交了过了。

  如果全部节点都是一个颜色,则出现flag为1而res尚未被赋值,特判输出之。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long ll;
#define sqr(x) ((x)*(x))
const int N=2e5+;
int head[N],to[N<<],nxt[N<<],cnt;
int n,c[N],res,flag;
void init(){
memset(head,-,sizeof(head));
cnt=res=;
flag=;
}
void addEdge(int u,int v){
nxt[cnt]=head[u];
to[cnt]=v;
head[u]=cnt++;
}
void dfs(int u,int pre){
if(!flag) return;
int tot=,id;
for(int i=head[u];~i;i=nxt[i]){
int v=to[i];
if(v==pre) continue;
if(c[u]!=c[v]){
tot++;
id=v;
}
}
if(tot>){
if(tot==){
if(res){
if(res!=u) flag=;
}
else res=id;
}
else{
if(res&&res!=u) flag=;
else res=u;
}
}
for(int i=head[u];~i;i=nxt[i]){
int v=to[i];
if(v==pre) continue;
dfs(v,u);
}
}
int main(){
//freopen("in.txt","r",stdin);
while(~scanf("%d",&n)){
init();
for(int i=;i<n;i++){
int u,v;
scanf("%d%d",&u,&v);
addEdge(u,v),addEdge(v,u);
}
for(int i=;i<=n;i++)
scanf("%d",c+i);
dfs(,);
if(flag) printf("YES\n%d\n",res?res:);
else puts("NO");
}
return ;
}

Codeforces 763A的更多相关文章

  1. Codeforces 763A. Timofey and a tree

    A. Timofey and a tree 题意:给一棵树,要求判断是否存在一个点,删除这个点后,所有连通块内颜色一样.$N,C \le 10^5$ 想法:这个叫换根吧.先求出一个点合法即其儿子的子树 ...

  2. CodeForces - 763A(并查集/思维)

    题意 https://vjudge.net/problem/CodeForces-763A 一棵无根树中各个节点被染上了一种颜色c[i] 现在让你选择一个点作为根节点,使得这个根节点的所有儿子满足以该 ...

  3. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  4. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  5. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  6. 【Codeforces 738A】Interview with Oleg

    http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...

  7. CodeForces - 662A Gambling Nim

    http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...

  8. CodeForces - 274B Zero Tree

    http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...

  9. CodeForces - 261B Maxim and Restaurant

    http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...

随机推荐

  1. 可以通过dict[key]获得dict[value]

    dict={key:value,key2:value2} print (dict[key] )    得到的是 dict[value] # 软文预存接口,通过key来预览未保存的软文,联查商品.kol ...

  2. 使用js将Unix时间戳转换为普通时间

    var unixtime=1358932051;formatTime (time) { let unixtime = time let unixTimestamp = new Date(unixtim ...

  3. 2018百度之星资格赛T2 子串查询

    [题解] 很容易想到暴力做法:对于每个询问暴力查找区间内的最小字母,统计其出现次数.效率O(N^2),无法通过全部数据. 我们可以换一个思路,设f[i][j]为第i个字母(字母‘A'到’Z'分别对应0 ...

  4. uva 1444 Knowledge for the masses

    uva 1444 Description   You are in a library equipped with bookracks that move on rails. There are ma ...

  5. 3.5.6 关系和boolean运算符

        Java包含丰富的关系运算符.要检测相等性,可以使用两个等号 == .例如, 3 == 7  的值为 false.       另外可以使用 != 检测不相等.例如,  3  ! = 7 的值 ...

  6. Python基础(四) 基础拾遗、数据类型进阶

    一.基础拾遗 (一).变量作用域 外层变量,可以被内层变量直接调用:内层变量,无法被外层变量使用.这种说法在其它语言中适用,在python中除了栈以外,正常的变量作用域,只要执行声明并在内存中存在,该 ...

  7. Why does MySQL produce so many temporary MYD files?

    http://dba.stackexchange.com/questions/30505/why-does-mysql-produce-so-many-temporary-myd-files Data ...

  8. HDU 2242 连通分量缩点+树形dp

    题目大意是: 所有点在一个连通图上,希望去掉一条边得到两个连通图,且两个图上所有点的权值的差最小,如果没有割边,则输出impossible 这道题需要先利用tarjan算法将在同一连通分量中的点缩成一 ...

  9. hdu 1874 dijkstra 队列实现 比数组高效特别在稀疏图

    参考  http://blog.csdn.net/zhuyingqingfen/article/details/6370561 刘汝佳白皮书 #include<stdio.h> #incl ...

  10. 转载 - C++ - placement new

    出处:http://www.cnblogs.com/wanghetao/archive/2011/11/21/2257403.html 有关placement new                  ...