HDU 5927 Auxiliary Set 【DFS+树】(2016CCPC东北地区大学生程序设计竞赛)
2016-10-19 18:05
288 查看
Auxiliary Set
Time Limit: 9000/4500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 873 Accepted Submission(s): 271
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
1
6 3
6 4
2 5
5 4
1 5
5 3
3 1 2 3
1 5
3 3 1 4
[align=left]Sample Output[/align]
Case #1:
3
6
3
Hint
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 ,b ,c ,fa ,last ,d ; 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东北地区大学生程序设计竞赛)
- hdu 5927 Auxiliary Set 2016CCPC东北地区大学生程序设计竞赛 - 重现赛 F题(树状dp)
- 【HDU5927 2016CCPC东北地区大学生程序设计竞赛 - 重现赛 F】【dfs序 + 线段树 or 树状数组 复杂度计算】Auxiliary Set 一个点如果是好点或是两个好点的LCA就是好
- (HDU 5927)Auxiliary Set 思维题 <2016CCPC东北地区大学生程序设计竞赛 - 重现赛 >
- HDU Basic Data Structure 2016CCPC东北地区大学生程序设计竞赛 - 重现赛
- (HDU 5924)Mr. Frog’s Problem 思维水题 <2016CCPC东北地区大学生程序设计竞赛 - 重现赛 >
- HDU 5922 Minimum’s Revenge 【模拟】 (2016CCPC东北地区大学生程序设计竞赛)
- HDU 5924 Mr. Frog’s Problem 【模拟】 (2016CCPC东北地区大学生程序设计竞赛)
- HDU 5929 Basic Data Structure 【模拟】 (2016CCPC东北地区大学生程序设计竞赛)
- HDU 5925 Coconuts 【离散化+BFS】 (2016CCPC东北地区大学生程序设计竞赛)
- HDU 5926 Mr. Frog's Game 【模拟】 (2016CCPC东北地区大学生程序设计竞赛)
- hdu 5924 Mr. Frog’s Problem(2016CCPC东北地区大学生程序设计竞赛 C题,数学思维)
- hdu 5929 Basic Data Structure 2016CCPC东北地区大学生程序设计竞赛
- (HDU 5922)Minimum’s Revenge 思维水题 <2016CCPC东北地区大学生程序设计竞赛 - 重现赛 >
- HDU 5925 Coconuts 【离散化+BFS】 (2016CCPC东北地区大学生程序设计竞赛)
- HDU 5922 Minimum’s Revenge 【模拟】(2016CCPC东北地区大学生程序设计竞赛)
- HDU 5924 Mr. Frog’s Problem 【模拟】 (2016CCPC东北地区大学生程序设计竞赛)
- hdu 5926 Mr. Frog’s Game 2016CCPC东北地区大学生程序设计竞赛E题(模拟)
- HDU 5929 Basic Data Structure 【模拟】 (2016CCPC东北地区大学生程序设计竞赛)
- HDU Mr. Frog’s Problem 2016CCPC东北地区大学生程序设计竞赛 - 重现赛