题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6203

题意:n+1 个点 n 条边的树(点标号 0 ~ n),有若干个点无法通行,导致 p 组 U V 无法连通。问无法通行的点最少有多少个。

解法:按照询问的LCA深度排序,然后顺序标记每个询问的LCA。根据所给的树(任意点为根)预处理出每个点的前序 DFS 序和后序 DFS 序(需统一标号),及点的深度。根据 p 组 U V 处理每组两点的 LCA 。压入优先队列(LCA 深度大的点优先出队)。对于出队的 U V 及其对应的 LCA ,判断点 U 或点 V 是否在之前已禁止的某点的子树中,对于某点U如果在禁止通行的点P的子树中,则L[P]<=L[U]<=R[U]<=R[P]一定成立。所以可以利用树状数组区间更新单点查询,对于每个禁止通行点标记区间L[P],R[P]的所有节点。查询的时候如果L[U]被标记,说明U,V已经被隔断。

#include <bits/stdc++.h>
using namespace std;
const int maxn = 20100;
typedef long long LL;
vector <int> G[maxn];
int n, m, dfsclk, c[maxn], fa[maxn][21], dep[maxn], L[maxn], R[maxn];
bool vis[maxn];
struct node{
int u,v,uv;
node(){}
node(int u,int v,int uv):u(u),v(v),uv(uv){}
bool operator<(const node &rhs)const{
return dep[uv]<dep[rhs.uv];
}
};
void dfs(int x){
vis[x]=1;
L[x]=++dfsclk;
for(int i=0; i<G[x].size(); i++){
int v=G[x][i];
if(!vis[v]){
dep[v]=dep[x]+1;
fa[v][0]=x;
dfs(v);
}
}
R[x]=++dfsclk;
}
void init(){
for(int j=1; j<=20; j++)
for(int i=1; i<=n; i++)
fa[i][j] = fa[fa[i][j-1]][j-1];
}
int LCA(int u, int v){
if(dep[u]<dep[v]) swap(u,v);
for(int i=20; i>=0; i--){
if((dep[u]-dep[v])&(1<<i))
u=fa[u][i];
}
if(u==v) return u;
for(int i=20; i>=0; i--){
if(fa[u][i]!=fa[v][i]){
u=fa[u][i];
v=fa[v][i];
}
}
return fa[u][0];
}
int lowbit(int x){
return x&(-x);
}
void add(int x, int v){
while(x<maxn){
c[x]+=v;
x+=lowbit(x);
}
}
void update(int x, int y, int z){
add(x, z);
add(y+1, -z);
}
int query(int x){
int ret=0;
while(x>0){
ret+=c[x];
x-=lowbit(x);
}
return ret;
}
int main()
{
while(~scanf("%d", &n))
{
memset(c, 0, sizeof(c));
memset(vis, 0, sizeof(vis));
memset(dep, 0, sizeof(dep));
for(int i=0; i<=n+1; i++) G[i].clear();
for(int i=1; i<=n; i++){
int u,v;
scanf("%d %d", &u,&v);
u++,v++;
G[u].push_back(v);
G[v].push_back(u);
}
n++;
dfsclk = 0;
dfs(1);
init();
priority_queue <node> q;
scanf("%d", &m);
while(m--){
int u, v;
scanf("%d %d", &u,&v);
u++;
v++;
int uv = LCA(u, v);
q.push(node(u,v,uv));
}
int ans=0;
while(!q.empty()){
node now = q.top();
q.pop();
int flag = query(L[now.u])+query(L[now.v]);
if(flag==0){
ans++;
update(L[now.uv],R[now.uv],1);
}
}
printf("%d\n", ans);
}
return 0;
}

HDU 6203 2017沈阳网络赛 LCA,DFS+树状数组的更多相关文章

  1. HDU 5877 2016大连网络赛 Weak Pair(树状数组,线段树,动态开点,启发式合并,可持久化线段树)

    Weak Pair Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Tota ...

  2. HDU - 6393 Traffic Network in Numazu (LCA+RMQ+树状数组)

    这道题相当于将这两题结合: http://poj.org/problem?id=2763 http://codeforces.com/gym/101808/problem/K 题意:有N各点N条边的带 ...

  3. HDU 6201 2017沈阳网络赛 树形DP或者SPFA最长路

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6201 题意:给出一棵树,每个点有一个权值,代表商品的售价,树上每一条边上也有一个权值,代表从这条边经过 ...

  4. HDU 6200 2017沈阳网络赛 树上区间更新,求和

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6200 题意:给个图,有2种操作,一种是加一条无向边,二是查询u,v之间必须有的边的条数,所谓必须有的边 ...

  5. HDU 6199 2017沈阳网络赛 DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6199 题意:n堆石子,Alice和Bob来做游戏,一个人选择取K堆那么另外一个人就必须取k堆或者k+1 ...

  6. HDU 6205 2017沈阳网络赛 思维题

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6205 题意:给你n堆牌,原本每一堆的所有牌(a[i]张)默认向下,每次从第一堆开始,将固定个数的牌(b ...

  7. HDU 6198 2017沈阳网络赛 线形递推

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6198 题意:给出一个数k,问用k个斐波那契数相加,得不到的数最小是几. 解法:先暴力打表看看有没有规律 ...

  8. HDU 6195 2017沈阳网络赛 公式

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6195 题意:有M个格子,有K个物品.我们希望在格子与物品之间连数量尽可能少的边,使得——不论是选出M个 ...

  9. Luogu 2680 NOIP 2015 运输计划(树链剖分,LCA,树状数组,树的重心,二分,差分)

    Luogu 2680 NOIP 2015 运输计划(树链剖分,LCA,树状数组,树的重心,二分,差分) Description L 国有 n 个星球,还有 n-1 条双向航道,每条航道建立在两个星球之 ...

随机推荐

  1. vyos 基础配置

    vyos 基础配置 http://www.lowefamily.com.au/2015/11/29/using-a-vyos-router-with-hyper-v/1/http://thomasvo ...

  2. linux 实践到的命令 collection

    查看文件夹/文件 大小:du   :(disk usage) 要通过 1024 字节块概述一个目录树及其每个子树的磁盘使用情况,请输入: du -k /home/fran/filename 这在/ho ...

  3. 【刷题】BZOJ 1901 Zju2112 Dynamic Rankings

    Description 给定一个含有n个数的序列a[1],a[2],a[3]--a[n],程序必须回答这样的询问:对于给定的i,j,k,在a[i],a[i+1],a[i+2]--a[j]中第k小的数是 ...

  4. 20165218 《网络对抗技术》Exp1 逆向及Bof基础

    Exp1 逆向及Bof基础 基础知识 1. NOP, JNE, JE, JMP, CMP汇编指令的机器码 指令 机器码 NOP NOP指令即"空指令",在x86的CPU中机器码为0 ...

  5. vue入门教程

    vue视频教程(对vue有个概览,要掌握vue-cli的用法,对vue-router,vuex有基本的概念) https://www.imooc.com/learn/1091 1. vue-cli v ...

  6. Python之旅:并发编程之IO模型

    一 IO模型介绍 为了更好地了解IO模型,我们需要事先回顾下:同步.异步.阻塞.非阻塞 同步(synchronous) IO和异步(asynchronous) IO,阻塞(blocking) IO和非 ...

  7. C/C++:文本查询(单词查询)

    如题: C/C++: Textqurey.h(方便看都在.h里实现了): // // Created by 徐爱东 on 17/7/10. // #ifndef TEXTQUERY_TEXTQUERY ...

  8. pg_upgrade升级报错:Only the install user can be defined in the new cluster

    前两天pg11刚出来,打算测试一下,想将测试库升级到pg11,之前测试库的版本是pg9.6,后面我将它升到了pg10,打算在pg10的版本基础上升级到pg11. 但执行时,多次报出: Performi ...

  9. 转:RAC中比较replay, replayLast, and replayLazily

    A co-worker recently asked me about the difference between -replay, -replayLast, and -replayLazily i ...

  10. 介绍——基于类的视图(class-based view)

    ​刚开始的时候,django只有基于函数的视图(Function-based views).为了解决开发视图中繁杂的重复代码,基于函数的通用视图( Class-based generic views) ...