题解

1 条题解

  • 0
    @ 2017-10-11 19:54:25

    深搜,windows下会死。,Linux下就过了对不对?
    #include<bits/stdc++.h>
    const long long maxn=4e6+1;
    inline const void read(long long &a)
    {
    char c=getchar();a=0;
    while(c<'0'||c>'9')c=getchar();
    while(c>='0'&&c<='9')
    {
    a=(a<<1)+(a<<3)+c-'0';
    c=getchar();
    }
    }
    long long n,d=0,kk=1;
    long long point[maxn],next[maxn],father[maxn],to[maxn],ans[maxn],num[maxn],sum[maxn],deep[maxn];
    bool vis[maxn];
    inline const void add(long long a,long long b)
    {
    next[++d]=point[a];
    point[a]=d;
    to[d]=b;
    }
    inline const void dfs1(long long p)
    {
    vis[p]=true;
    long long side=point[p];
    num[p]=1;sum[p]+=deep[p];
    while(side)
    {
    if(!vis[to[side]])
    {
    father[to[side]]=p;
    deep[to[side]]=deep[p]+1;
    dfs1(to[side]);
    num[p]+=num[to[side]];sum[p]+=sum[to[side]];
    }
    side=next[side];
    }
    }
    inline const void dfs2(long long p)
    {

    if(p!=1)ans[p]=ans[father[p]]-2*num[p]+num[1];
    if(ans[p]>ans[kk])kk=p;
    if(ans[p]==ans[kk]&&p<kk)kk=p;
    vis[p]=true;
    long long side=point[p];
    while(side)
    {
    if(!vis[to[side]])dfs2(to[side]);
    side=next[side];
    }
    }
    int main()
    {
    memset(father,0,sizeof(father));
    memset(sum,0,sizeof(sum));
    memset(point,0,sizeof(point));
    memset(ans,0,sizeof(ans));
    read(n);
    long long u,v;
    for(long long i=1;i<=n-1;i++){read(u);read(v);add(u,v);add(v,u);}
    deep[1]=0;
    memset(vis,false,sizeof(vis));
    dfs1(1);
    memset(vis,false,sizeof(vis));ans[1]=sum[1];
    dfs2(1);
    std::cout<<kk;
    return 0;
    }
    这样就可以过windows了
    #include <iostream>
    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <algorithm>
    #include <queue>
    using namespace std;
    #define rep(i,l,r) for (int i=l; i<=r; i++)
    #define drep(i,r,l) for (int i=r; i>=l; i--)
    #define ll long long
    #define pb push_bake
    const int N = 1000008;
    int n, tot, head[N], h[N], fa[N], sum[N];
    bool vis[N];
    ll Down[N], Up[N];
    int read()
    {
    char c; int w = 1, num = 0;
    for (c = getchar(); !isdigit(c) && c != '+' && c != '-'; c = getchar());
    if (c == '-') c = getchar(), w = -1;
    if (c == '+') c = getchar();
    for (; isdigit(c); c = getchar()) num = num * 10 + c - '0';
    return num * w;
    }
    struct Node
    {
    int next, node;
    }e[2 * N];
    inline void add(int x, int y)
    {
    e[++tot].next = head[x], head[x] = tot, e[tot].node = y;
    e[++tot].next = head[y], head[y] = tot, e[tot].node = x;
    }
    void topsort()
    {
    int l = 1, r = 1;
    h[1] = 1; vis[1] = 1;
    while (l <= r)
    {
    int u = h[l++];
    for (int i = head[u], v; v = e[i].node, i; i = e[i].next)
    if (!vis[v])
    {
    vis[v] = 1;
    fa[v] = u;
    h[++r] = v;
    }
    }
    }
    int main()
    {
    n = read();
    rep(i, 1, n - 1)
    {
    int u, v;
    u = read(); v = read();
    add(u, v);
    }

    topsort();
    drep(k, n, 1)
    {
    int u = h[k];
    sum[u] = 1;
    for (int i = head[u], v; v = e[i].node, i; i = e[i].next)
    if (v != fa[u])
    {
    sum[u] += sum[v];
    Down[u] += Down[v] + sum[v];
    }
    }
    rep(k, 2, n)
    {
    int u = h[k];
    Up[u] += Up[fa[u]] + sum[1] - sum[fa[u]] + 1;
    Up[u] += Down[fa[u]] - Down[u] - sum[u] + sum[fa[u]] - sum[u] - 1;
    }
    ll Max = 0; int ans;
    rep(i, 1, n) if (Up[i] + Down[i] > Max) Max = Up[i] + Down[i], ans = i;
    printf("%d\n", ans);
    return 0;
    }

  • 1

信息

难度
9
分类
(无)
标签
(无)
递交数
7
已通过
1
通过率
14%
上传者