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 ...
随机推荐
- 前端知识之css
css的几种引入方式 行内样式 行内式是在标记的style属性中设定css样式,不推荐大规模使用 <p style='color:red'>hello world</p> 内部 ...
- v:bind指令对于传boolean值的注意之处
1,
- ISR吞吐性能问题
ISR大致可以分几类: Cisco 860.880.890 ISR1800 (fixed).1800 (modular).2800.3800 Series ISR1900.2900.3800.3900 ...
- VIM - 问题: 简单变换
1. 概述 学习 Java 反射的时候, 碰到的简单问题 手动可以处理, 但是真的很浪费时间 想熟悉一下工具的使用 2. 题目 源 Modifier.isAbstract(int modifiers) ...
- 动态设置 layui select 为选中状态
// 当前的select的id $('#type').val('你的value值'); //更新全部 layui.form.render();
- C# 之 代码实现延时
Task.Delay();异步实现 using System;using System.Threading.Tasks; namespace csharpYS{ class Program { sta ...
- Python 语法特点:注释/编写规则/命名规范
1.注释 1)单行注释 # 2) 多行注释 前后三个单引号或双引号 ‘’‘ ... ''' """ ...""" ...
- Dart语言学习(三)Dart数值型
一.类型 数值型有 num,int, double num a = ; a = 12.5; print(a); print(a.runtimeType); ; // b = 20.5; print( ...
- python进阶(十七)xml(下)
1.XML简介 xml用到的地方:tomcat配置文件 1) xml 指可扩展标记语言(Extensible Markup Language) 2) xml 被设计用于结构化.存储和传输数据 3) x ...
- SQL - 各种joins