zoukankan      html  css  js  c++  java
  • [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;
    }
  • 相关阅读:
    Running ASP.NET Applications in Debian and Ubuntu using XSP and Mono
    .net extjs 封装
    ext direct spring
    install ubuntu tweak on ubuntu lts 10.04,this software is created by zhouding
    redis cookbook
    aptana eclipse plugin install on sts
    ubuntu open folderpath on terminal
    ubuntu install pae for the 32bit system 4g limited issue
    EXT Designer 正式版延长使用脚本
    用 Vagrant 快速建立開發環境
  • 原文地址:https://www.cnblogs.com/captain1/p/9808322.html
Copyright © 2011-2022 走看看