[Codeforces734E]Anton and Tree 树的直径
来源:互联网 发布:企业号码搜索软件 编辑:程序博客网 时间:2024/05/29 18:46
好神的题
做法:对于每一条边E(u,v),若两点颜色相同那么边权为0,否则为1,输出树的直径/2上取整
证明:
边权为0相当于将两个点并成一个点,那么树上的点相当于已经进行了黑白染色,对于树的直径,至少需要树的直径/2,上取整,才能染成同一颜色,做法即对直径的中点染色即可
#include <iostream>#include <cstdio>#include <cstring>#include <vector>#define N 200050using namespace std;int dep[N],vis[N],a[N],mx,n,x,cnt;int h[N];struct Edge{ int a,b,v,next; }e[2*N];void add(int a,int b,int v) { e[++cnt].a = a; e[cnt].b = b; e[cnt].v = v; e[cnt].next = h[a]; h[a] = cnt;}void dfs(int u) { vis[u] = 1; if (dep[u] > mx) mx = dep[u] , x = u; for (int i=h[u];i;i=e[i].next) { int v = e[i].b; if (vis[v]) continue; dep[v] = dep[u] + e[i].v; dfs(v); }}int main() { scanf("%d",&n); for (int i=1;i<=n;i++) scanf("%d",&a[i]); for (int i=1;i<=n-1;i++) { int u,v; scanf("%d%d",&u,&v); add(u,v,a[u]^a[v]); add(v,u,a[u]^a[v]); } memset(dep,0,sizeof(dep)); memset(vis,0,sizeof(vis)); mx = 0 , dep[1] = 0 , dfs(1); memset(dep,0,sizeof(dep)); memset(vis,0,sizeof(vis)); mx = 0 , dep[x] = 0 , dfs(x); printf("%d\n",(mx+1)/2); return 0;}
0 0
- [Codeforces734E]Anton and Tree 树的直径
- 【CodeForces734E】【缩点】Anton and Tree 题解
- Codeforces-734E Anton and Tree(树的直径)
- Codeforces734E【缩点+树的直径】
- E. Anton and Tree(缩点+树直径模板)
- 思路题,树的直径(Anton and Tree,cf 734E)
- Codeforces 734E. Anton and Tree By Assassin 缩点+树的最大直径
- Codeforces 734E Anton and Tree【并查集缩点+树的直径】好题~
- Codeforces 734E Anton and Tree (dfs缩点+树的直径)
- Codeforces 734E Anton and Tree【并查集缩点||DFS缩点+树的直径】
- CF734E:Anton and Tree(缩点 & 直径)
- Anton and Tree CodeForces
- CodeForces 734E - Anton and Tree
- 【27.91%】【codeforces 734E】Anton and Tree
- CodeForces734E
- HDU 3534 Tree(dfs统计树的直径的数量)
- 【codeforces 734E】Anton and Tree【缩点+DP】
- CodeForces 734 E.Anton and Tree(dfs)
- javascript 笔记
- 构建基于 JPA 的 Hibernate 环境
- Leetcode-61. Rotate List
- selenium-java-Firefox 特殊异常:界面找不到元素
- 【通信框架】Google的开源通信框架protobuf概述
- [Codeforces734E]Anton and Tree 树的直径
- 《白说》---- 读书笔记
- spring中bean的生命周期详解
- vim各种命令
- Unity如何区分安卓、苹果设备是手机与平板?
- JAVA SE IO个人小结
- 【Android】结束活动退出程序的方法
- centos 6安装tensorflow
- .net aspose.word导出world生成成列表及合并单元格