CodeForces-734E Anton and Tree 树的直径
题目大意:
给定一棵有n个节点的树,有黑点白点两种节点.
每一次操作可以选择一个同种颜色的联通块将其染成同一种颜色
现在给定一个初始局面问最少多少步可以让树变为纯色.
题解:
首先我们拿到这棵树时先将其缩点
然后我们手中的树就变成了一棵黑白相间的黑白树.
那么我们现在就是每次选择一个节点使其变色,都会使得这个节点相邻的所有节点合并进来.
所以我们找度数最大的合并就好了啊
我们现在把这棵树想象成由若干条路径组成的.
那么我们每次合并都会使某些路径的长度最多减少2
所以我们可以自然而然地想到一定是树的直径花费的操作次数最大.
所以我们将一棵树化作一条链上面连着许多其他的分支的形式.
手模几个样例就话发现答案实际上是\([\frac{len+1}{2}]\)len为直径长度.
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
inline void read(int &x){
    x=0;char ch;bool flag = false;
    while(ch=getchar(),ch<'!');if(ch == '-') ch=getchar(),flag = true;
    while(x=10*x+ch-'0',ch=getchar(),ch>'!');if(flag) x=-x;
}
const int maxn = 200010;
int belong[maxn],nodecnt;
int col[maxn];
struct Graph{
    struct Edge{
	int to,next;
    }G[maxn<<1];
    int head[maxn],cnt;
    void add(int u,int v){
	G[++cnt].to = v;
	G[cnt].next = head[u];
	head[u] = cnt;
    }
#define v G[i].to
    void dfs1(int u,int f){
	if(col[u] != col[f]) belong[u] = ++ nodecnt;
	else belong[u] = belong[f];
	for(int i = head[u];i;i=G[i].next){
	    if(v == f) continue;
	    dfs1(v,u);
	}return ;
    }
    int dis[maxn],p;
    void dfs2(int u,int f){
	for(int i = head[u];i;i=G[i].next){
	    if(v == f) continue;
	    dis[v] = dis[u] + 1;
	    dfs2(v,u);
	}
	if(dis[p] < dis[u]) p = u;
    }
#undef v
}g1,g2;
int main(){
    int n;read(n);
    for(int i=1;i<=n;++i) read(col[i]);
    for(int i=1,u,v;i<n;++i){
	read(u);read(v);
	g1.add(u,v);g1.add(v,u);
    }belong[1] = ++ nodecnt;
    g1.dfs1(1,1);
    for(int u=1;u<=n;++u){
	for(int i = g1.head[u];i;i=g1.G[i].next){
	    if(belong[g1.G[i].to] != belong[u]){
		g2.add(belong[u],belong[g1.G[i].to]);
	    }
	}
    }
    g2.dfs2(1,1);
    int u = g2.p;
    memset(g2.dis,0,sizeof g2.dis);
    g2.dfs2(u,u);
    int ans = (g2.dis[g2.p] + 1)/2;
    printf("%d\n",ans);
    return 0;
}
CodeForces-734E Anton and Tree 树的直径的更多相关文章
- Codeforces 734E Anton and Tree(缩点+树的直径)
		题目链接: Anton and Tree 题意:给出一棵树由0和1构成,一次操作可以将树上一块相同的数字转换为另一个(0->1 , 1->0),求最少几次操作可以把这棵数转化为只有一个数字 ... 
- Codeforces Round #379 (Div. 2) E. Anton and Tree 树的直径
		E. Anton and Tree time limit per test 3 seconds memory limit per test 256 megabytes input standard i ... 
- Codeforces 734E. Anton and Tree 搜索
		E. Anton and Tree time limit per test: 3 seconds memory limit per test :256 megabytes input:standard ... 
- Codeforces 379F New Year Tree  树的直径的性质推理
		New Year Tree 我们假设当前的直径两端为A, B, 那么现在加入v的两个儿子x, y. 求直径的话我们可以第一次dfs找到最远点这个点必定为直径上的点, 然而用这个点第二次dfs找到最远点 ... 
- CodeForces 734E Anton and Tree
		$dfs$缩点,树形$dp$. 首先将连通块缩点,缩点后形成一个黑白节点相间的树.接下来的任务就是寻找一个$root$,使这棵树以$root$为根,树的高度是最小的(也就是一层一层染色).树形$dp$ ... 
- Codeforces Round #379 (Div. 2) E. Anton and Tree 缩点 直径
		E. Anton and Tree 题目连接: http://codeforces.com/contest/734/problem/E Description Anton is growing a t ... 
- LightOJ1094 - Farthest Nodes in a Tree(树的直径)
		http://lightoj.com/volume_showproblem.php?problem=1094 Given a tree (a connected graph with no cycle ... 
- 【bzoj2870】最长道路tree  树的直径+并查集
		题目描述 给定一棵N个点的树,求树上一条链使得链的长度乘链上所有点中的最小权值所得的积最大. 其中链长度定义为链上点的个数. 输入 第一行N 第二行N个数分别表示1~N的点权v[i] 接下来N-1行每 ... 
- codeforces 14D(搜索+求树的直径模板)
		D. Two Paths time limit per test 2 seconds memory limit per test 64 megabytes input standard input o ... 
随机推荐
- python 基础 9.7 创建表
			一. 创建表 #/usr/bin/python #-*- coding:utf-8 -*- #@Time :2017/11/22 18:05 #@Auther :liuzhenchuan #@Fi ... 
- 【HTML5开发系列】CSS3
			选择器 属性 背景和边框 盒模型 布局 文本 动画 其他 
- mysql数据库访问授权
			1.进入MySQL服务器 d:\mysql\bin\> mysql -h localhost -u root; 2.赋予任何主机访问数据的权限 mysql> GRANT ALL PRIVI ... 
- The Princess and the Pea,摘自iOS应用Snow White and more stories
			Once upon a time there was a prince who wanted to marry a real princess.从前,有个王子想和真正的公主结婚. He looked ... 
- Git——版本控制概论(一)
			随着信息技术的发展,软件开发已不是小手工作坊,软件的规模和复杂度已经不再适合一个人单打独斗的开发了, 团队协作变得相当重要,如果没有VCS(版本控制系统Version Control System), ... 
- 图片加载ImageLoader
			https://github.com/nostra13/Android-Universal-Image-Loader public class AtguiguApplication extends A ... 
- rewrite_static
			<?php class MyObject { public static $myStaticVar = 0; function myMethod() { self::$myStaticVar + ... 
- ZOJ - 3705 Applications 【模拟】
			题目链接 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3705 题意 给出N个队员 然后一个教练要从中选择 M名队员 要选 ... 
- 网页中显示xml,直接显示xml格式的文件
			第一种方法 使用<pre></pre>包围代码(在浏览器中测试不行啊,但是在富编辑器中又可以,怪):使用<xmp></xmp>包围代码(官方不推荐,但是 ... 
- echarts相关设置
			1.显示隐藏工具栏 注释toolbox即可 /* toolbox: { show : true, feature : { dataView ... 
