Auxiliary Set
Time Limit: 9000/4500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1966 Accepted Submission(s): 574
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.
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 ui i 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.
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 ui i 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.
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 3HintFor 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
好题,绝对赞,让我学会了链式前向星和查询最近的祖先的方法。耐心看代码就好了,很仔细的把自己一步一步如何实现的都注解了,希望对读者有帮助。
题意:
根节点为1的树,定义一个节点为重要的节点至少满足两个条件中的一个条件
1:自己本身是重要的点
2:两个以上的子孙是重要的点
q组询问给出m个不重要的点
输出树上有多少重要的点
每次询问给出的是不重要点,如果重要点有两个以上重要点子孙,那么这个不重要点也变成集合里的一员,
每次输出集合里元素个数,将不重要点按照深度排序,然后遍历不重要点,如果不重点的孩子个数为0,那么
他的父亲孩子数减一,因为在他的父亲那里,他这个孩子没有孩子,并且自身还是不重要点,那么他父亲要这个孩子没有什么用,所以父亲节点孩子数减一;当这个不重点有两个及两个以上孩子,那么他也归属集合里;
因
为
儿子只对父亲影
响,
我们从最深的节点开始遍历
,当遍历当前节点时此时所有孩子都是重要点。即便有个孩子是不重要点,但是这个孩子肯定有重要点的子孙,不然他的孩子数会变为0,假如他变为0了,就会影响父亲节点,父亲节点减一;
#include<stdio.h>
#include<iostream>
#include <algorithm>
#include<string.h>
#include<vector>
#include<math.h>
#include<queue>
#include<deque>
#include<set>
#define MOD 1e9+7
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
int KGCD(int a,int b){if(a==0)return b;if(b==0)return a;if(~a&1){ if(b&1) return KGCD(a>>1,b);else return KGCD(a>>1,b>>1) <<1; } if(~b & 1) return KGCD(a, b>>1); if(a > b) return KGCD((a-b)>>1, b);return KGCD((b-a)>>1, a);}
int LCM(int a,int b){ return a/KGCD(a,b)*b; }
int dir[5][2]={0,1,0,-1,1,0,-1,0};
using namespace std;
struct edge{
int u;
int next;//记录同起点的下一条边
int v;//终点边
int w;//权值
}e[200009];
int head[200009]; //记录i起点可以到达的最大的边
int fa[200009];//记录每个点的父亲节点是谁 方便查找
int son[200009];//记录当前节点有几个儿子节点
int dep[200009]; //记录层数 sort从低到上
int ni[2000009];//存放不重要的点
int change[200009];//改变了之后在改变回来
int vis[200009];//bfs 标记使用
int tot;//开始点
void init()//初始化 head
{
tot=0;
memset(head,-1,sizeof(head));
}
void add(int u,int v)//起始点 终止点 链式前向星
{
e[tot].u=u;//记录起点
e[tot].v=v;//记录终止点
e[tot].next=head[u];//记录下上一个点
head[u]=tot;//更新u作为开头的最后一条边
tot++;
}
int cmp(int a,int b)//从底下往上查找
{
return dep[a]>dep[b];
}
void bfs(int root)
{
memset(vis,0,sizeof(vis));
vis[root]=1;
queue<int>q;
q.push(root);
while(!q.empty())
{
int u=q.front();
q.pop();
for(int i=head[u];i!=-1;i=e[i].next)
{
int v=e[i].v;//终止边
if(vis[v]==0)//当前边没有访问过
{
son[u]++;//u的儿子v
fa[v]=u; //v的爸爸u
dep[v]=dep[u]+1;//更深一层
q.push(v);
vis[v]=1;
}
}
}
}
int main()
{
int t,n,q,u,v;
scanf("%d",&t);
for(int count=1;count<=t;count++)
{
scanf("%d%d",&n,&q);
init();//每次初始化
for(int i=1;i<n;i++)
{
scanf("%d%d",&u,&v);
add(u,v);
add(v,u);//不一定谁连接谁 只是告诉两点之间有一条边
}
memset(fa,0,sizeof(fa));
memset(son,0,sizeof(son));
memset(dep,0,sizeof(dep));
bfs(1);
printf("Case #%d:\n",count);
while(q--)
{
scanf("%d",&u);
for(int i=0;i<u;i++)//存放不重要的点
scanf("%d",&ni[i]);
int ans=0;
sort(ni,ni+u,cmp);//从底层开始
for(int i=0;i<u;i++)
{
if(son[ni[i]]==0)
{//如果当前是一个叶子 提供不了帮助 让它父亲少一个儿子
son[fa[ni[i]]]--;
change[fa[ni[i]]]++;
}
if(son[ni[i]]>=2)//因为从底下到上边 如果他有儿子不符合已经去掉了
ans++;
}
printf("%d\n",n-u+ans);
for(int i=0;i<u;i++)//还原状态
{
if(change[fa[ni[i]]])
{
son[fa[ni[i]]]=son[fa[ni[i]]]+change[fa[ni[i]]];
change[fa[ni[i]]]=0;
}
}
}
}
return 0;
}