首页 > 学院 > 开发设计 > 正文

F - Auxiliary Set HDU - 5927

2019-11-08 19:41:27
字体:
来源:转载
供稿:网友

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. InputThe first line contains only one integer T (T≤1000T≤1000), which indicates the number of test cases. For each test case, the first line contains two integers n (1≤n≤1000001≤n≤100000), q (0≤q≤1000000≤q≤100000). In the following n -1 lines, the i-th line contains two integers ui,vi(1≤ui,vi≤n)ui,vi(1≤ui,vi≤n)indicating there is an edge between uiuii and vivi in the tree. In the next q lines, the i-th line first comes with an integer mi(1≤mi≤100000)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∑i=1qmi≤100000. It is also guaranteed that the number of test cases in which n≥1000n≥1000  or ∑qi=1mi≥1000∑i=1qmi≥1000 is no more than 10. OutputFor 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. 

重点就是把不是重要的点按照深度排序,然后按照孩子的个数来判断这个点是不是能够加到集合里。

#include <bits/stdc++.h>using namespace std;const int MAXN=1e5+7;int fa[MAXN],son[MAXN],temp[MAXN],deep[MAXN];bool vis[MAXN];int uimp[MAXN];vector<int>head[MAXN];int n,m,k;void dfs(int u,int f,int d){    vis[u]=1;    fa[u]=f;    deep[u]=d;    for(int i=0,l=head[u].size();i<l;++i)    {        int v=head[u][i];        if(!vis[v])        {            son[u]++;            dfs(v,u,d+1);        }    }}bool cmp(int a,int b){    return deep[a]>deep[b];}int main(){    int t;    int i;    scanf("%d",&t);    for(int tt=1;tt<=t;++tt)    {        scanf("%d%d",&n,&m);        for(i=1;i<=n;++i)        {            head[i].clear();            son[i]=vis[i]=0;        }        int u,v;        for(i=0;i<n-1;++i)        {            scanf("%d%d",&u,&v);            head[u].push_back(v);            head[v].push_back(u);        }        dfs(1,0,0);        PRintf("Case #%d:/n",tt);        while(m--)        {            scanf("%d",&k);            int ans=n-k;            for(i=0;i<k;++i)            {                scanf("%d",&uimp[i]);                temp[uimp[i]]=son[uimp[i]];            }            sort(uimp,uimp+k,cmp);            for(i=0;i<k;++i)            {                if(temp[uimp[i]]>1)ans++;                else if(!temp[uimp[i]])temp[fa[uimp[i]]]--;            }            printf("%d/n",ans);        }    }    return 0;}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表