• [POI2008]STA-Station


    传送门

    题目的描述非常清楚,数据范围是106。暴力的方法很好想……O(n2)暴搜……

    这个数据范围基本就是明示你要O(n)去做这个题。我们考虑一下换根的时候发生的转移,先随便取一个根计算所有点的深度和(这个没难度),之后在换根的时候,现在的根的子树中所有节点深度-1,从它父亲那边过来的树所有节点深度+1,所以这个点的深度和就是max(sum[i],sum[i] + (n-size[i]) - size[i])

    这样DP一下得到最终结果即可。

    #include<cstdio>
    #include<algorithm>
    #include<cstring>
    #include<iostream>
    #include<cmath>
    #include<set>
    #include<queue>
    #define rep(i,a,n) for(int i = a;i <= n;i++)
    #define per(i,n,a) for(int i = n;i >= a;i--)
    #define enter putchar('
    ')
    
    using namespace std;
    typedef long long ll;
    const int M = 1000005;
    const int N = 500005;
    const int INF = 1000000009;
    
    int read()
    {
        int ans = 0,op = 1;
        char ch = getchar();
        while(ch < '0' || ch > '9')
        {
        if(ch == '-') op = -1;
        ch = getchar();
        }
        while(ch >= '0' && ch <= '9')
        {
        ans *= 10;
        ans += ch - '0';
        ch = getchar();
        }
        return ans * op;
    }
    
    struct edge
    {
        int next,to;
    }e[M<<1];
    
    int dep[M],n,x,y,head[M],ecnt,size[M],fa[M];
    ll sum[M],ans,tot;
    
    void add(int x,int y)
    {
        e[++ecnt].to = y;
        e[ecnt].next = head[x];
        head[x] = ecnt;
    }
    
    void dfs1(int x,int f,int depth)
    {
        dep[x] = sum[x] = depth,size[x] = 1,fa[x] = f;
        for(int i = head[x];i;i = e[i].next)
        {
        if(e[i].to == f) continue;
        dfs1(e[i].to,x,depth+1);
        sum[x] += sum[e[i].to],size[x] += size[e[i].to];
        }
    }
    
    void dfs2(int x)
    {
        for(int i = head[x];i;i = e[i].next)
        {
        if(e[i].to == fa[x]) continue;
        sum[e[i].to] = max(sum[e[i].to],sum[x] + n - (size[e[i].to] << 1));
        dfs2(e[i].to);
        }
        ans = max(ans,sum[x]);
    }
    
    int main()
    {
        n = read();
        rep(i,1,n-1) x = read(),y = read(),add(x,y),add(y,x);
        dfs1(1,0,1),tot = sum[1];
        dfs2(1);
        //rep(i,1,n) printf("%lld ",sum[i]);enter;
        rep(i,1,n) if(sum[i] == ans) printf("%d
    ",i),exit(0);
        return 0;
    }
  • 相关阅读:
    openstack trove实例状态转换条件--Mitaka版本
    trove module使用说明
    openstack trove mongodb配置项
    openstack trove weekly meeting时间即将更改
    openstack trove redis配置项
    trove database功能总结
    openstack trove 数据库镜像构建列表
    openstack trove主要贡献公司-Tesora被Stratoscale收购
    openstack trove,使pylint忽略错误
    十件你需要知道的事,关于openstack-trove(翻译)
  • 原文地址:https://www.cnblogs.com/captain1/p/9808322.html
Copyright © 2020-2023  润新知