1
无根树转有根数,另外注意递归函数有返回值时,莫忘判断,否则TLE
#include <iostream>
#include <math.h>
#include <vector>
#include <set>
using namespace std;
int kk;
vector<int> son[100010];
set <int> sset;
int p[100010];
int a[100010];
int visted[100010];
int n,q;
int total=0;
void Add(int u,int v){
son[u].push_back(v);
son[v].push_back(u);
}
void Build(int x,int fa){
for(int i=0;i<son[x].size();i++){
if(son[x][i]!=fa){
Build(son[x][i],p[son[x][i]]=x);
}
}
}
int Dfs(int u){
for(int i=0;i<son[u].size();i++){
if(son[u][i]==p[u]) continue;
if(sset.find(son[u][i])==sset.end()){
return 1;
}
else{
if(Dfs(son[u][i]))///递归函数若有返回值,莫忘判断
return 1;
}
}
return 0;
}
int Judge(int x){
int leiji=0;
for(int i=0;i<son[x].size();i++){
if(son[x][i]==p[x]) continue;
//cout<<x<<" son: "<<son[x][i]<<endl;
if(sset.find(son[x][i])==sset.end()){
leiji++;
}
else{
if(Dfs(son[x][i])){
leiji++;
}
}
if(leiji>=2){
//cout<<"Note: "<<x<<endl;
return 1;
}
}
return 0;
}
int test(int x){
cout<<"x: "<<x<<" size(): "<<son[x].size()<<endl;
for(int i=0;i<son[x].size();i++){
if(son[x][i]==p[x]) continue;
cout<<son[x][i]<<" ";
}
cout<<endl;
}
int main()
{
cin>>kk;
for(int k=1;k<=kk;k++){
scanf("%d%d",&n,&q);
for(int i=1;i<=n;i++){
son[i].clear();
p[i]=0;
}
int u,v;
for(int i=1;i<=n-1;i++){
scanf("%d%d",&u,&v);
Add(u,v);//无向,但注意函数内已有两次,注意统一。
}
p[1]=-1;
Build(1,-1);
int m;
printf("Case #%d:\n",k);
for(int i=1;i<=q;i++){
scanf("%d",&m);
sset.clear();
total=n-m;
int number;
for(int j=1;j<=m;j++){
scanf("%d",&number);
sset.insert(number);
}
for(set<int>::iterator it=sset.begin();it!=sset.end();it++){
if(Judge(*it)==1){
total++;
}
}
cout<<total<<endl;
}
}
}
2
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 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.
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.
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