HDU5927

#include <bits/stdc++.h>

using namespace std;

const int maxn = 1e5+10;
vector <int> V[maxn];
struct Node{
    int node;
    int father;
    int deep;
    Node(){
        node = father = deep = 0;
    }
}N[maxn],Un[maxn];
int child[maxn];
bool vis[maxn];
int change[maxn];

void dfs(int n){
    int len = V[n].size();
    vis[n] = true;
    for(int i=0;i<len;i++){
        if(!vis[V[n][i]]){
            ++child[n];
            N[V[n][i]].father = n;
            N[V[n][i]].deep = N[n].deep+1;
            dfs(V[n][i]);
        }
    }
}

int cmp(const Node &A,const Node &B){
    return A.deep > B.deep;
}

int main()
{
    ios::sync_with_stdio(false);
    int T,cnt=0;
    cin>>T;
    while(T--){
        for(int i=0;i<maxn;i++) V[i].clear();
        memset(child,0,sizeof(child));
        memset(vis,false,sizeof(vis));
        int n,q;
        cin>>n>>q;
        for(int i=1;i<n;i++){
             int u,v;
             cin>>u>>v;
             V[u].push_back(v);
             V[v].push_back(u);
        }


        N[1].deep=1;
        N[1].father=-1;    //表示是根节点
        dfs(1);

        int Count;
        cout<<"Case #"<<++cnt<<":\n";

        for(int i=0;i<q;i++){
            Count = 0;
            int m;
            cin>>m;
            for(int j=0;j<m;j++){
                int node;
                cin>>node;
                Un[j].deep = N[node].deep;
                Un[j].father = N[node].father;
                Un[j].node = node;
            }
            sort(Un,Un+m,cmp);
            int ans = n-m;
            for(int j=0;j<m;j++){
                if( child[ Un[j].node ] >= 2 ) ++ans;
                else if( child[ Un[j].node ] == 0 ) {
                    change[Count++] = Un[j].father;
                    --child[ Un[j].father ];
                }
            }
            for(int j=0;j<Count;j++){
                ++child[ change[j] ];
            }
            cout<<ans<<endl;
        }

    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值