如果发现广告等破坏行为,请尽量将条目恢复到较早的版本而不是把相应内容直接删除,谢谢合作。

URAL/1362

来自"NOCOW"

跳转到: 导航, 搜索

简单树型DP。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using std::sort;
const int maxn=100010,maxm=(maxn<<1);
int n,x,tot=0,t[maxn]={0},r[maxn];
int h[maxn]={0},node[maxm],next[maxm];
void addedge(int a,int b)
{
    node[++tot]=b;
    next[tot]=h[a];
    h[a]=tot;
}
int max(int a,int b)  {  return a>b?a:b;  }
void dfs(int x,int fa)
{
    int tot=0;
    for (int i=h[x];i;i=next[i])
        if (node[i] != fa)
            dfs(node[i],x);
    for (int i=h[x];i;i=next[i])
        if (node[i] != fa)
            r[++tot]=t[node[i]];
    sort(&r[1],&r[tot+1]);
    for (int i=1;i<=tot;++i)
        t[x]=max(t[x],r[i]+tot-i+1);
}
int main()
{
    scanf("%d",&n);
    for (int i=1;i<=n;++i)
    {
        scanf("%d",&x);
        while (x)
        {
            addedge(x,i);
            addedge(i,x);
            scanf("%d",&x);
        }
    }
    scanf("%d",&x);
    dfs(x,0);
    printf("%d\n",t[x]);
    return 0;
}
//by zzy
个人工具