Day10 - C - Blow up the city HDU - 6604
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 nn 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 qq hypotheses about the two cities carrying out the critical task.
Calculate the number of plan that makes the mission of country A fail.
InputThe first line contains a integer TT (1≤T≤10)(1≤T≤10), denoting the number of test cases.
In each test case, the first line are two integers n,mn,m, denoting the number of cities and roads(1≤n≤100,000,1≤m≤200,000)(1≤n≤100,000,1≤m≤200,000).
Then mm lines follow, each with two integers uu and vv, which means there is a directed road from city uu to vv (1≤u,v≤n,u≠v)(1≤u,v≤n,u≠v).
The next line is a integer q, denoting the number of queries (1≤q≤100,000)(1≤q≤100,000)
And then qq lines follow, each with two integers aa and bb, which means the two cities carrying out the critical task are aa and bb (1≤a,b≤n,a≠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).OutputFor each query output a line with one integer, means the number of plan that makes the mission of country A fail.Sample Input
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
Sample Output
4
3
2
2
国家A和B处于战争状态。A国需要组织运输队向一些指挥中心城市提供物资。为了确保交付有效,A国的所有道路只能向一个方向发展。因此,国家A的地图可以被视为DAG。指挥中心城市只收到物资而不发送物资。B国的情报机构可靠地获悉,将有两个城市在A国执行关键的运输任务。只要两个城市中的任何一个都无法到达指挥中心城市,任务就会失败,B国将拥有巨大的优势。因此,B国计划摧毁A国的n个城市之一和所有直接连接的道路。(如果执行任务的城市也是指挥中心城市,则可以摧毁城市以使任务失败)现在,B国对这两个执行关键任务的城市提出了q假设。计算使国家A的任务失败的计划数量。
思路:支配树,相当于求2点的lca到出度为0的点的数量,拓扑反向建树,DAG->拓扑排序->从出度为0的点开始反向建树->通过lca,因为该题可能有多个连通块,就把每个出度为0的点连接到一个多源上,方便统计
const int maxm = 1e5+;
int head[maxm<<], edgecnt, depth[maxm], grand[maxm][], n, limit, in[maxm], que[maxm];
struct edge{
int u, v, nex;
} edges[maxm<<];
void addedge(int u, int v) {
edges[++edgecnt].u = u;
edges[edgecnt].v = v;
edges[edgecnt].nex = head[u];
head[u] = edgecnt;
}
void init() {
edgecnt = ;
memset(head, , sizeof(head));
memset(in, , sizeof(in));
memset(grand, , sizeof(grand));
memset(depth, , sizeof(depth));
}
void toposort() {
int l = , r = ;
for(int i = ; i <= n; ++i)
if(!in[i]) que[r++] = i;
while(l < r) {
int u = que[l++];
for(int i = head[u]; i; i = edges[i].nex) {
if(!--in[edges[i].v])
que[r++] = edges[i].v;
}
}
}
int lca(int a, int b) {
if(a == b) return a;
if(depth[a] > depth[b]) swap(a, b);
for(int i = limit; i >= ; --i)
if(depth[a] <= depth[b] - (<<i)) b = grand[b][i];
if(a == b) return a;
for(int i = limit; i >= ; --i)
if(grand[a][i] == grand[b][i]) continue;
else {
a = grand[a][i], b = grand[b][i];
}
return grand[a][];
}
void run_case() {
init();
int m, u, v, q;
cin >> n >> m;
for(int i = ; i < m; ++i) {
cin >> u >> v;
addedge(u, v);
in[v]++;
}
limit = floor(log(n+0.0)/log(2.0))+;
toposort();
for(int i = n; i > ; --i) {
int u = que[i];
if(!head[u]) {
addedge(, u);
depth[u] = ;
continue;
}
int v = edges[head[u]].v;
for(int j = edges[head[u]].nex; j; j = edges[j].nex) v = lca(v, edges[j].v);
depth[u] = depth[v] + ;
grand[u][] = v;
for(int j = ; j <= limit; ++j) grand[u][j] = grand[grand[u][j-]][j-];
}
cin >> q;
while(q--) {
cin >> u >> v;
cout << depth[u] + depth[v] - depth[lca(u, v)] << "\n";
}
}
int main() {
ios::sync_with_stdio(false), cin.tie();
int t;
cin >> t;
while(t--)
run_case();
return ;
}
Day10 - C - Blow up the city HDU - 6604的更多相关文章
- Blow up the city
Blow up the city 时间限制: 1 Sec 内存限制: 128 MB 题目描述 Country A and B are at war. Country A needs to organ ...
- 【HDOJ6604】Blow up the city(支配树)
题意:给定一个n点m边的DAG,将只有入边的点称为周驿东点 q次询问,每次给定a,b两点,询问删去某个点x和其相连的所有边,能使a,b至少其中之一不能到达任何周驿东点的x的个数 n,q<=1e5 ...
- 2019 Multi-University Training Contest 3
B.Blow up the city solved by F0_0H 210min 题意 给一个DAG,每次询问给定u,v,求使得u或v不能与中心点联通的关键点个数 做法 按照拓扑序建树 新加节点的父 ...
- ZJUT11 多校赛补题记录
牛客第一场 (通过)Integration (https://ac.nowcoder.com/acm/contest/881/B) (未补)Euclidean Distance (https://ac ...
- HDU 1505 City Game (hdu1506 dp二维加强版)
F - City Game Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submi ...
- HDU 3634 City Planning (离散化)
City Planning Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- hdu 3624 City Planning(暴力,也可扫描线)
City Planning Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) To ...
- HDU 3080 The plan of city rebuild(prim和kruskal)
The plan of city rebuild Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java ...
- Hdu 5352 MZL's City (多重匹配)
题目链接: Hdu 5352 MZL's City 题目描述: 有n各节点,m个操作.刚开始的时候节点都是相互独立的,一共有三种操作: 1:把所有和x在一个连通块内的未重建过的点全部重建. 2:建立一 ...
随机推荐
- DOC文档与DOCX文档有什么区别
doc 是 Microsoft Office 2003 里的 Word 文档,而 docx 是 Microsoft Office 2007 里的 Word 文档.高版本是向下兼容的,也就是能够打开 d ...
- FastDFS上传文件访问url地址直接下载
fdfs 存储节点storage安装nginx,修改nginx配置文件 location ~/group[1-9]/M00 { if ( $query_string ~* ^(.*)paramete ...
- Spring Boot Web 开发@Controller @RestController 使用教程
在 Spring Boot 中,@Controller 注解是专门用于处理 Http 请求处理的,是以 MVC 为核心的设计思想的控制层.@RestController 则是 @Controller ...
- 文件的读取与保存(try-with-resource优雅关闭)
借鉴:https://www.cnblogs.com/itZhy/p/7636615.html 一.背景 在Java编程过程中,如果打开了外部资源(文件.数据库连接.网络连接等),我们必须在这些外部资 ...
- RTT之时钟管理
时钟节拍 :等于 1/T_TICK_PER_SECOND 秒,用 SysTick_Handler实现,在每次加1时都会检查当前线程的时间片是否用完,以及是否有定时器超时.定时值应该为该值的整数倍.非整 ...
- 「luogu3402」【模板】可持久化并查集
「luogu3402」[模板]可持久化并查集 传送门 我们可以用一个可持久化数组来存每个节点的父亲. 单点信息更新和查询就用主席树多花 一个 \(\log\) 的代价来搞. 然后考虑如何合并两个点. ...
- 解决fastjson反序列化日期0000-00-00失败的方案
解决fastjson反序列化日期0000-00-00失败的方案 22 Jul 2016 一.案例场景复原 示例场景里涉及两个class:TestDemo.java, DateBeanDemo.java ...
- 【转】十步让你成为一名优秀的Web开发人员
第一步:学好HTML HTML(超文本标记语言)是网页的核心,因此你首先应该学好它,不要害怕,HTML很容易学习的,但也很容易误用,学懂容易要学精还得费点功夫,但学好HTML是成为Web开发人员的基本 ...
- vue element 时间选择器设置禁用日期
在 el-date-picker 组件中有一个 picker-options 属性 disabledDate 可以设置日期的可选范围 <el-date-picker v-model=" ...
- Kubernetes集群部署及简单命令行操作
三个阶段部署docker:https://www.cnblogs.com/rdchenxi/p/10381631.html 环境准备 [root@master ~]# hostnamectl set- ...