2016CCPC东北地区大学生程序设计竞赛 - Auxiliary Set(思维题)
2017-06-07 15:36
495 查看
Auxiliary Set
Time Limit: 9000/4500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1536 Accepted Submission(s): 463
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 (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.
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
题意:给出一个n个节点n-1条边的树。每次询问给出一个集合,集合内的点为不重要点,集合外的点为重要点。如果不重要点是两个重要点的lca则转变成重要点。输出重要点的个数。
题解:先预处理出每个点的父亲,儿子个数和深度.对于集合中的不要重要点,按深度降序排序。则这个点的儿子数量如果超过两个,就可以变成重要点。如果儿子数量为0,则该节点父亲节点不为根的情况下,将父亲节点的儿子数-1.因为这是”没用的“儿子。
代码:
Time Limit: 9000/4500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1536 Accepted Submission(s): 463
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 (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.
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
题意:给出一个n个节点n-1条边的树。每次询问给出一个集合,集合内的点为不重要点,集合外的点为重要点。如果不重要点是两个重要点的lca则转变成重要点。输出重要点的个数。
题解:先预处理出每个点的父亲,儿子个数和深度.对于集合中的不要重要点,按深度降序排序。则这个点的儿子数量如果超过两个,就可以变成重要点。如果儿子数量为0,则该节点父亲节点不为根的情况下,将父亲节点的儿子数-1.因为这是”没用的“儿子。
代码:
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #include<vector> using namespace std; const int N = 100000 + 100; int t,n,m,cnt; int head[N<<2]; int u,v,x; int c ; int read(){ int x=0;char ch = getchar(); while('0'>ch||ch>'9')ch=getchar(); while('0'<=ch&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();} return x; } struct node { int to,next; }edge[N<<2]; struct nod { int son,pre,dep; }a ,b ; void add(int u,int v) { edge[cnt].to=v; edge[cnt].next=head[u]; head[u]=cnt++; } void init() { cnt=0; memset(head,-1,sizeof(head)); } void dfs(int u,int pre,int dep) { int ans=0,v; for(int i=head[u];i!=-1;i=edge[i].next) { v=edge[i].to; if(v==pre)continue; ans++; dfs(v,u,dep+1); } a[u].son=ans; a[u].pre=pre; a[u].dep=dep; } bool cmp (int x,int y ) { return a[x].dep>a[y].dep; } int main() { t=read(); for(int k=1;k<=t;k++) { init(); n=read(),m=read(); for(int i=1;i<n;i++) { u=read(),v=read(); add(u,v); add(v,u); } dfs(1,-1,0); printf("Case #%d:\n",k); while(m--) { x=read(); for(int i=1;i<=x;i++) { c[i]=read(); } sort(c+1,c+1+x,cmp); for(int i=1;i<=x;i++) b[c[i]]=a[c[i]]; int ans=n-x; for(int i=1;i<=x;i++) { int id=c[i]; if(b[id].son>1) ans++; else if(b[id].son==0&&b[id].pre!=-1) b[b[id].pre].son--; } printf("%d\n",ans); } } }
相关文章推荐
- 【HDU5927 2016CCPC东北地区大学生程序设计竞赛 - 重现赛 F】【dfs序 + 线段树 or 树状数组 复杂度计算】Auxiliary Set 一个点如果是好点或是两个好点的LCA就是好
- (HDU 5927)Auxiliary Set 思维题 <2016CCPC东北地区大学生程序设计竞赛 - 重现赛 >
- HDU 5927 Auxiliary Set 【DFS+树】(2016CCPC东北地区大学生程序设计竞赛)
- HDU 5927 Auxiliary Set 【DFS+树】(2016CCPC东北地区大学生程序设计竞赛)
- hdu 5927 Auxiliary Set 2016CCPC东北地区大学生程序设计竞赛 - 重现赛 F题(树状dp)
- hdu 5924 Mr. Frog’s Problem(2016CCPC东北地区大学生程序设计竞赛 C题,数学思维)
- (HDU 5922)Minimum’s Revenge 思维水题 <2016CCPC东北地区大学生程序设计竞赛 - 重现赛 >
- (HDU 5924)Mr. Frog’s Problem 思维水题 <2016CCPC东北地区大学生程序设计竞赛 - 重现赛 >
- hdu5922 2016CCPC东北地区大学生程序设计竞赛 A题 Minimum’s Revenge(思维,水题)
- HDU 5924 Mr. Frog’s Problem 【模拟】 (2016CCPC东北地区大学生程序设计竞赛)
- 【HDU5925 2016CCPC东北地区大学生程序设计竞赛 - 重现赛 D】【离散化 BFS】Coconuts 大地图少数坏点输出每个联通块的大小
- HDU 5924 Mr. Frog’s Problem 【模拟】 (2016CCPC东北地区大学生程序设计竞赛)
- HDU 5926 Mr. Frog's Game 【模拟】 (2016CCPC东北地区大学生程序设计竞赛)
- HDU 5925 Coconuts 【离散化+BFS】 (2016CCPC东北地区大学生程序设计竞赛)
- 【HDU5926 2016CCPC东北地区大学生程序设计竞赛 - 重现赛 E】【水题】Mr. Frog’s Game 连连看
- HDU 5926 Mr. Frog's Game 【模拟】 (2016CCPC东北地区大学生程序设计竞赛)
- HDU 5925 Coconuts 【离散化+BFS】 (2016CCPC东北地区大学生程序设计竞赛)
- (HDU 5926)Mr. Frog’s Game 水题 <2016CCPC东北地区大学生程序设计竞赛 - 重现赛 >
- 2016CCPC东北地区大学生程序设计竞赛-Minimum’s Revenge
- 2016CCPC东北地区大学生程序设计竞赛 1003 HDU5924