Codeforces Round #606 (Div. 2) - E. Two Fairs(割点+dfs)
题意:给你一张无向连通图,对于求有多少对$(x,y)$满足互相到达必须经过$(a,b)$,其中$x\neq a,x\neq b,y\neq a,y\neq b$
思路:显然$a,b$都必须为割点,所以先用$tarjan$判断$a,b$是否都为割点,如果$a$或$b$有一个不为割点,那么答案就是$0$
当$a,b$都为割点时,答案为连通块$1$内点的个数$*$连通块$2$内点的个数,以求连通块$1$内点的个数为例,从$b$点开始$dfs$,当遇到$a$点时停止,统计出$($连通块$2+$复杂网络$+b+a)$这些点的个数,连通块$1$内点的个数就是用$n$减去$dfs$求出的点的个数,求连通块$2$内点的个数从$a$点开始$dfs$即可。
#include <iostream>
#include <algorithm>
#include <cstdio> using namespace std; const int N = ; typedef long long ll; struct node {
int to, nex;
}; node edge[ * N];
int t, n, m, a, b, cnt, head[N];
int dfn[N], timing, pcut[N];
int cnta, cntb, vis[N]; void dfs(int u, int mk, int a, int b)
{
vis[u] = ;
if ( == mk) cnta++;
if ( == mk) cntb++;
if ( == mk && u == b) return;
if ( == mk && u == a) return;
for (int i = head[u]; != i; i = edge[i].nex) {
int v = edge[i].to;
if (!vis[v]) dfs(v, mk, a, b);
}
return;
} void add_edge(int u, int v)
{
edge[++cnt].to = v;
edge[cnt].nex = head[u];
head[u] = cnt;
} int tarjan(int u, int fa)
{
int child = , lowu;
lowu = dfn[u] = ++timing;
for (int i = head[u]; != i; i = edge[i].nex) {
int v = edge[i].to;
if (!dfn[v]) {
child++;
int lowv = tarjan(v, u);
if (lowv >= dfn[u] && u != fa) pcut[u] = ;
lowu = min(lowu, lowv);
}
else if (v != fa) {
lowu = min(lowu, dfn[v]);
}
}
if (u == fa && child > ) pcut[u] = ;
return lowu;
} void init()
{
cnt = timing = cnta = cntb = ;
for (int i = ; i <= n; i++)
pcut[i] = dfn[i] = head[i] = ;
} int main()
{
scanf("%d", &t);
while (t--) {
scanf("%d%d%d%d", &n, &m, &a, &b);
init();
for (int i = ; i <= m; i++) {
int u, v;
scanf("%d%d", &u, &v);
add_edge(u, v), add_edge(v, u);
}
for (int i = ; i <= n; i++) {
if ( == dfn[i]) tarjan(i, i);
}
if ( == pcut[a] || == pcut[b]) {
printf("0\n");
continue;
}
for (int i = ; i <= n; i++) vis[i] = ;
dfs(a, , a, b);
for (int i = ; i <= n; i++) vis[i] = ;
dfs(b, , a, b);
ll x = ll(n - cnta), y = ll(n - cntb);
printf("%lld\n", x * y);
}
return ;
}
Codeforces Round #606 (Div. 2) - E. Two Fairs(割点+dfs)的更多相关文章
- Codeforces Round #606 (Div. 2) E - Two Fairs(DFS,反向思维)
- 20191214 Codeforces Round #606 (Div. 2, based on Technocup 2020 Elimination Round 4)
概述 切了 ABCE,Room83 第一 还行吧 A - Happy Birthday, Polycarp! 题解 显然这样的数不会很多. 于是可以通过构造法,直接求出 \([1,10^9]\) 内所 ...
- Codeforces Round #606 (Div. 2)
传送门 A. Happy Birthday, Polycarp! 签到. Code /* * Author: heyuhhh * Created Time: 2019/12/14 19:07:57 * ...
- 【cf比赛记录】Codeforces Round #606 (Div. 2, based on Technocup 2020 Elimination Round 4)
比赛传送门 只能说当晚状态不佳吧,有点头疼感冒的症状.也跟脑子没转过来有关系,A题最后一步爆搜没能立即想出来,B题搜索没有用好STL,C题也因为前面两题弄崩了心态,最后,果然掉分了. A:简单数学 B ...
- Codeforces Round #606 (Div. 2, based on Technocup 2020 Elimination Round 4) 题解
Happy Birthday, Polycarp! Make Them Odd As Simple as One and Two Let's Play the Words? Two Fairs Bea ...
- Codeforces Round #606 (Div. 1) Solution
从这里开始 比赛目录 我菜爆了. Problem A As Simple as One and Two 我会 AC 自动机上 dp. one 和 two 删掉中间的字符,twone 删掉中间的 o. ...
- Codeforces Round #606 (Div. 2, based on Technocup 2020 Elimination Round 4)
链接 签到题,求出位数,然后9*(位数-1)+ 从位数相同的全一开始加看能加几次的个数 #include<bits/stdc++.h> using namespace std; int m ...
- Codeforces Round #606 (Div. 2) D - Let's Play the Words?(贪心+map)
- Codeforces Round #606 Div. 2 比赛总结
比赛情况 bq. A题 Wrong Answer on test 2 , E题sb题没切.bqbqbq. 比赛总结 bq. 那就直接上题解吧!^-^ A 数位dp,分类讨论,注意细节. Talk is ...
随机推荐
- Python记:通用的序列操作之成员资格(听起来倒是有些抽象的!)
______________________________永远守护这一尘不染的真心! 要检查特定的值是否包含在序列中,可使用运算符in.它检查是否满足指定的条件,并返回相应的值:满足时返回True, ...
- JarvisOJ - Writeup(5.31更新)
此篇用来记录我在jarivsOJ上的一些题解,只给解题思路,不放flag Misc 0x01 You Need Python(300) 题目有两个文件,一个py文件,另一个是经过编码的key 文件ke ...
- Vue 实现todolist的添加删除功能
直接上代码 vm.$emit( eventName, [-args] ) 触发当前实例上的事件 可选附加参数 传给监听器回调. <style> #app{ margin: 100px; } ...
- winform中的DataGridView的列宽设置
DataGridView有一个属性AutoSizeColumnMode,他有很多枚举值: 1.AllCells 调整列宽,以适合该列中的所有单元格的内容,包括标题单元格. 2.AllCellsExc ...
- WLC-Virtual Interface IP
关于思科WLC,有很多接口类型,如下所示,这里主要针对Virtual IP记录一些最佳实践建议. 思科WLC的Virtual IP地址的作用: • Mobility management • DHCP ...
- 素问 - 信贷和GDP
摘自<小韭的学习圈> Q:近期看到2019年金融统计数据,全年人民币贷款增加16.81万亿元,同比多增6439亿元.这里有个问题我储备好久了,没有高人指点俺.请问2019年全年GDP近10 ...
- Go_type
1. type的定义和使用 Go语言支持函数式编程,可以使用高阶编程语法.一个函数可以作为另一个函数的参数,也可以作为另一个函数的返回值,那么在定义这个高阶函数的时候,如果函数的类型比较复杂,我们可以 ...
- 部署DVWA时的一些问题和解决办法(二)
DVWA reCAPTCHA key: Missing解决方法 编辑 dvwa/config/config.inc.php这个配置文件 $_DVWA[ 'recaptcha_public_key' ] ...
- Spring JdbcTemplate类常用的方法
execute(String sql) 可执行任何sql语句,但返回值是void,所以一般用于数据库的新建.修改.删除和数据表记录的增删改. int update(String sql) int ...
- 什么是buffer?
Buffer 类的实例类似于整数数组,但 Buffer 的大小是固定的.且在 V8 堆外分配物理内存. Buffer 的大小在被创建时确定,且无法调整. Buffer 类在 Node.js 中是一个全 ...