码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 5927 Auxiliary Set 【DFS+树】(2016CCPC东北地区大学生程序设计竞赛)

时间:2016-10-19 20:14:55      阅读:386      评论:0      收藏:0      [点我收藏+]

标签:

Auxiliary Set

Time Limit: 9000/4500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 873    Accepted Submission(s): 271


Problem Description
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 (T1000), which indicates the number of test cases.

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

In the following n -1 lines, the i-th line contains two integers ui,vi(1ui,vin) 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(1mi100000) 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=1mi100000.

It is also guaranteed that the number of test cases in which n1000  or qi=1mi1000 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
Hint
技术分享 For the query {1,2, 3}: ?node 4, 5, 6 are important nodes For the query {5}: ?node 1,2, 3, 4, 6 are important nodes ?node 5 is the lea of node 4 and node 3 For the query {3, 1,4}: ? node 2, 5, 6 are important nodes
 

 

Source
 

 

Recommend
wange2014   |   We have carefully selected several similar problems for you:  5932 5931 5930 5929 5928 
 

 

Statistic | Submit | Discuss | Note

 

 

题目链接:

  http://acm.hdu.edu.cn/showproblem.php?pid=5927

题目大意:

  T组数据(T<=1000),对于每组数据,N(N<=100000)个点的一棵树,根节点为1,一个点在Set里需要满足下列情况之一:

    1.这个点是特殊点  2.这个点是两个特殊点的最近公共祖先(LCA)。

  M(M<=100000)个询问,每次询问给一个Q(Q<=N),表示N个点里面有Q个点不是特殊点,接下来是Q个点。求Set里有几个数。

  ∑Q<=100000,超过1000的N或∑Q的数据组数<=10

题目思路:

  【DFS】

  首先由于每组数据的树是一样的,先预处理出树的每个节点的深度d[x],父亲fa[x],度s[x](儿子个数,不算子孙)。

  接下来对于每一个Q,将Q个数按照深度从深到浅排序,从最深的开始做,c[x]表示c的儿子子树全为非特殊点的个数。

  如果当前节点的所有子孙都不是特殊点(c[x]=s[x]),则当前结点也不是特殊点,不需要加入Set,并且将x的父亲y=fa[x]的c[y]++

  如果当前节点只有一颗子树含有特殊点,其他子树都不含有特殊点(s[x]-c[x]<=1),则这个点不需要加入Set,但是y=fa[x]的c[y]不加(表示x这个子树中有特殊点)。

  

技术分享
  1 //
  2 //by coolxxx
  3 //#include<bits/stdc++.h>
  4 #include<iostream>
  5 #include<algorithm>
  6 #include<string>
  7 #include<iomanip>
  8 #include<map>
  9 #include<stack>
 10 #include<queue>
 11 #include<set>
 12 #include<bitset>
 13 #include<memory.h>
 14 #include<time.h>
 15 #include<stdio.h>
 16 #include<stdlib.h>
 17 #include<string.h>
 18 //#include<stdbool.h>
 19 #include<math.h>
 20 #pragma comment(linker,"/STACK:1024000000,1024000000")
 21 #define min(a,b) ((a)<(b)?(a):(b))
 22 #define max(a,b) ((a)>(b)?(a):(b))
 23 #define abs(a) ((a)>0?(a):(-(a)))
 24 #define lowbit(a) (a&(-a))
 25 #define sqr(a) ((a)*(a))
 26 #define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
 27 #define mem(a,b) memset(a,b,sizeof(a))
 28 #define eps (1e-10)
 29 #define J 10000
 30 #define mod 1000000007
 31 #define MAX 0x7f7f7f7f
 32 #define PI 3.14159265358979323
 33 #define N 100004
 34 using namespace std;
 35 typedef long long LL;
 36 double anss;
 37 LL aans;
 38 int cas,cass;
 39 int n,m,lll,ans;
 40 int s[N],b[N],c[N],fa[N],last[N],d[N];
 41 struct xxx
 42 {
 43     int to,next;
 44 }a[N+N];
 45 void add(int x,int y)
 46 {
 47     a[++lll].next=last[x];
 48     last[x]=lll;
 49     a[lll].to=y;
 50 }
 51 bool cmp(int a,int b)
 52 {
 53     return d[a]>d[b];
 54 }
 55 void dfs(int now,int ffa)
 56 {
 57     int i,to;
 58     s[now]=1;c[now]=0;d[now]=d[ffa]+1;fa[now]=ffa;
 59     for(i=last[now];i;i=a[i].next)
 60     {
 61         to=a[i].to;
 62         if(to==ffa)continue;
 63         dfs(to,now);
 64         s[now]++;
 65     }
 66 }
 67 void work()
 68 {
 69     int i,x,mm;
 70     ans=n;
 71     scanf("%d",&mm);
 72     for(i=1;i<=mm;i++)scanf("%d",&b[i]);
 73     sort(b+1,b+1+mm,cmp);
 74     for(i=1;i<=mm;i++)
 75     {
 76         x=b[i];
 77         c[x]++;
 78         if(s[x]==c[x])c[fa[x]]++;
 79         if(s[x]-c[x]<2)ans--;
 80     }
 81     for(i=1;i<=mm;i++)c[b[i]]=c[fa[b[i]]]=0;
 82     printf("%d\n",ans);
 83 }
 84 int main()
 85 {
 86     #ifndef ONLINE_JUDGEW
 87 //    freopen("1.txt","r",stdin);
 88 //    freopen("2.txt","w",stdout);
 89     #endif
 90     int i,j,k;
 91     int x,y,z;
 92 //    init();
 93 //    for(scanf("%d",&cass);cass;cass--)
 94     for(scanf("%d",&cas),cass=1;cass<=cas;cass++)
 95 //    while(~scanf("%s",s))
 96 //    while(~scanf("%d%d",&n,&m))
 97     {
 98         printf("Case #%d:\n",cass);
 99         lll=0;mem(last,0);
100         scanf("%d%d",&n,&m);
101         for(i=1;i<n;i++)
102         {
103             scanf("%d%d",&x,&y);
104             add(x,y),add(y,x);
105         }
106         dfs(1,0);
107         for(i=1;i<=m;i++)
108             work();
109     }
110     return 0;
111 }
112 /*
113 //
114 
115 //
116 */
View Code

 

HDU 5927 Auxiliary Set 【DFS+树】(2016CCPC东北地区大学生程序设计竞赛)

标签:

原文地址:http://www.cnblogs.com/Coolxxx/p/5978265.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!