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 ...
随机推荐
- 字符串中去除多余的空格保留一个(MS SQL Server)
大约2年前,写过一篇<字符串中去除多余的空格保留一个(C#)>https://www.cnblogs.com/insus/p/7954151.html 今天,Insus.NET使用MS S ...
- 机器学习十大算法之EM算法
此文已由作者赵斌授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 由于目前论坛的Markdown不支持Mathjax,数学公式没法正常识别,文章只能用截图上传了... ...
- C++ List的用法(转载)
Lists将元素按顺序储存在链表中. 与 向量(vectors)相比, 它允许快速的插入和删除,但是随机访问却比较慢. assign() 给list赋值 back() 返回最后一个元素 begin ...
- cf804C(dfs染色)
题目链接: http://codeforces.com/problemset/problem/804/C 题意: 有一颗含有 n 个顶点的树, 第 i 个顶点上有 k 个冰激凌, 每个冰激凌的种类为 ...
- PAT甲级——1099 Build A Binary Search Tree (二叉搜索树)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90701125 1099 Build A Binary Searc ...
- vue中v-bind绑定样式
近来发现v-bind绑定样式的两个好玩的栗子 可以直接绑定到一个样式对象,让模板更清晰: <div id="app"> <div v-bind:style=&qu ...
- redis常用
redis的key和string类型value限制均为512MB
- Mysql与Sql server,Sum函数跟Count函数
两者均是统计类函数,都不计算NULL字段!!! 单纯计算行数的话,count的效率比sum的效率高 MySQL SUM()函数介绍 SUM()函数用于计算一组值或表达式的总和,SUM()函数的语法如下 ...
- 多重背包(dp专题)
题目大意:输入n,代表有n种数,接下来n个数代表n种数,再接下来n个数代表每种数有多少个,在输入K,代表用这些数要加成的和 问你是否能加为K,能输出yes,不能输出no 这是一个典型的多重背包问题,可 ...
- Spring Boot 整合 Hibernate5
Run java -jar -Dspring.profiles.active=dev sport.web.services.jar Maven <parent> <groupId&g ...