Codeforces 763A. Timofey and a tree
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的更多相关文章
- 763A - Timofey and a tree
A. Timofey and a tree time limit per test 2 seconds memory limit per test 256 megabytes input standa ...
- 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 ...
- cf 763A. Timofey and a tree
呵呵呵,直接判断是不是一个点连起来所有的特殊边(连接2不同颜色的点的边) (一开始还想各种各样奇怪的dfs...垃圾) #include<bits/stdc++.h> #define LL ...
- 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 764C】Timofey and a tree
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 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 题意 给出一棵树,每条边上有一个字符,字符集大小只 ...
- codeforces 812E Sagheer and Apple Tree(思维、nim博弈)
codeforces 812E Sagheer and Apple Tree 题意 一棵带点权有根树,保证所有叶子节点到根的距离同奇偶. 每次可以选择一个点,把它的点权删除x,它的某个儿子的点权增加x ...
- codeforces 220 C. Game on Tree
题目链接 codeforces 220 C. Game on Tree 题解 对于 1节点一定要选的 发现对于每个节点,被覆盖切选中其节点的概率为祖先个数分之一,也就是深度分之一 代码 #includ ...
- Codeforces E. Alyona and a tree(二分树上差分)
题目描述: Alyona and a tree time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
随机推荐
- 利用MVC的Area作为二级域名
此处使用的域名是我改了系统的hosts文件达到的 测试成功! 全局的注册方式 在Area的注册文件里进行配置 一个Area和一个外部的Controller 废话不多说,提供DEMO 下载地址
- Go:定时执行任务time.sleep和time.tick的优劣
golang 写循环执行的定时任务,常见的有以下三种实现方式:1.time.Sleep方法: for { time.Sleep(time.Second) fmt.Println("我在定时执 ...
- 51nod1315(位运算)
题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1315 题意:中文题诶- 思路:位或(|)运算是二进制位有一个 ...
- IDEA开发Spark的漫漫摸索(一)
系统:Win10 01 安装IDEA IDEA版本:IntelliJ IDEA 2017.2.1 64位 使用的学生授权下载的ultimate版本,此处不赘叙安装过程. 02安装编译环境 Spark可 ...
- linux网络配置详解
一:相关网络配置的文件 1.网卡名配置相关文件 网卡名命名规则文件: /etc/udev/rules.d/70-persistent-net.rules # PCI device 0x8086:0x1 ...
- CDN-内容发布网络
整理<CDN技术详解>一书中重要的内容. 互联网与万维网 广义的互联网,由两层组成:一层是以TCP/IP为代表的网络层:另一层是以万维网WWW为代表的应用层.辨识互联网和万维网的区别,是认 ...
- GUI的最终选择 Tkinter(五):Text用法
Text组件 绘制单行文本使用Label组件,多行选使用Listbox,输入框使用Entry,按钮使用Button组件,还有Radiobutton和Checkbutton组件用于提供单选或多选的情况, ...
- python_魔法方法(二):算术运算
python2.2之后,对类和类型做了同意,将int().float().str().list().touple()这些BIF转换为工厂函数 >>> type(len) <cl ...
- Spring Boot 测试 junit
import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.ann ...
- 迷你迅雷+SqlServer2008r2下载
迷你迅雷下载 http://down.sandai.net/mini/MiniThunderInstaller3.1.1.58.exe SqlServer 2008r2下载 ed2k://|file| ...