A. Timofey and a tree

题意:给一棵树,要求判断是否存在一个点,删除这个点后,所有连通块内颜色一样。$N,C \le 10^5$

想法:这个叫换根吧。先求出一个点合法即其儿子的子树内颜色一样,非该点子树的点颜色都一样。可以用DFS序解决。

#include< cstdio >

typedef long long ll;
template
inline void read(T&x)
{
x=0;bool f=0;char c=getchar();
while((c<'0'||c>'9')&&c!='-') c=getchar();if(c=='-')f=1, c=getchar();
while(c>='0'&&c<='9'){x=x*10+c-'0'; c=getchar();}
x=f?-x:x;
}
const int MAXN(100010);
struct Node{int nd,nx;}bot[MAXN<<1];int tot,first[MAXN];
void add(int a,int b){bot[++tot]=(Node){b,first[a]};first[a]=tot;}
int n,a,b,col[MAXN],Eu[MAXN],size[MAXN],num[MAXN],last[MAXN],cnt,Ans;
bool dif[MAXN];
void DFS(int x,int f)
{
Eu[x]=++cnt; num[cnt]=x;size[x]=1;
for(int v=first[x];v;v=bot[v].nx)
if(bot[v].nd!=f)
{
DFS(bot[v].nd,x); size[x]+=size[bot[v].nd];
dif[x]|=dif[bot[v].nd]|(col[x]!=col[bot[v].nd]);
}
}
void DP(int x,int f)
{
bool bf=false;
for(int v=first[x];v;v=bot[v].nx)
if(bot[v].nd!=f)
{
DP(bot[v].nd,x);
bf|=dif[bot[v].nd];
}
if(!bf)
{
// fprintf(stderr,"%d\n",x);
int L=Eu[x]-1,R=Eu[x]+size[x];
// fprintf(stderr,"%d %d\n",L,R);
if((!L||last[1]>=L)&&(R>n||last[R]>=n)&&(!L||R>n||col[num[1]]==col[num[n]]))Ans=x;
}
}
int main()
{
// freopen("C.in","r",stdin);
read(n);
for(int i=1;i<n;i++)
{
read(a);read(b);
add(a,b);add(b,a);
}
for(int i=1;i<=n;i++)read(col[i]);
DFS(1,0);
for(int i=n;i>=1;i--)last[i]=(col[num[i]]==col[num[i+1]])?last[i+1]:i;
DP(1,0);
if(!Ans)printf("NO");
else printf("YES\n%d\n",Ans);
return 0;
}

Codeforces 763A. Timofey and a tree的更多相关文章

  1. 763A - Timofey and a tree

    A. Timofey and a tree time limit per test 2 seconds memory limit per test 256 megabytes input standa ...

  2. 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 ...

  3. cf 763A. Timofey and a tree

    呵呵呵,直接判断是不是一个点连起来所有的特殊边(连接2不同颜色的点的边) (一开始还想各种各样奇怪的dfs...垃圾) #include<bits/stdc++.h> #define LL ...

  4. 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 ...

  5. 【codeforces 764C】Timofey and a tree

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  6. codeforces 741D Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths(启发式合并)

    codeforces 741D Arpa's letter-marked tree and Mehrdad's Dokhtar-kosh paths 题意 给出一棵树,每条边上有一个字符,字符集大小只 ...

  7. codeforces 812E Sagheer and Apple Tree(思维、nim博弈)

    codeforces 812E Sagheer and Apple Tree 题意 一棵带点权有根树,保证所有叶子节点到根的距离同奇偶. 每次可以选择一个点,把它的点权删除x,它的某个儿子的点权增加x ...

  8. codeforces 220 C. Game on Tree

    题目链接 codeforces 220 C. Game on Tree 题解 对于 1节点一定要选的 发现对于每个节点,被覆盖切选中其节点的概率为祖先个数分之一,也就是深度分之一 代码 #includ ...

  9. Codeforces E. Alyona and a tree(二分树上差分)

    题目描述: Alyona and a tree time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

随机推荐

  1. 《Linux内核设计与实现》读书笔记(二)- 内核开发的准备

    在尝试内核开发之前,需要对内核有个整体的了解. 主要内容: 获取内核源码 内核源码的结构 编译内核的方法 内核开发的特点 1. 获取内核源码 内核是开源的,所有获取源码特别方便,参照以下的网址,可以通 ...

  2. 洛谷P3146 [USACO16OPEN]248

    P3146 [USACO16OPEN]248 题目描述 Bessie likes downloading games to play on her cell phone, even though sh ...

  3. iOS常用设计模式——工厂方法(简单工厂模式,工厂方法模式, 抽象工厂模式)

    1. 简单工厂模式 如何理解简单工厂,工厂方法, 抽象工厂三种设计模式? 简单工厂方法包含:父类拥有共同基础接口,具体子类实现子类特殊功能,工厂类根据参数区分创建不同子类实例.该场景对应的UML图如下 ...

  4. Jdk升级到11引起的问题:程序包javax.xml.bind.annotation不存在

    Jdk12 都发布了, 我也下载一个玩一玩吧.刚准备要下载,发现之前已经下载了一个11, 那就11 吧,也不用太新了. 安装了jdk11,习惯性的设置了一下环境变量: JAVA_HOME=D:\too ...

  5. 练习二十二:python兵乓求比赛顺序练习,关于连个兵乓球队进行比赛

    已知有两支兵乓球队进行比赛,每队各出3人: 甲队有a,b,c三人,乙队有x,y,z三人,已抽签决定比赛名单 问题:有人向队员打听比赛名单.a说他不和X比,c说他不和x,z比,程序找出比赛对手 方法一: ...

  6. Silverlight Telerik 学习之主题的设置

    Telerik控件版本:RadControls for Silverlight Q1 2013 当前的Telerik Silverlight 控件支持以下主题 Office Black - 这是默认主 ...

  7. 035 Search Insert Position 搜索插入位置

    给定一个排序数组和一个目标值,如果在数组中找到目标值则返回索引.如果没有,返回到它将会被按顺序插入的位置.你可以假设在数组中无重复元素.案例 1:输入: [1,3,5,6], 5输出: 2案例 2:输 ...

  8. eclipse ctrl+h

    之前我的eclipse使用ctrl + h 出现的总是Plug-in Search ,总是需要点那个小三角才能找到File Search ,深感使用不便, 修改方法:Window->Genera ...

  9. 整理一下postgresql的扩展功能postgis和pgrouting的使用

    postgis windows的下的安装使用postgresql的bin目录下的stackbuiler Ubuntu14.04下的安装: apt-get install postgresql-9.3- ...

  10. Python 装饰器的形成过程

    装饰器  定义:本质是函数,(装饰其他函数),即为其他函数添加附加功能.  原则: 1.不能修改被装饰的函数的源代码:            2.不能修改被装饰的函数的调用方式. 实现装饰器知识储备: ...