(vijos 1892 noip 模擬 tree)

傳送門


Solution

以後不補


Code

輸入和vijos1892不太一樣

// by spli
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<iostream>
#define LL long long
using namespace std;

const int N=1010;
int n;
struct node{
    int to,nxt;
}e[N<<1];int head[N],cnt;
bool vis[N];
LL ff[N],gg[N];
LL f[2][N];
LL g[2][N];

void add(int f,int t){
    cnt++;
    e[cnt]=(node){t,head[f]};
    head[f]=cnt;
}

void dfs(int u,int fa){
    g[0][u]=1;
    for(int i=head[u];i!=-1;i=e[i].nxt){
        int v=e[i].to;
        if(v==fa) continue;
        dfs(v,u);
        f[0][u]+=ff[v];
        g[0][u]*=gg[v];
    }
    for(int i=head[u];i!=-1;i=e[i].nxt){
        int v=e[i].to;
        if(v==fa) continue;
        LL t=f[0][u]-ff[v]+f[0][v]+1;
        LL p=g[0][u]/gg[v]*g[0][v];
        if(f[1][u]==t) g[1][u]+=p;
        else if(t>f[1][u]){
            f[1][u]=t;g[1][u]=p;
        }
    }
    if(f[1][u]>f[0][u]) ff[u]=f[1][u],gg[u]=g[1][u];
    else if(f[0][u]>f[1][u]) ff[u]=f[0][u],gg[u]=g[0][u];
    else ff[u]=f[1][u],gg[u]=g[0][u]+g[1][u];
}

int main(){
    memset(head,-1,sizeof(head));
    scanf("%d",&n);
    int u,num,x;
    for(int i=1;i<=n;++i){
        scanf("%d%d",&u,&num);
        if(!num) vis[i]=1;
        for(int j=1;j<=num;++j){
            scanf("%d",&x);
            add(u,x);
        }
    }
    dfs(1,1);
    if(f[1][1]>f[0][1])
        cout<<f[1][1]<<endl<<g[1][1];
    else if(f[1][1]==f[0][1]) cout<<f[1][1]<<endl<<g[1][1]+g[0][1];
    else cout<<f[0][1]<<endl<<g[0][1];
    return 0;
}
發佈了80 篇原創文章 · 獲贊 10 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章