【Codeforces708C】【树形dp】【贪心】Centroids

Centroids

Time Limit: 4000MS Memory Limit: 524288KB 64bit IO Format: %I64d & %I64u

Description

Tree is a connected acyclic graph. Suppose you are given a tree consisting of n vertices. The vertex of this tree is called centroid if the size of each connected component that appears if this vertex is removed from the tree doesn’t exceed .
You are given a tree of size n and can perform no more than one edge replacement. Edge replacement is the operation of removing one edge from the tree (without deleting incident vertices) and inserting one new edge (without adding new vertices) in such a way that the graph remains a tree. For each vertex you have to determine if it’s possible to make it centroid by performing no more than one edge replacement.

Input

The first line of the input contains an integer n (2 ≤ n ≤ 400 000) — the number of vertices in the tree. Each of the next n - 1 lines contains a pair of vertex indices ui and vi (1 ≤ ui, vi ≤ n) — endpoints of the corresponding edge.

Output

Print n integers. The i-th of them should be equal to 1 if the i-th vertex can be made centroid by replacing no more than one edge, and should be equal to 0 otherwise.

Samples

Input1

3
1 2
2 3

Output1

1 1 1

Input2

5
1 2
1 3
1 4
1 5

Output2

1 0 0 0 0

Hint

In the first sample each vertex can be made a centroid. For example, in order to turn vertex 1 to centroid one have to replace the edge (2, 3) with the edge (1, 3).

Source

AIM Tech Round 3 (Div. 1)

先说题意:给定一棵n 个结点的树,对于每个结点求出它是否能通过至多一次操作,使其成为这棵树的重心。一次操作可以删去原树中一条边,然后再连上任意一条边,要求操作后图仍是一棵n 个结点的树

这个题有两个做法,一个是贪心,一个是树规,一个一个来说

假设重心为根,那么所有儿子节点的节点个数都不会比n/2大,相反的,如果所有和一个点相连的节点的节点个数都不比n/2大,那么这个节点一定是重心,然后对于一棵树,重心最多有两个
然后下面我们分别来看两种算法,判断一个点能不能成为重心都是用的上面的那个方法


先是贪心的做法,我们先来看题,题意思实际上就是改变一颗子树的父亲,如果要让当前点成为重心,显然应该讲要改变的子树的父亲改为当前搜索的节点
所以我们先找到树的重心,然后以重心为根重新建树,这样每根的每个儿子节点的节点个数都不会比n/2大,在判断每一个节点的时候,先判断他是不是来自根节点节点数最多的那棵子树,如果是,就判断他是那个面的点减去根节点次大的子树结点个数是不是大于n/2,小于就说明可以改为重心
同样的,如果这个节点不是来自根节点最大的那一颗子树,那么就判断它如果把根节点最大的一颗子树接到他的上面那么他会不会成为重心
大概就是这样,算法时间复杂度O(n)


然后我们来看树规,树规实际上我认为这里是没有想到用重心来建树的一个劣势做法,大体的思路就是两次DFS,第一次从下向上,判断每个点如果从下面找一颗子树给他接上去能不能成为重心,第二次DFS从上向下,判断每一个节点如果从上面给他接一颗子树能不能成为重心
思路看起来比贪心简单的多,实际上写起来要复杂许多,具体我就不说了,可以自己去体会


然后下面是我的代码,我两个都写了一下,供大家参考

先是贪心:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<set>
#include<map>
#include<queue>
#include<algorithm>
#include<vector>
#include<cstdlib>
#include<cmath>
#include<ctime>
#include<stack>
#define INF 2100000000
#define ll long long
#define clr(x)  memset(x,0,sizeof(x))
#define clrmax(x)  memset(x,127,sizeof(x))

using namespace std;

inline int read()
{
    char c;
    int ret=0;
    while(!(c>='0'&&c<='9'))
        c=getchar();
    while(c>='0'&&c<='9')
    {
        ret=(c-'0')+(ret<<1)+(ret<<3);
        c=getchar();
    }
    return ret;
}

#define M 400005

int first[M],next[M*2],to[M*2];
int fa[M],root,size[M],can[M],c,n,t;
int fir,sec,p_fir;

void addedge(int s,int v)
{
    next[++t]=first[s];
    first[s]=t;
    to[t]=v;
}

void bulid(int x)
{
    size[x]=1;
    int b=1;
    for(int i=first[x];i;i=next[i])
    {
        int v=to[i];
        if(fa[x]==v)continue;
        fa[v]=x;
        bulid(v);
        if(size[v]>n/2)b=0;
        size[x]+=size[v];
    }
    if(b&&size[x]-1>=(n-1)/2)c=x;
}

void get_fir_sec()
{
    int x=root;
    for(int i=first[x];i;i=next[i])
    {
        int v=to[i];
        if(size[v]>fir)
        {
            fir=size[v];
            p_fir=v;
        }
    }
    for(int i=first[x];i;i=next[i])
    {
        int v=to[i];
        if(size[v]>sec&&v!=p_fir)
            sec=size[v];
    }
}

void dfs(int x,int b)
{
    if(x==p_fir)b=1;
    if(b)size[x]+=sec;
    else size[x]+=fir;
    if(size[x]-1>=(n-1)/2)can[x]=1;
    if(b==1&&fir*2==n)can[x]=1;
    for(int i=first[x];i;i=next[i])
    {
        int v=to[i];
        if(v==fa[x])continue;
        dfs(v,b);
    }
}

int main()
{
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
    n=read();
    for(int i=1;i<n;i++)
    {
        int a=read(),b=read();
        addedge(a,b);
        addedge(b,a);
    }
    bulid(1);
    root=c;
    if(root!=1)
    {
        clr(fa);clr(size);
        bulid(root);
    }
    get_fir_sec();
    dfs(root,0);
    for(int i=1;i<=n;i++)
        printf("%d ",can[i]);
    return 0;
}

然后是树规:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<set>
#include<map>
#include<queue>
#include<algorithm>
#include<vector>
#include<cstdlib>
#include<cmath>
#include<ctime>
#include<stack>
#define INF 2100000000
#define ll long long
#define clr(x)  memset(x,0,sizeof(x))
#define clrmax(x)  memset(x,127,sizeof(x))

using namespace std;

inline int read()
{
    char c;
    int ret=0;
    while(!(c>='0'&&c<='9'))
        c=getchar();
    while(c>='0'&&c<='9')
    {
        ret=(c-'0')+(ret<<1)+(ret<<3);
        c=getchar();
    }
    return ret;
}

#define M 400005

int first[M],next[M*2],to[M*2];
int fa[M],size[M],can[M],c,n,t;
int fir[M],sec[M],fir_p[M];

void addedge(int s,int v)
{
    next[++t]=first[s];
    first[s]=t;
    to[t]=v;
}

void bulid(int x)
{
    size[x]=1;
    int b=1;
    for(int i=first[x];i;i=next[i])
    {
        int v=to[i];
        if(fa[x]==v)continue;
        fa[v]=x;
        bulid(v);
        if(size[v]<=n/2&&size[v]>fir[x])
        {
            fir[x]=size[v];
            fir_p[x]=v;
        }
        if(fir[v]>fir[x])
        {
            fir[x]=fir[v];
            fir_p[x]=v;
        }
        if(size[v]>n/2)b=0;
        size[x]+=size[v];
    }
    for(int i=first[x];i;i=next[i])
    {
        int v=to[i];
        if(v==fa[x]||v==fir_p[x])continue;
        if(size[v]<=n/2&&size[v]>sec[x])
            sec[x]=size[v];
        if(fir[v]>sec[x])
            sec[x]=fir[v];
    }
    if(b&&size[x]-1>=(n-1)/2)can[x]=1;
    if(size[x]>=n/2&&size[fir_p[x]]-fir[x]<=n/2)can[x]=1;
}

void dfs(int x,int mx,int up)
{
    int te=mx;
    if(size[x]<=n/2&&(n-size[x]-mx)<=n/2)can[x]=1;
    for(int i=first[x];i;i=next[i])
    {
        mx=te;
        int v=to[i];
        if(v==fa[x])continue;
        int down=up+size[x]-size[v];
        if(down<=n/2)mx=max(mx,down);
        if(v==fir_p[x])mx=max(mx,sec[x]);
        else mx=max(mx,fir[x]);
        dfs(v,mx,down);
    }
}

int main()
{
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
    n=read();
    for(int i=1;i<n;i++)
    {
        int a=read(),b=read();
        addedge(a,b);
        addedge(b,a);
    }
    bulid(1);
    dfs(1,0,0);
    for(int i=1;i<=n;i++)
        printf("%d ",can[i]);
    return 0;
}

大概就是这个样子,如果有什么问题,或错误,请在评论区提出,谢谢。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值