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 ...
随机推荐
- php用jquery-ajax上传多张图片限制图片大小
php用jquery-ajax上传多张图片限制图片大小 /** * 上传图片,默认大小限制为3M * @param String $fileInputName * @param number $siz ...
- Android的init过程(二):初始化语言(init.rc)解析【转】
转自:http://www.cnblogs.com/nokiaguy/p/3164799.html Android的init过程(一) 本文使用的软件版本 Android:4.2.2 Linux内核: ...
- 【转】Windows下搭建cvs服务器
转载地址:http://hi.baidu.com/iloverobot/item/fad1eb6d66c45e166995e66d 下载cvs server:CVSNT 网址为:http://www. ...
- js笔记---封装自己的Ajax方法
//地址 成功方法 失败方法function Ajax(url, funsucc, funfial) { var oAjax = null; if (window.XMLHttpRequest) { ...
- c# TCPclient
服务端 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; ...
- Uva 12563,劲歌金曲,01背包
题目链接:https://uva.onlinejudge.org/external/125/12563.pdf 题意:n首歌,每首歌的长度给出,还剩 t 秒钟,由于KTV不会在一首歌没有唱完的情况下切 ...
- 安装nginx python uwsgi环境 以及模拟测试
uwsgi帮助文档: http://uwsgi-docs-cn.readthedocs.io/zh_CN/latest/WSGIquickstart.html http://uwsgi-docs.re ...
- error: command 'x86_64-linux-gnu-gcc' failed with exit status 1
转载自:http://blog.csdn.net/wang1144/article/details/42277179 在ubuntu14.04版本上安装lxml,老是出错,在一番艰辛的搜索之后 ,终于 ...
- win10 + VS2015 + EF6 + MySQL
前置配置 在下面的网址去安装最新版的 (Connector/Net http://dev.mysql.com/downloads/connector/net/#downloads) 然后安装 MySQ ...
- 基于@AspectJ和schema的aop(一)
在前面我们使用Pointcut和Advice描述切点和增强, 并使用Advisor整合两者描述切面.@AspectJ使用注解来描述切点和增强.两者使用的方式不同, 但是在本质上都是一样的. 我们还是用 ...