hdu 5927 Auxiliary Set 贪心

时间:2022-09-25 13:02:43

Auxiliary Set

Time Limit: 9000/4500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

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
Hint

hdu 5927 Auxiliary Set 贪心

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

 
Source
思路:记录儿子个数,子树的全删掉就是删掉一儿子;
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-14
const int N=2e5+,M=4e6+,inf=1e9+,mod=1e9+;
const ll INF=1e18+;
vector<int>v[N];
int si[N],fa[N],deep[N],a[N],change[N];
int n,m,q;
void init()
{
for(int i=;i<=n;i++)
{
change[i]=;
fa[i]=;
si[i]=;
deep[i]=;
v[i].clear();
}
}
void dfs(int u,int pre,int dep)
{
deep[u]=dep;
for(int i=;i<v[u].size();i++)
{
if(v[u][i]==pre)continue;
si[u]++;dfs(v[u][i],u,dep+);
fa[v[u][i]]=u;
}
}
int cmp(int a,int b)
{
return deep[a]>deep[b];
}
int main()
{
int T,cas=;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&q);
init();
for(int i=;i<n;i++)
{
int u,w;
scanf("%d%d",&u,&w);
v[u].push_back(w);
v[w].push_back(u);
}
dfs(,,);
printf("Case #%d:\n",cas++);
while(q--)
{
int ans=;
scanf("%d",&m);
for(int i=;i<=m;i++)
scanf("%d",&a[i]);
sort(a+,a++m,cmp);
for(int i=;i<=m;i++)
{
int FA=fa[a[i]];
if(si[a[i]]==)
{
si[FA]--;
change[FA]++;
}
if(si[a[i]]>=)
{
ans++;
}
}
printf("%d\n",n-m+ans);
for(int i=;i<=m;i++)
{
int FA=fa[a[i]];
if(change[FA])
{
si[FA]+=change[FA];
change[FA]=;
}
}
}
}
return ;
}

hdu 5927 Auxiliary Set 贪心的更多相关文章

  1. HDU 5927 Auxiliary Set 【DFS&plus;树】&lpar;2016CCPC东北地区大学生程序设计竞赛&rpar;

    Auxiliary Set Time Limit: 9000/4500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tot ...

  2. HDU 5927 Auxiliary Set &lpar;dfs&rpar;

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5927 题意: 给你一棵树,其中有一些'不重要'的点,要是这些'不重要'的点的子树中有两个重要的点的LC ...

  3. hdu 5927 Auxiliary Set

    传送门 分析:感觉这道题有点意思.就写一篇mark一下吧. 现场比赛的时候去枚举了儿子用了线段树+dfs序,和预想的一样T了. 可以换一个想法,从儿子对父亲的贡献来思考. 在点中先假设一个节点的每一个 ...

  4. F - Auxiliary Set HDU - 5927 (dfs判断lca)

    题目链接: F - Auxiliary Set HDU - 5927 学习网址:https://blog.csdn.net/yiqzq/article/details/81952369题目大意一棵节点 ...

  5. HDU 4442 Physical Examination&lpar;贪心&rpar;

    HDU 4442 Physical Examination(贪心) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=4442 Descripti ...

  6. HDU 5835 Danganronpa (贪心)

    Danganronpa 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5835 Description Chisa Yukizome works as ...

  7. HDU 5821 Ball (贪心)

    Ball 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5821 Description ZZX has a sequence of boxes nu ...

  8. hdu 4004 &lpar;二分加贪心&rpar; 青蛙过河

    题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=4004 题目意思是青蛙要过河,现在给你河的宽度,河中石头的个数(青蛙要从石头上跳过河,这些石头都是在垂 ...

  9. Saving HDU(hdu2111,贪心)

    Saving HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

随机推荐

  1. 51nod1183&lpar;Edit Distance&rpar;

    题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1183 题意:中文题啦- 思路:dp 用dp[i][j]表示从 ...

  2. PHPCMS V9 环境搭建

    PHPCMS V9的学习总结分为以下几点: [1]PHPCMS 简介 PHP原始为Personal Home Page的缩写,(外文名:PHP: Hypertext Preprocessor,中文名: ...

  3. abstract修饰符

    abstract(C# 参考) abstract 修饰符指示所修饰的内容缺少实现或未完全实现. abstract 修饰符可用于类.方法.属性.索引器和事件. 在类声明中使用 abstract 修饰符以 ...

  4. CSS 居中大全

    <center> text-align:center 在父容器里水平居中 inline 文字,或 inline 元素 vertical-align:middle 垂直居中 inline 文 ...

  5. linux创建用户,指定组

    本博客不再更新 该文章新链接移步:http://it.lovepet.vip/archives/7/ 一.创建用户: 1.使用命令 useradd 例:useradd test——创建用户test  ...

  6. UIAutomation识别UI元素

    MS UI Automation(Microsoft User Interface Automation:UIA)是随.net framework3.0一起发布的,虽然在如今这个几乎每天都有各种新名词 ...

  7. php5&period;6在yum下安装redis

    yum install redis php-redis --enablerepo=remi,remi-php56 设置redis开机自动启动,具体路径以实际为准, echo "/usr/bi ...

  8. hadoop部署

    [root@xiong ~]# hostnamectl set-hostname hadoop001 [root@xiong ~]# vim /etc/hostnamehadoop001 vim /e ...

  9. Python内置函数&lpar;57&rpar;——setattr

    英文文档: setattr(object, name, value) This is the counterpart of getattr(). The arguments are an object ...

  10. UVA 10382 Watering Grass(区间覆盖)

    n sprinklers are installed in a horizontal strip of grass l meters long and w meters wide. Each spri ...