HDU 5927 Auxiliary Set(树上搞事)
来源:互联网 发布:加拿大数据科学硕士 编辑:程序博客网 时间:2024/05/29 18:34
思路:考虑对于每一个询问的点按照深度排序,如果该不重要的点的儿子有两个,那么就证明可以贡献一个答案,如果该不重要的点没有儿子了,那么对于该点的父节点的儿子数减一
#include<bits/stdc++.h>using namespace std;const int maxn = 100000+7;vector<int>e[maxn];int dep[maxn],fa[maxn],son[maxn];int a[maxn];int b[maxn];void dfs(int u,int f,int d){dep[u]=d;fa[u]=f;int len = e[u].size();for(int i= 0;i<len;i++){int v = e[u][i];if(v==f)continue;dfs(v,u,d+1); son[u]++;}}bool cmp(int a,int b){return dep[a]>dep[b];}int main(){int T,cas=1;scanf("%d",&T);while(T--){printf("Case #%d:\n",cas++);int n,q;scanf("%d%d",&n,&q);for(int i =0;i<=n;i++)e[i].clear();memset(son,0,sizeof(son));memset(a,0,sizeof(a));memset(b,0,sizeof(b));for(int i = 1;i<=n-1;i++){int u,v;scanf("%d%d",&u,&v);e[u].push_back(v);e[v].push_back(u);}dfs(1,-1,0);while(q--){int num;scanf("%d",&num);int ans = n-num;for(int i = 0;i<num;i++)scanf("%d",&a[i]);sort(a,a+num,cmp); for(int i = 0;i<num;i++)b[a[i]]=son[a[i]]; for(int i = 0;i<num;i++){if(b[a[i]]>=2)ans++;else if (b[a[i]]==0)b[fa[a[i]]]--;}printf("%d\n",ans);}}}
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.
An auxiliary set is a set containing vertices satisfying at least one of the two conditions:
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 ( ), which indicates the number of test cases.
For each test case, the first line contains two integers n ( ), q ( ).
In the following n -1 lines, the i-th line contains two integers indicating there is an edge between i and in the tree.
In the next q lines, the i-th line first comes with an integer 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 .
It is also guaranteed that the number of test cases in which or is no more than 10.
For each test case, the first line contains two integers n (
In the following n -1 lines, the i-th line contains two integers
In the next q lines, the i-th line first comes with an integer
It is guaranteed that
It is also guaranteed that the number of test cases in which
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.
Then q lines follow, i-th line contains an integer indicating the size of the auxiliary set for each query.
Sample Input
16 36 42 55 41 55 33 1 2 31 53 3 1 4
Sample Output
Case #1:363
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
0 0
- HDU 5927 Auxiliary Set(树上搞事)
- [HDU 5927] Auxiliary Set (DFS+树上LCA)
- hdu 5927 Auxiliary Set
- hdu 5927 Auxiliary Set
- HDU 5927 Auxiliary Set
- HDU 5927Auxiliary Set
- hdu 5927 Auxiliary Set (分析*)
- HDU 5927 Auxiliary Set(DFS)
- hdu 5927 Auxiliary Set(思路)
- HDU 5927 Auxiliary Set (树形)
- hdu 5927 Auxiliary Set dfs
- F - Auxiliary Set HDU - 5927
- hdu 5927 Auxiliary Set (dfs)
- HDU 5927 Auxiliary Set (树形DP+思维)
- HDU 5927 Auxiliary Set(dfs+逆向思维)
- HDU 5927 Auxiliary Set dfs(思维)
- HDU 5927 Auxiliary Set By Assassin
- HDU - 5927 Auxiliary Set (树形DP)
- 客户端 用不用 bind 的区别
- HTML头
- Qt: 网络编程之UDP(理论+实例)
- 第三天学习数据结构,顺序栈
- 程序员自我修养
- HDU 5927 Auxiliary Set(树上搞事)
- Git客户端图文详解如何安装配置GitHub操作流程攻略
- Android之Bundle传递数据详解与实例及Bundle与SharedPreferences的区别
- 最简单最高效的Git教程---SourceTree
- HDU 2874 Connections between cities
- Git版本控制软件结合GitHub从入门到精通常用命令学习手册
- cookieandlocalStorage
- 396. Rotate Function
- Linux -- 进程管理之僵尸进程