【题目】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. rsyslog配置文件详解(rsyslog.conf)

    # rsyslog configuration file # For more information see /usr/share/doc/rsyslog-*/rsyslog_conf.html # ...

  2. 0429团队项目-Scrum团队成立

    Scrum团队成立 团队名称:开拓者 团队目标:努力让每一个小伙伴在学会走路的基础上学会跑. 团队口号:我们要的只是这片天而已. 团队照:正面照+背影照(那就是为什么组名叫开拓者) 5.2 角色分配 ...

  3. Spring的初始化:org.springframework.web.context.ContextLoaderListener

    在web.xml中配置 <listener>    <listener-class>org.springframework.web.context.ContextLoaderL ...

  4. http://deepdish.io/2015/04/28/creating-lmdb-in-python/

    http://deepdish.io/2015/04/28/creating-lmdb-in-python/

  5. 第15章 磁盘配额(Quota)与高级文件系统管理

    磁盘配额(quota)的应用与实践 什么是quota 举例来说,用户的默认主文件夹是在/home下面,如果/home是个独立的分区,假设是10G,/home下有30个账号,这样30个用户共享这10G的 ...

  6. SpringMVC项目中获取所有URL到Controller Method的映射

    Spring是一个很好很强大的开源框架,它就像是一个容器,为我们提供了各种Bean组件和服务.对于MVC这部分而言,它里面实现了从Url请求映射控制器方法的逻辑处理,在我们平时的开发工作中并不需要太多 ...

  7. 蜗牛慢慢爬 LeetCode 2. Add Two Numbers [Difficulty: Medium]

    题目 You are given two non-empty linked lists representing two non-negative integers. The digits are s ...

  8. 【第九周】psp

        代码累计 300+575+475+353+620=2223 随笔字数 1700+3000+3785+4210+4333=17695 知识点 java反射机制 数据库技术 动态规划算法 pyth ...

  9. DRM Study

    1.DRM是什么? DRM,英文全称Digital Rights Management, 可以翻译为:数字版权管理.指的是出版者用来控制被保护对象的使用权的一些技术,这些技术保护的有数字化内容(例如: ...

  10. Ants UVA - 1411(km板题竟然让我换了个板子)

    题意: 给出n个白点和n个黑点的坐标,要求用n条不相交的线段把它们连接起来,其中每条线段恰好连接一个白点和一个黑点,每个点恰好连接到一条线段 解析: 带入负的欧几里得距离求就好了 假设a1-b1 与 ...