HDU 5927 Auxiliary Set 【DFS+树】(2016CCPC东北地区大学生程序设计竞赛)
Auxiliary Set
Time Limit: 9000/4500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 873 Accepted Submission(s): 271Problem DescriptionGiven 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≤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.
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.
Sample Input1
6 3
6 4
2 5
5 4
1 5
5 3
3 1 2 3
1 5
3 3 1 4Sample OutputCase #1:
3
6
3Hint
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 nodesSourceRecommend
题目链接:
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这个子树中有特殊点)。
- //
- //by coolxxx
- //#include<bits/stdc++.h>
- #include<iostream>
- #include<algorithm>
- #include<string>
- #include<iomanip>
- #include<map>
- #include<stack>
- #include<queue>
- #include<set>
- #include<bitset>
- #include<memory.h>
- #include<time.h>
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- //#include<stdbool.h>
- #include<math.h>
- #pragma comment(linker,"/STACK:1024000000,1024000000")
- #define min(a,b) ((a)<(b)?(a):(b))
- #define max(a,b) ((a)>(b)?(a):(b))
- #define abs(a) ((a)>0?(a):(-(a)))
- #define lowbit(a) (a&(-a))
- #define sqr(a) ((a)*(a))
- #define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
- #define mem(a,b) memset(a,b,sizeof(a))
- #define eps (1e-10)
- #define J 10000
- #define mod 1000000007
- #define MAX 0x7f7f7f7f
- #define PI 3.14159265358979323
- #define N 100004
- using namespace std;
- typedef long long LL;
- double anss;
- LL aans;
- int cas,cass;
- int n,m,lll,ans;
- int s[N],b[N],c[N],fa[N],last[N],d[N];
- struct xxx
- {
- int to,next;
- }a[N+N];
- void add(int x,int y)
- {
- a[++lll].next=last[x];
- last[x]=lll;
- a[lll].to=y;
- }
- bool cmp(int a,int b)
- {
- return d[a]>d[b];
- }
- void dfs(int now,int ffa)
- {
- int i,to;
- s[now]=;c[now]=;d[now]=d[ffa]+;fa[now]=ffa;
- for(i=last[now];i;i=a[i].next)
- {
- to=a[i].to;
- if(to==ffa)continue;
- dfs(to,now);
- s[now]++;
- }
- }
- void work()
- {
- int i,x,mm;
- ans=n;
- scanf("%d",&mm);
- for(i=;i<=mm;i++)scanf("%d",&b[i]);
- sort(b+,b++mm,cmp);
- for(i=;i<=mm;i++)
- {
- x=b[i];
- c[x]++;
- if(s[x]==c[x])c[fa[x]]++;
- if(s[x]-c[x]<)ans--;
- }
- for(i=;i<=mm;i++)c[b[i]]=c[fa[b[i]]]=;
- printf("%d\n",ans);
- }
- int main()
- {
- #ifndef ONLINE_JUDGEW
- // freopen("1.txt","r",stdin);
- // freopen("2.txt","w",stdout);
- #endif
- int i,j,k;
- int x,y,z;
- // init();
- // for(scanf("%d",&cass);cass;cass--)
- for(scanf("%d",&cas),cass=;cass<=cas;cass++)
- // while(~scanf("%s",s))
- // while(~scanf("%d%d",&n,&m))
- {
- printf("Case #%d:\n",cass);
- lll=;mem(last,);
- scanf("%d%d",&n,&m);
- for(i=;i<n;i++)
- {
- scanf("%d%d",&x,&y);
- add(x,y),add(y,x);
- }
- dfs(,);
- for(i=;i<=m;i++)
- work();
- }
- return ;
- }
- /*
- //
- //
- */
HDU 5927 Auxiliary Set 【DFS+树】(2016CCPC东北地区大学生程序设计竞赛)的更多相关文章
- HDU 5925 Coconuts 【离散化+BFS】 (2016CCPC东北地区大学生程序设计竞赛)
Coconuts Time Limit: 9000/4500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Su ...
- HDU 5929 Basic Data Structure 【模拟】 (2016CCPC东北地区大学生程序设计竞赛)
Basic Data Structure Time Limit: 7000/3500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Oth ...
- HDU 5926 Mr. Frog's Game 【模拟】 (2016CCPC东北地区大学生程序设计竞赛)
Mr. Frog's Game Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)T ...
- HDU 5924 Mr. Frog’s Problem 【模拟】 (2016CCPC东北地区大学生程序设计竞赛)
Mr. Frog's Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ...
- HDU 5922 Minimum’s Revenge 【模拟】 (2016CCPC东北地区大学生程序设计竞赛)
Minimum's Revenge Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
- 2016CCPC东北地区大学生程序设计竞赛1008/HDU 5929 模拟
Basic Data Structure Time Limit: 7000/3500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Oth ...
- 2016CCPC东北地区大学生程序设计竞赛 (2018年8月22日组队训练赛)
题目链接:http://acm.hdu.edu.cn/search.php?field=problem&key=2016CCPC%B6%AB%B1%B1%B5%D8%C7%F8%B4%F3%D ...
- 2016CCPC东北地区大学生程序设计竞赛 1008 HDU5929
链接http://acm.hdu.edu.cn/showproblem.php?pid=5929 题意:给你一种数据结构以及操作,和一种位运算,最后询问:从'栈'顶到低的运算顺序结果是多少 解法:根据 ...
- 2016CCPC东北地区大学生程序设计竞赛 1005 HDU5926
链接http://acm.hdu.edu.cn/showproblem.php?pid=5926 题意:给我们一个矩阵,问你根据连连看的玩法可以消去其中的元素 解法:连连看怎么玩,就怎么写,别忘记边界 ...
随机推荐
- 学习java随笔第八篇:封装、继承、多态
java和c#一样都是面向对象的语言. 面向对象的语言有三大特征:封装.继承.多态 封装 封装:隐藏对象的属性和实现细节,仅对外公开接口,控制在程序中属性的读和修改的访问级别. class Perso ...
- [条形码] BarCodeToHTML条码生成类 (转载)
点击下载 BarCodeToHTML.zip 过多的我就不多说了大家直接看代码吧,这是一个帮助大家生成条码的类,大小大家可以自由的设定 /// <summary> /// 类说明:条码生成 ...
- SQL server 跨库插入数据
1.INSERT INTO SELECT语句 语句形式为: Insert into Table2(field1,field2,...) select value1,value2,... from Ta ...
- 组策略彻底解决windows 2003 终端数
win2003的话可以从组策略修改: 组策略级别要高于终端服务配置,当启用组策略后终端服务配置中的相应选项会变成灰色不可修改 运行-gpedit.msc-计算机配置-管理模板-Windows ...
- Angularjs总结(一)表单验证
常用的表单验证1.必须字段html5特性 增加required <input type="text" required /> 2.最小长度和最大长度 <input ...
- SQL数据库的备份和恢复
SQL数据库的备份和恢复 一.SQL数据库的备份: 1.依次打开 开始菜单 → 程序 → Microsoft SQL Server 2008 → SQL Server Management Studi ...
- map函数(转)
STL中map用法详解 说明:如果你具备一定的C++ template知识,即使你没有接触过STL,这个文章你也应该可能较轻易的看懂.本人水平有限,不当之处,望大家辅正. 一.Map概述 Map是ST ...
- 简单的背包问题(入门)HDU2602 HDU2546 HDU1864
动态规划,我一直都不熟悉,因为体量不够,所以今天开始努力地学习学习. 当然背包从01开始,先选择了一个简单的经典的背包HDU2602. Many years ago , in Teddy's home ...
- 『重构--改善既有代码的设计』读书笔记----Change Value to Reference
有时候你会认为某个对象应该是去全局唯一的,这就是引用(Reference)的概念.它代表当你在某个地点对他进行修改之后,那么所有共享他的对象都应该在再次访问他的时候得到相应的修改.而不会像值对象(Va ...
- 桂电在线-php-提取菜单到配置文件
新建存储菜单的配置文件 menus.php,并配置菜单 <?php if ( ! defined('BASEPATH')) exit('No direct script access allow ...