国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

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;}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 湖州市| 武山县| 陵川县| 张家川| 乐山市| 蕲春县| 女性| 乐东| 喜德县| 上栗县| 岳普湖县| 西乌珠穆沁旗| 喜德县| 中西区| 武鸣县| 西城区| 绥宁县| 咸阳市| 河西区| 阿瓦提县| 隆林| 翁牛特旗| 会昌县| 宁波市| 庄河市| 谢通门县| 连州市| 伊川县| 鹤壁市| 农安县| 嵩明县| 本溪| 安达市| 澄江县| 揭东县| 江永县| 宜章县| 潜山县| 伊春市| 台南市| 新密市|