Blow up the city

时间限制: 1 Sec  内存限制: 128 MB

题目描述

Country A and B are at war. Country A needs to organize transport teams to deliver supplies toward some command center cities.

In order to ensure the delivery works efficiently, all the roads in country A work only one direction. Therefore, map of country A can be regarded as DAG( Directed Acyclic Graph ). Command center cities only received supplies and not send out supplies.

Intelligence agency of country B is credibly informed that there will be two cities carrying out a critical transporting task in country A.

As long as **any** one of the two cities can not reach a command center city, the mission fails and country B will hold an enormous advantage. Therefore, country B plans to destroy one of the n cities in country A and all the roads directly connected. (If a city carrying out the task is also a command center city, it is possible to destroy the city to make the mission fail)

Now country B has made q hypotheses about the two cities carrying out the critical task.
Calculate the number of plan that makes the mission of country A fail.
 

输入

The first line contains a integer T (1≤T≤10), denoting the number of test cases.

In each test case, the first line are two integers n,m, denoting the number of cities and roads(1≤n≤100,000,1≤m≤200,000).
Then m lines follow, each with two integers u and v, which means there is a directed road from city u to v (1≤u,v≤n,u≠v).

The next line is a integer q, denoting the number of queries (1≤q≤100,000)
And then q lines follow, each with two integers a and b, which means the two cities carrying out the critical task are a and b (1≤a,b≤n,a≠b).

A city is a command center if and only if there is no road from it (its out degree is zero).

输出

For each query output a line with one integer, means the number of plan that makes the mission of country A fail.

样例输入

2
8 8
1 2
3 4
3 5
4 6
4 7
5 7
6 8
7 8
2
1 3
6 7
3 2
3 1
3 2
2
1 2
3 1

样例输出

4
3
2
2

题意:有一个DAG,求有多少方案,使得割掉图中一个点,图上特定两个点不能都到达出度为0的点。
思路:反向建边,连接超级源点到原图中出度为0的点。那么题目就变成了:有一个DAG,求有多少种方案,使得割掉图中一个点,从超级源点不能都到达两个特定点。这就是支配树问题。支配树是一个有向图抽象出来的树,它满足某一点到根节点的路径上的点,都是根结点在原图中到达这个点的必经点。
#include<bits/stdc++.h>
#define N 100005
using namespace std; struct ss
{
int u,v,next;
};
ss edg[*N];
int head[N],sum_edge=; void addedge(int u,int v)
{
edg[sum_edge]=(ss){u,v,head[u]};
head[u]=sum_edge++;
} int grand[N][]={};
int depth[N],DEPTH; void deal(int u,int v)
{
grand[v][]=u;
depth[v]=depth[u]+; for(int i=;i<=DEPTH;i++)
{
grand[v][i]=grand[grand[v][i-]][i-];
}
} int lca(int a,int b)
{
if(a==||b==)return a+b; if(depth[a]>depth[b])swap(a,b);
for(int i=DEPTH;i>=;i--)
if(depth[a]<depth[b]&&depth[grand[b][i]]>=depth[a])b=grand[b][i]; for(int i=DEPTH;i>=;i--)
if(grand[a][i]!=grand[b][i])
{
a=grand[a][i];
b=grand[b][i];
} if(a!=b)
{
return grand[a][];
}
return a;
} void init(int n)
{
DEPTH=floor(log(n + 0.0) / log(2.0));
memset(grand[],,sizeof(grand[]));
depth[]=;
sum_edge=;
memset(head,-,sizeof(head));
} void getControl_tree(int n,int s)//从s出发可以到达图的所有点,s为支配树的根
{
int fa[N]={};
int rd[N]={};
stack<int>Stack; for(int i=;i<=n;i++)
for(int j=head[i];j!=-;j=edg[j].next)rd[edg[j].v]++; Stack.push(s); while(!Stack.empty())
{
int x=Stack.top();
Stack.pop();
deal(fa[x],x);//建支配树 for(int i=head[x];i!=-;i=edg[i].next)
{
int v=edg[i].v;
fa[v]=lca(fa[v],x);
rd[v]--;
if(!rd[v])Stack.push(v);
}
}
} int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n,m;
scanf("%d %d",&n,&m);
init(n); int rd[N]={}; while(m--)
{
int u,v;
scanf("%d %d",&u,&v);
addedge(v,u);
rd[u]++;
} for(int i=;i<=n;i++)if(!rd[i])addedge(n+,i);
getControl_tree(n+,n+); scanf("%d",&m);
while(m--)
{
int u,v;
scanf("%d %d",&u,&v);
int LCA=lca(u,v);
printf("%d\n",depth[u]+depth[v]-depth[LCA]--+);
}
}
return ;
}

支配树参考博客:https://www.cnblogs.com/fenghaoran/p/dominator_tree.html

Blow up the city的更多相关文章

  1. Day10 - C - Blow up the city HDU - 6604

    Country A and B are at war. Country A needs to organize transport teams to deliver supplies toward s ...

  2. 【HDOJ6604】Blow up the city(支配树)

    题意:给定一个n点m边的DAG,将只有入边的点称为周驿东点 q次询问,每次给定a,b两点,询问删去某个点x和其相连的所有边,能使a,b至少其中之一不能到达任何周驿东点的x的个数 n,q<=1e5 ...

  3. 2019 Multi-University Training Contest 3

    B.Blow up the city solved by F0_0H 210min 题意 给一个DAG,每次询问给定u,v,求使得u或v不能与中心点联通的关键点个数 做法 按照拓扑序建树 新加节点的父 ...

  4. ZJUT11 多校赛补题记录

    牛客第一场 (通过)Integration (https://ac.nowcoder.com/acm/contest/881/B) (未补)Euclidean Distance (https://ac ...

  5. BZOJ 2001: [Hnoi2010]City 城市建设

    2001: [Hnoi2010]City 城市建设 Time Limit: 20 Sec  Memory Limit: 162 MBSubmit: 1132  Solved: 555[Submit][ ...

  6. History lives on in this distinguished Polish city II 2017/1/5

    原文 Some fresh air After your time underground,you can return to ground level or maybe even a little ...

  7. History lives on in this distinguished Polish city 2017/1/4

    原文 History lives on in this distinguished Polish city Though it may be ancient. KraKow, Poland, is a ...

  8. #1094 : Lost in the City

    时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Little Hi gets lost in the city. He does not know where he is ...

  9. GeoIP Legacy City数据库安装说明

    Here is a brief outline of the steps needed to install GeoIP Legacy City on Linux/Unix. The installa ...

随机推荐

  1. mybatis框架中XxxxMaper.xml的文件

    我们知道在mybatis框架中,config.xml中会关联到许多的XxxxMapper的xml文件,这些文件又对应着一个个的接口,来观察下这些xml文件 从以下这个文件为例子: <?xml v ...

  2. <day005>jQuery事件、文档基本操作 + 点赞事件

    任务1: jQuery的基本操作 <!DOCTYPE html> <html lang="zh-CN"> <head> <meta cha ...

  3. <数据分析>初级入门

    1.何为数据分析? 数据分析是指用适当的统计方法对收集来的大量数据进行分析,将它们加以汇总和理解消化,以求最大化地开发数据的功能,发挥数据的作用. 直接的理解:提炼杂乱无章的数据背后的信息,总结出研究 ...

  4. 二分图最佳匹配KM算法 /// 牛客暑期第五场E

    题目大意: 给定n,有n间宿舍 每间4人 接下来n行 是第一年学校规定的宿舍安排 接下来n行 是第二年学生的宿舍安排意愿 求满足学生意愿的最少交换次数 input 2 1 2 3 4 5 6 7 8 ...

  5. 2 _ 基本框架 _ 检测VMX环境

    VT 是先开为大,谁先开谁上层,谁上层 谁权限大. 1 判断是否支持 VMX intel 白皮书 第3卷 传入 参数eax =1, 返回值 ecx 的第5位 = 1 则 surpported VMX. ...

  6. Type.GetType(string.contains(','))

    例如 Type type = Type.GetType("ACalCoreServiceLib.BaseService,ACalCoreServiceLib"); 里面的ACalC ...

  7. 使用 SourceTree 操作时弹出 password required

    通过 https 的方式克隆仓库的,SourceTree 推送等操作的时候会弹出提示要求输入密码. 在仓库里面设置: 远程仓库-选中仓库-点击编辑-修改 url 路径, 路径格式,以码云为例: htt ...

  8. 箭头函数报错:Add @babel/plugin-proposal-class-properties (https://git.io/vb4SL) to the 'plugins' section of your Babel config to enable transformation.

    解决:根目录新建babel.config.js加入如下内容 module.exports = { presets: [ "@babel/preset-env", "@ba ...

  9. 使用UUID和int自增主键的区别

    知其然,知其所以然.在看到生成UUID的代码,后带给我的百度结合自己的经验再写下来的区别 一.UUID做主键: 优点: .保证数据在表和库都是独立的,有利于后续的分库 .合并表的时候主键不会重复 .有 ...

  10. iOS开发本地推送(iOS10)UNUserNotificationCenter

    1.简介 iOS10之后苹果对推送进行了封装,UNUserNotificationCenter就这样产生了.简单介绍本地推送的使用UserNotifications官方文档说明! 2.简单使用UNUs ...