题意:给你一张无向连通图,对于求有多少对$(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)的更多相关文章

  1. Codeforces Round #606 (Div. 2) E - Two Fairs(DFS,反向思维)

  2. 20191214 Codeforces Round #606 (Div. 2, based on Technocup 2020 Elimination Round 4)

    概述 切了 ABCE,Room83 第一 还行吧 A - Happy Birthday, Polycarp! 题解 显然这样的数不会很多. 于是可以通过构造法,直接求出 \([1,10^9]\) 内所 ...

  3. Codeforces Round #606 (Div. 2)

    传送门 A. Happy Birthday, Polycarp! 签到. Code /* * Author: heyuhhh * Created Time: 2019/12/14 19:07:57 * ...

  4. 【cf比赛记录】Codeforces Round #606 (Div. 2, based on Technocup 2020 Elimination Round 4)

    比赛传送门 只能说当晚状态不佳吧,有点头疼感冒的症状.也跟脑子没转过来有关系,A题最后一步爆搜没能立即想出来,B题搜索没有用好STL,C题也因为前面两题弄崩了心态,最后,果然掉分了. A:简单数学 B ...

  5. 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 ...

  6. Codeforces Round #606 (Div. 1) Solution

    从这里开始 比赛目录 我菜爆了. Problem A As Simple as One and Two 我会 AC 自动机上 dp. one 和 two 删掉中间的字符,twone 删掉中间的 o. ...

  7. Codeforces Round #606 (Div. 2, based on Technocup 2020 Elimination Round 4)

    链接 签到题,求出位数,然后9*(位数-1)+ 从位数相同的全一开始加看能加几次的个数 #include<bits/stdc++.h> using namespace std; int m ...

  8. Codeforces Round #606 (Div. 2) D - Let's Play the Words?(贪心+map)

  9. Codeforces Round #606 Div. 2 比赛总结

    比赛情况 bq. A题 Wrong Answer on test 2 , E题sb题没切.bqbqbq. 比赛总结 bq. 那就直接上题解吧!^-^ A 数位dp,分类讨论,注意细节. Talk is ...

随机推荐

  1. schroeder reverb matlab实现

    原理参考:Natural sounding artificial reverberation combFilter.m: function output = combFilter(delay, gai ...

  2. command failed: npm install --loglevel error --registry=https://registry.npm 用vue-cli 4.0 新建项目总是报错

    昨天新买的本本,今天布环境,一安装vue-cli发现都4.0+的版本了,没管太多,就开始新建个项目感受哈,一切运行顺利,输入 "vue create app" 的时候,一切貌似进展 ...

  3. SSI注入漏洞

    简介 SSI是英文Server Side Includes的缩写,翻译成中文就是服务器端包含的意思.从技术角度上说,SSI就是在HTML文件中,可以通过注释行调用的命令或指针.SSI具有强大的功能,只 ...

  4. 微信小程序 scroll-view 左右横向滑动没有效果(无法滑动)问题

    小程序组件 scroll-view 中分别有上下竖向滑动和左右横向滑动之分,在这次项目中刚好需要用到横向滑动,但在测试过程中发现横向滑动没有了效果(静止在那里没移动过),经调试发现: 1.scroll ...

  5. 博客下方有一个2D动画

    第一步 第二步 第三步 将下面一段代码放入 支持 js代码的那里  (如果没有申请是不可以使用js代码的,记得申请) <script src="https://eqcn.ajz.mie ...

  6. php商城数据库的设计 之无限分类

    商品分类,使用无限分类 即: -------如何创建数据表 pid---父级分类id,如果是顶级分类则为0 path---1,用户分类的排序 . 排序示例: 实现逻辑:获取type表的所有分类,ord ...

  7. Bugku-CTF分析篇-这么多数据包(这么多数据包找找吧,先找到getshell的流)

    这么多数据包 这么多数据包找找吧,先找到getshell的流

  8. Linux - XShell - alt 快捷键的设置

    1. 概述 命令行的 alt 快捷键可能会冲突 2. 环境 os win10 centos7 xshell xhell6 3. 场景 开启 centos7 虚拟机 在 win10 打开 xshell6 ...

  9. etc/hosts文件详解

    Linux 修改 etc/hosts文件 hosts文件 hosts —— the static table lookup for host name(主机名查询静态表). hosts文件是Linux ...

  10. Linux查看mysql是否启动的命令

    使用命令 # service mysqld status 或者 # service mysql status 来查看mysql 的启动状态. 如果是 mysqld is stopped 那就说明mys ...