题意:给你一张无向连通图,对于求有多少对$(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. set_set_switching_activity

    set_switching_activity  -static   0.2   -toggle_rate   20 -period  1000 [all_inputs] 这时,翻转率设置的节点是输入, ...

  2. form表单提交且接口回调显示提交成功

    前端: <form method="post" enctype="multipart/form-data" id="formSubmit&quo ...

  3. ubuntu16.04spyder闪退

    解决办法我试了好用 sudo pip install --upgrade html5lib==.0b8 完事.

  4. 联网调试DE1

    步骤:打开PUTTY,com3,串口,115200 重启开发板, 输入root, 输入   cd /mnt 输入    ifconfig 尝试一下自动分配网址:udhcpc 网络直连失败,原因占时不知 ...

  5. Mongodb学习笔记(四)管理

    一.数据导出.导入 数据导入:mongoexport ./mongoimport -d 数据库 -c 集合 文件名 数据导出:mongoimport 参数说明: -d:指明使用的库,如text -c: ...

  6. List 数据分批入库

    直接贴代码,主要运用 List<E> subList(int fromIndex, int toIndex); 把 List 分割 /** * 保存批价结果 * * @param pric ...

  7. 两个Beta函数类型的积分及其一般形式

    \[\Large\displaystyle \int_{0}^{1}\frac{\sqrt[4]{x\left ( 1-x \right )^{3}}}{\left ( 1+x \right )^{3 ...

  8. Yii2.0 连接数据库

    打开数据库配置文件common\config\main-local.php

  9. 【C语言】函数调用的简单例子

    #include<stdio.h> void p1() { printf("******************\n"); } void p2() { printf(& ...

  10. POJ 1995 Raising Modulo Numbers(快速幂)

    嗯... 题目链接:http://poj.org/problem?id=1995 快速幂模板... AC代码: #include<cstdio> #include<iostream& ...