codeforces708C
CF708C Centroids
题意翻译
给定一颗树,你有一次将树改造的机会,改造的意思是删去一条边,再加入一条边,保证改造后还是一棵树。
请问有多少点可以通过改造,成为这颗树的中心?(如果以某个点为根,每个子树的大小都小于n/2,则称某个点为中心)
sol:先找到整颗树的重心,然后对于每个点,肯定是把重心的最大子树串过来最优(XJByy一下),如果这个点在最大子树中,就搞来次大子树,或者把除了最大子树的剩下一大坨串过来
#include <bits/stdc++.h>
using namespace std;
typedef int ll;
inline ll read()
{
ll s=; bool f=; char ch=' ';
while(!isdigit(ch)) {f|=(ch=='-'); ch=getchar();}
while(isdigit(ch)) {s=(s<<)+(s<<)+(ch^); ch=getchar();}
return (f)?(-s):(s);
}
#define R(x) x=read()
inline void write(ll x)
{
if(x<) {putchar('-'); x=-x;}
if(x<) {putchar(x+''); return;}
write(x/); putchar((x%)+'');
}
#define W(x) write(x),putchar(' ')
#define Wl(x) write(x),putchar('\n')
const int N=,M=;
int n,rt,t1=,t2=,sz[N],mx[N];
int tot=,Next[M],to[M],head[N];
bool bo[N];
inline void Link(int x,int y)
{
Next[++tot]=head[x]; to[tot]=y; head[x]=tot;
}
inline void getrt(int x,int fat)
{
int e; sz[x]=; mx[x]=;
for(e=head[x];e;e=Next[e]) if(to[e]!=fat)
{
getrt(to[e],x); sz[x]+=sz[to[e]]; mx[x]=max(mx[x],sz[to[e]]);
}
mx[x]=max(mx[x],n-sz[x]); if(mx[x]<mx[rt]) rt=x;
}
inline void dfs(int x,int fat)
{
int e; sz[x]=;
for(e=head[x];e;e=Next[e]) if(to[e]!=fat)
{
dfs(to[e],x); sz[x]+=sz[to[e]];
}
}
inline void draw(int x,int fat)
{
int e; bo[x]=;
for(e=head[x];e;e=Next[e]) if(to[e]!=fat)
{
draw(to[e],x);
}
}
int main()
{
freopen("data.in","r",stdin);
int i,x,y;
R(n);
for(i=;i<n;i++)
{
R(x); R(y); Link(x,y); Link(y,x);
}
mx[rt=]=n+; sz[]=; getrt(,); dfs(rt,);
for(i=head[rt];i;i=Next[i])
{
if(sz[to[i]]>sz[t1]) t2=t1,t1=to[i];
else if(sz[to[i]]>sz[t2]) t2=to[i];
}
if(t1) draw(t1,rt);
// cout<<"rt="<<rt<<endl;
// for(i=1;i<=n;i++) cout<<bo[i]<<' '; puts("");
for(x=;x<=n;x++)
{
if(!bo[x])
{
if((n-sz[x]-sz[t1])*<=n) putchar('');
else putchar('');
}
else
{
if((n-sz[x]-sz[t2])*<=n) putchar('');
else if((n-sz[t1])*<=n) putchar('');
else putchar('');
}
putchar(' ');
}
return ;
}
codeforces708C的更多相关文章
- Codeforces708C Centroids 【树形dp】
题目链接 题意:给定一棵n个结点的树,问:对于每个结点,能否通过删除一条边并添加一条边使得仍是树,并且删除该结点后得到的各个连通分量结点数 <= n/2? 题解:树形dp,两遍dfs,第一遍df ...
- Codeforces-708C(树形DP)
一.题意 给定一颗树,对于每一个节点,判断能否在树中删除某一条边,然后在任意两个节点之间加一条边,使这个点成为重心. 注:删除树中某一条边后,标程并不会这么无聊地把这棵树变成两个孤立的连通图,而是再让 ...
随机推荐
- Spring实战(三)Spring中装配Bean的三种方式---XML、JavaConfig、AutoWire
创建应用对象之间协作关系的行为称为装配(wiring),这也是依赖注入的本质. Spring容器负责创建应用程序中的bean并通过DI来协调这些对象之间的关系,而开发者需要告诉Spring需要创建哪些 ...
- sql server 数据库中明明有值但是查询怎么都查不到值
产生原因是因为编码问题 数据库是英文版 但是数据库中数据又是中文的 所以查询中文时需要加上N select * from customer where Username=N'张三'
- PBE加密 .net 实现
using System; using System.Security.Cryptography; using System.Text; namespace Demo { internal class ...
- 【原创】大叔经验分享(77)openresty(nginx+lua)发http请求
openresty(nginx+lua)发http请求 利用location+proxy_pass间接实现 location ^~ /test/http { internal; proxy_pass ...
- Lua虚拟机中的数据结构与栈
Lua虚拟机中的数据结构与栈 来源 https://blog.csdn.net/zry112233/article/details/80828327 由上一篇文章可知解释器分析Lua文件之后生成Pro ...
- 【web安全】浅谈web安全之XSS
XSS定义 XSS, 即为(Cross Site Scripting), 中文名为跨站脚本, 是发生在目标用户的浏览器层面上的,当渲染DOM树的过程成发生了不在预期内执行的JS代码时,就发生了XSS攻 ...
- 远程操控批量复制应用(scp/pssh/pscp.pssh/rsync/pslurp)
scp命令: scp [options] SRC... DEST/两种方式: scp [options] [user@]host:/sourcefile /destpath scp [options] ...
- CAFFE(三):Ubuntu下Caffe框架安装(仅仅Caffe框架安装)
步骤一. 从github上下载(克隆)安装包 1.1 在你要安装的路径下 clone 此处我直接安装到home目录,执行: ~$ cd ~ 2 :~$ git clone https://github ...
- 018.查询练习50题(sql实例)
CREATE TABLE EMP(EMPNO numeric(5,0) NOT NULL primary key,--雇员的编号ENAME nvarchar(10) not null,--雇员的名字J ...
- Centos7.4安装RabbitMQ
1.1 安装RabbitMQ 1.1.1 系统环境 [root@rabbitmq ~]# cat /etc/redhat-release CentOS Linux release 7.4.1708 ( ...