【题目】C. Centroids

【题意】给定一棵树,求每个点能否通过 [ 移动一条边使之仍为树 ] 这一操作成为树的重心。n<=4*10^5。

【算法】树的重心

【题解】若树存在双重心,则对于任何一个点将另一边的n/2个点取下来接上去即可,均成立。

若树为单重心,假设w为树的重心及根,x为当前考虑节点。

由于w是重心,x的父亲这棵子树必定超过n/2,此时最优策略只能在(w,son[w])中砍下最大的一棵子树接到x下面(除了x所在子树)。

复杂度O(n)。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=;
int first[maxn],tot,size[maxn],n,p,top[maxn];
int mx,mxi,mi,mii;
bool ans[maxn],z[maxn];
struct edge{int v,from;}e[maxn*];
void insert(int u,int v){tot++;e[tot].v=v;e[tot].from=first[u];first[u]=tot;}
void dfs(int x,int fa){
size[x]=;z[x]=;
for(int i=first[x];i;i=e[i].from)if(e[i].v!=fa){
dfs(e[i].v,x);
size[x]+=size[e[i].v];
if(size[e[i].v]>n/)z[x]=;
}
if(n-size[x]>n/)z[x]=;
if(z[x]){if(p)p=;else p=x;}
}
void DFS(int x,int fa,int tp){
size[x]=;top[x]=tp;
for(int i=first[x];i;i=e[i].from)if(e[i].v!=fa){
DFS(e[i].v,x,tp);
size[x]+=size[e[i].v];
}
}
int main(){
scanf("%d",&n);
int u,v;
for(int i=;i<n;i++){
scanf("%d%d",&u,&v);
insert(u,v);insert(v,u);
}
dfs(,);
if(!p){for(int i=;i<=n;i++)printf("1 ");return ;}
for(int i=first[p];i;i=e[i].from){
DFS(e[i].v,p,e[i].v);
if(size[e[i].v]>mx){
mi=mx;mii=mxi;
mx=size[e[i].v];
mxi=e[i].v;
}
else if(size[e[i].v]>mi)mi=size[e[i].v],mii=e[i].v;
}
ans[p]=;
for(int i=;i<=n;i++)if(i!=p){
if(mxi==top[i])u=mi;else u=mx;
if(n-size[i]-u<=n/)ans[i]=;
}
for(int i=;i<=n;i++)printf("%d ",ans[i]);
return ;
}

【CodeForces】708 C. Centroids 树的重心的更多相关文章

  1. Codeforces 1182D Complete Mirror 树的重心乱搞 / 树的直径 / 拓扑排序

    题意:给你一颗树,问这颗树是否存在一个根,使得对于任意两点,如果它们到根的距离相同,那么它们的度必须相等. 思路1:树的重心乱搞 根据样例发现,树的重心可能是答案,所以我们可以先判断一下树的重心可不可 ...

  2. Kay and Snowflake CodeForces - 686D (树的重心性质)

    After the piece of a devilish mirror hit the Kay's eye, he is no longer interested in the beauty of ...

  3. CodeForces - 686D 【树的重心】

    传送门:http://codeforces.com/problemset/problem/686/D 题意:给你n个节点,其中1为根, 第二行给你2~n的节点的父亲节点编号. 然后是q个询问,求询问的 ...

  4. codeforces 685B Kay and Snowflake 树的重心

    分析:就是找到以每个节点为根节点的树的重心 树的重心可以看这三篇文章: 1:http://wenku.baidu.com/link?url=yc-3QD55hbCaRYEGsF2fPpXYg-iO63 ...

  5. Codeforces Round #268 (Div. 1) 468D Tree(杜教题+树的重心+线段树+set)

    题目大意 给出一棵树,边上有权值,要求给出一个1到n的排列p,使得sigma d(i, pi)最大,且p的字典序尽量小. d(u, v)为树上两点u和v的距离 题解:一开始没看出来p需要每个数都不同, ...

  6. codeforces 701E E. Connecting Universities(树的重心)

    题目链接: E. Connecting Universities time limit per test 3 seconds memory limit per test 256 megabytes i ...

  7. D. Kay and Snowflake 树的重心

    http://codeforces.com/contest/686/problem/D 给出q个询问,每次要求询问以x为根的子树中,哪一个点是重心. 树的重心:求以cur为根的子树的重心,就是要找一个 ...

  8. HCW 19 Team Round (ICPC format) B. Beggin' For A Node(树的重心,交互题)

    B. Beggin' For A Node time limit per test2.0 s memory limit per test256 MB inputstandard input outpu ...

  9. POJ3107Godfather[树形DP 树的重心]

    Godfather Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6121   Accepted: 2164 Descrip ...

随机推荐

  1. Redis 简要介绍--用于讲解消息中间件

    1:安装 Redis yum install -y redis   2:编辑配置文件/etc/redis.conf,Redis作为一个消息中间件,那么应该监听于本机的外网socket上,因此修改 bi ...

  2. springmvc值传递

    1.页面向后台传值 A.HttpServletRequest方式: package com.rong.controller; import javax.annotation.Resource; imp ...

  3. ant build.xml 解释!

    Ant的概念  Make命令是一个项目管理工具,而Ant所实现功能与此类似.像make,gnumake和nmake这些编译工具都有一定的缺陷,但是Ant却克服了这些工具的缺陷.最初Ant开发者在开发跨 ...

  4. 常用的一些sql

    --根据某一列中包括的逗号将一行数据变多行 select a,c from (with test as (select 'abc' a,'1,2,3' c from dual e) select a, ...

  5. Debugger DataSet 调试时查看DataSet

    delphi  跟踪调试的时候查看DataSet数据记录 Ctrl+F7调试 增强工具DataSethttp://edn.embarcadero.com/article/40268 http://do ...

  6. 第210天:node、nvm、npm和gulp的安装和使用详解

    一.node 1.什么是node? 它不是JS文件,也不是JS框架,而是Server side JavaScript runtime,当服务端的一个JS文件运行时,会被NODE拦截,在NODE中运行J ...

  7. H5跳转到百度地图并定位

    找了半天的JS api,发现没有,后来发现这个叫 url api,让我好找. 官方文档: http://lbsyun.baidu.com/index.php?title=uri/api/web : 简 ...

  8. json 当集合类型的字符串变成集合时候 里面有map类型的转换操作

  9. 【HDU4471】Homework(矩阵快速幂)

    [HDU4471]Homework(矩阵快速幂) 题面 Vjudge 给定一个数列的前\(m\)项,给定一个和前\(t\)项相关的递推式. 有\(q\)个位置的递推式单独给出,求数列第\(n\)项. ...

  10. 【BZOJ 2754 喵星球上的点名】

    Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 2512  Solved: 1092[Submit][Status][Discuss] Descript ...