Auxiliary Set HDU - 5927(思维题)

Given a rooted tree with n vertices, some of the vertices are important.

An auxiliary set is a set containing vertices satisfying at least one of the two conditions:

∙It is an important vertex
∙It is the least common ancestor of two different important vertices.

You are given a tree with n vertices (1 is the root) and q queries.

Each query is a set of nodes which indicates the unimportant vertices in the tree. Answer the size (i.e. number of vertices) of the auxiliary set for each query.
Input
The first line contains only one integer T (T≤1000), which indicates the number of test cases.

For each test case, the first line contains two integers n (1≤n≤100000), q (0≤q≤100000).

In the following n -1 lines, the i-th line contains two integers ui,vi(1≤ui,vi≤n) indicating there is an edge between uii and vi in the tree.

In the next q lines, the i-th line first comes with an integer mi(1≤mi≤100000) indicating the number of vertices in the query set.Then comes with mi different integers, indicating the nodes in the query set.

It is guaranteed that ∑qi=1mi≤100000.

It is also guaranteed that the number of test cases in which n≥1000 or ∑qi=1mi≥1000 is no more than 10.
Output
For each test case, first output one line “Case #x:”, where x is the case number (starting from 1).

Then q lines follow, i-th line contains an integer indicating the size of the auxiliary set for each query.
Sample Input
1
6 3
6 4
2 5
5 4
1 5
5 3
3 1 2 3
1 5
3 3 1 4
Sample Output
Case #1:
3
6
3
这题是一个思维题,被唬住了,也没怎么仔细想,现在想来也的确是这么一回事。当时过的人多,的确应该好好思考,也不是不可能想出来。

代码;

#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<vector>
#define maxx 100005
using namespace std;
int fa[maxx],d[maxx];
vector<int> tree[maxx];
int n,q;
int a[maxx];
int son[maxx];
void dfs(int u,int last,int deep)
{
    d[u]=deep;
    fa[u]=last;
    for(int i=0;i<tree[u].size();i++)
    {
        int v=tree[u][i];
        if(v==last)
            continue;
        son[u]++;
        dfs(v,u,deep+1);
    }
}
void init(int x)
{
    for(int i=1;i<=x;i++)
        tree[i].clear();
    memset(son,0,sizeof(son));
}
bool cmp(int x,int y)
{
    return d[x]>d[y];
}
int _size[maxx];
int main()
{
    int t;
    cin>>t;
    int cal=1;
    while(t--)
    {
        scanf("%d%d",&n,&q);
        int x,y;
        init(n);
        for(int i=1;i<n;i++)
        {
            scanf("%d%d",&x,&y);
            tree[x].push_back(y);
            tree[y].push_back(x);
        }
        dfs(1,0,1);
        printf("Case #%d:\n",cal++);
        while(q--)
        {
            int num;
            scanf("%d",&num);
            for(int i=0;i<num;i++)
                scanf("%d",a+i);
            for(int i=0;i<num;i++)
                _size[a[i]]=son[a[i]];
            sort(a,a+num,cmp);
            int ans=n-num;
            for(int i=0;i<num;i++)
            {
                if(_size[a[i]]>=2)
                    ans++;
                else
                    if(!_size[a[i]])
                        _size[fa[a[i]]]--;
            }
            cout<<ans<<endl;
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/coldfresh/article/details/80149776