hdu 5927 Auxiliary Set 贪心
Auxiliary Set
Time Limit: 9000/4500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
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.
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.
Then q lines follow, i-th line contains an integer indicating the size of the auxiliary set for each query.
6 3
6 4
2 5
5 4
1 5
5 3
3 1 2 3
1 5
3 3 1 4
3
6
3
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
#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 贪心的更多相关文章
- HDU 5927 Auxiliary Set 【DFS+树】(2016CCPC东北地区大学生程序设计竞赛)
Auxiliary Set Time Limit: 9000/4500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tot ...
- HDU 5927 Auxiliary Set (dfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5927 题意: 给你一棵树,其中有一些'不重要'的点,要是这些'不重要'的点的子树中有两个重要的点的LC ...
- hdu 5927 Auxiliary Set
传送门 分析:感觉这道题有点意思.就写一篇mark一下吧. 现场比赛的时候去枚举了儿子用了线段树+dfs序,和预想的一样T了. 可以换一个想法,从儿子对父亲的贡献来思考. 在点中先假设一个节点的每一个 ...
- F - Auxiliary Set HDU - 5927 (dfs判断lca)
题目链接: F - Auxiliary Set HDU - 5927 学习网址:https://blog.csdn.net/yiqzq/article/details/81952369题目大意一棵节点 ...
- HDU 4442 Physical Examination(贪心)
HDU 4442 Physical Examination(贪心) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=4442 Descripti ...
- HDU 5835 Danganronpa (贪心)
Danganronpa 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5835 Description Chisa Yukizome works as ...
- HDU 5821 Ball (贪心)
Ball 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5821 Description ZZX has a sequence of boxes nu ...
- hdu 4004 (二分加贪心) 青蛙过河
题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=4004 题目意思是青蛙要过河,现在给你河的宽度,河中石头的个数(青蛙要从石头上跳过河,这些石头都是在垂 ...
- Saving HDU(hdu2111,贪心)
Saving HDU Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
随机推荐
- 161118、linux(centos) 项目部署阶段相关命令汇总
1.ssh免密码登陆 主要命令 cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys-->添加公钥 service sshd restart ...
- tomcat监控脚本
工作所需,匆匆忙忙写了个监控tomcat的shell脚本,大概思路是这样的:先检测tomcat进程是否存在,如果不存在就启动,如果进程存在,检测页面返回码状态,如果是200就是正常,如果不是就重启. ...
- arduino 蓝牙控制RGB LED灯
/* 日期:2016.9.2 功能:arduino 蓝牙控制RGB LED灯 元件: 跳线公公头 * 8 rgbled, 220欧电阻 蓝牙模块 接线: 蓝牙模块VCC,GND分别接5V,GND;TX ...
- weblogic的jar包冲突
异常: Error creating bean with name 'sessionFactoryWrite' defined in class path resource [applicationC ...
- Docker Centos安装Redis以及问题处理
之前一篇文章 Redis安装及主从配置 介绍了redis的安装配置,另一篇文件介绍了 Docker Centos安装Openssh .今天将两篇文件结合一下——在Docker Centos环境下搭建r ...
- 第十一章 Android 内核驱动——Alarm
11.1 基本原理 Alarm 闹钟是 android 系统中在标准 RTC 驱动上开发的一个新的驱动,提供了一个定时器 用于把设备从睡眠状态唤醒,当然因为它是依赖 RTC 驱动的,所以它同时还可以 ...
- 【转】MYSQL入门学习之二:使用正则表达式搜索
转载地址:http://www.2cto.com/database/201212/173869.html 一.正则表达式介绍 www.2cto.com 正则表达式是用来匹配文本的特殊的串(字符 ...
- Matlab的libsvm的安装
最关键的是compilers的选择(对于把Microsoft visual stdio 2005或者其他的编译器安装在自定义目录下的这一步非常关键) 以下是步骤:>> mex -setu ...
- JAVA类访问URL
URI uri = new URI("https://www.baidu.com/"); Desktop desktop = null; if (Des ...
- asp.net 页面跳转传值的几种方式
参考地址: http://blog.csdn.net/zzzzzzzert/article/details/8486143 protected void Button1_Click(object se ...