dfs序 树状数组--hdu5927 Auxiliary Set
来源:互联网 发布:平价化妆水知乎 编辑:程序博客网 时间:2024/06/11 16:24
An auxiliary set is a set containing vertices satisfying at least one of the two conditions:
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.
//dfs序将子树的问题转换为序列上的问题
//再配合上数据结构,ST表、线段树、树状数组etc
//然后如果一个坏点有两个子儿子的子树内存在好点则这个点就是好点 <- LCA
#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
using namespacestd;
const int maxn =1e5 + 5;
vector<int> mp[maxn];
int in[maxn],out[maxn];//dfs序
int te = 0;
int assist[maxn];
int n;
int unimp[maxn];
int fat[maxn] = {0,0};
void init(int n)
{
te = 0;
for (int i =1; i <= n; i ++) {
mp[i].clear();
}
}
void dfs(int s)
{
in[s] = ++te;
for(int i =0;i < mp[s].size();i ++)
{
if(mp[s][i] !=fat[s]){
fat[mp[s][i]] = s;
dfs(mp[s][i]);
}
}
out[s] =te;
}
void add(int x,int d)
{
for (; x <=n; x += (x & -x)) {
assist[x] += d;
}
}
int query(int x)
{
int res =0;
for(;x >0 ;x -= (x & -x)){
res += assist[x];
}
return res;
}
bool check(int x)
{
int cnt =0;
for (int i =0; i < mp[x].size(); i ++) {
int y =mp[x][i];
if(y !=fat[x]){
if(query(out[y]) -query(in[y] -1) != out[y] -in[y] + 1)
if(++cnt ==2) returntrue;
}
}
returnfalse;
}
int main()
{
int T;cin >> T;
int q,u,v;
for (int cn =1; cn <= T; cn ++) {
scanf("%d%d",&n,&q);
init(n);
for (int i =0; i < n -1; i ++) {
scanf("%d%d",&u,&v);
mp[u].push_back(v);
mp[v].push_back(u);
}
dfs(1);
printf("Case #%d:\n",cn);
int sz =0;
for (int i =0; i < q; i ++) {
scanf("%d",&sz);
for (int j =0; j < sz; j ++) {
scanf("%d",&unimp[j]);
add(in[unimp[j]],1);
}
int ans =n;
for (int j =0; j < sz; j ++) {
if(!check(unimp[j])) ans--;//if unimp[j] has two import son return true
}
for (int j =0; j < sz; j ++) {
add(in[unimp[j]], -1);
}
printf("%d\n",ans);
}
}
return0;
}
- dfs序 树状数组--hdu5927 Auxiliary Set
- HDU5927 Auxiliary Set(dfs)
- Auxiliary Set hdu5927(dfs)
- HDU5927-Auxiliary Set
- 【HDU5927 2016CCPC东北地区大学生程序设计竞赛 - 重现赛 F】【dfs序 + 线段树 or 树状数组 复杂度计算】Auxiliary Set 一个点如果是好点或是两个好点的LCA就是好
- HDU5927 Auxiliary Set ->树形DP
- hdu 5927 Auxiliary Set dfs
- Auxiliary Set----DFS思维题
- hdu 5927 Auxiliary Set (dfs)
- hdu5927 dfs
- dfs序+树状数组
- HDU 5927 Auxiliary Set dfs(思维)
- HDU 5927 Auxiliary Set DFS好题
- HDU 5927 Auxiliary Set(DFS)
- HDU 5927 Auxiliary Set (DFS+模拟)
- hdu 5927 Auxiliary Set dfs+逆向思维
- 2016东北赛 && hdu 5927 Auxiliary Set [dfs序+BIT]【数据结构】
- hdu5877 dfs序+树状数组
- hibernate多对一映射报错
- webpack获取图片的真实路径
- java 爬取彩票开奖信息,爬取双色球历史开奖,爬取大乐透历史开奖
- 近日总结
- 优秀博文推荐:源码篇
- dfs序 树状数组--hdu5927 Auxiliary Set
- 类和对象
- 【深度学习】优化方法
- 使用jdbc连接数据库
- 图像显著性检测
- PageRank
- Melis系统崩溃问题分析以及解决思路
- RocketMQ实战(二)
- Spring-MVC拦截器