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

?? ?? ??
题意:求点对中,满足要互达必须经过a,b两点的对数,图为无向连通图

若(x,y)要满足互达必须经过(a,b),反过来想,
a必须通过b点到达y点:满足a--->b--->y;
b必须通过a点到达x点:满足b--->a--->x,无向图:x--->a--->b;
连起来即为:x--->a--->b--->y;

int vis[MAXN];
vector<int>edge[MAXN];
void dfs(int x,int e)
{
    vis[x]=1;
    if(x==e) return ;
    for(auto v:edge[x])
    {
        if(vis[v]==0)
        {
            vis[v]=1;
            dfs(v,e);
        }
    }
}
int main()
{
    int t;cin>>t;
    while(t--)
    {
        int n,m,a,b;
        cin>>n>>m>>a>>b;//
        rpp(i,n) edge[i].clear(),vis[i]=0;
        rep(i,m)
        {
            int x,y;cin>>x>>y;
            edge[x].push_back(y);
            edge[y].push_back(x);
        }
        int num0=0,num1=0;
        dfs(a,b);
        rpp(i,n) if(!vis[i]) ++num0;
        rpp(i,n) vis[i]=0;
        dfs(b,a);
        rpp(i,n) if(!vis[i]) ++num1;
        ll ans=1ll*num0*num1;
        cout<<ans<<endl;
    }
    //stop;
    return 0;
}

原文地址:https://www.cnblogs.com/Herlo/p/12064106.html

时间: 2024-08-02 09:26:41

Codeforces Round #606 (Div. 2) E - Two Fairs(DFS,反向思维)的相关文章

Codeforces Round #606 (Div. 2) D. Let&#39;s Play the Words?(贪心+map)

?? ?? ?? 题意:给你一些序列,要求把这些序列翻转之后能首尾相连(01,10),并且字符串不能相同,询问最小步数: 1.我们只关心这个字符串首尾位置,一共只有四种情况:00,01,10,11:00 和 11 是没必要翻转的,剩下 01,10 只要存在就可以相互抵消(0110,1001这种),剩下多的 01 or 10,就是我们最后需要翻转的字符串,数量为abs(num01-num10)/2: 2.为了满足字符串不能相同,一开始就记录下哪写字符串不可以翻转,例,假如s翻转之后为rev,rev

Codeforces Round #480 (Div. 2) C 贪心 D 数字、思维 E 树上倍增

Codeforces Round #480 (Div. 2) C. Posterized 题意: 给出 n 个数,都是区间 [0,255] 内的数,要你把 [0,255] 划分成多个长度 <=k 的不重叠的子区间.每个数必须包含在一个子区间内,且这个数的价值是这个子区间的左端点.要你输出这 n 数的价值,且这 n 个价值字典序要最小. tags: 首先很明显字典序最小,那对于第 i 个数 p[i] 定它的区间时,左端点肯定要尽可能小.所以我们直接枚举区间 [ p[i]-k+1, p[i] ] 定

Codeforces Round #556 (Div. 2) - C. Prefix Sum Primes(思维)

Problem  Codeforces Round #556 (Div. 2) - D. Three Religions Time Limit: 1000 mSec Problem Description Input Output Sample Input 51 2 1 2 1 Sample Output 1 1 1 2 2 题解:这个题有做慢了,这种题做慢了和没做出来区别不大... 读题的时候脑子里还意识到素数除了2都是奇数,读完之后就脑子里就只剩欧拉筛了,贪心地构造使得前缀和是连续的素数,那

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

比赛传送门 只能说当晚状态不佳吧,有点头疼感冒的症状.也跟脑子没转过来有关系,A题最后一步爆搜没能立即想出来,B题搜索没有用好STL,C题也因为前面两题弄崩了心态,最后,果然掉分了. A:简单数学 B:数学+排序 C:字符串搜索 A // https://codeforces.com/contest/1277/problem/A /* 题意: 给出一个数,求不大于该数的完美整数的个数(完美整数指全是一个数组成的数字,如:111, 333333, 444444, 9, 8888 ...) 分析:

Codeforces Round #606 Div. 2 比赛情况

比赛情况 bq. A题 Wrong Answer on test 2 , E题sb题没切.bqbqbq. 比赛总结 bq. 那就直接上题解吧!^-^ A 数位dp,分类讨论. Talk is cheap.Show me the code. B 我们把数值一样的数放在一起,扔进堆里.按数值从大到小处理就OK了. 注意值域比较大,用一下 \(STL\) 里面的 map. Talk is cheap.Show me the code. #include<bits/stdc++.h> using na

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

A. 签到题:思路为:所求答案 = 9 * (字符长度 - 1) + 最高位数 +(- 1)//通过判断语言确定是否需要再减个一 如果a****** > *******则需要加一反之不需要 #include <bits/stdc++.h> using namespace std; typedef long long ll; int main () { ll t; cin >> t; while(t--) { ll ans; ll temp = 0; string num; c

Codeforces Round #360 (Div. 1)A (二分图&dfs染色)

题目链接:http://codeforces.com/problemset/problem/687/A 题意:给出一个n个点m条边的图,分别将每条边连接的两个点放到两个集合中,输出两个集合中的点,若不可能则输出-1: 思路:通过画图我们不难发现,图中没有出现长度为奇数的环则是可行的,反之则是不行的.那么现在我们只需判断有木有长度为偶数的环即可. 对于这点我们可以直接用dfs搜索+染色,对于当前标记为1的点,我们将其所有儿子标记为2, 对于当前标记为2的点,将其所有儿子标记为1,若出现某个节点的标

Codeforces Round #272 (Div. 2) C. Dreamoon and Sums (数学 思维)

题目链接 这个题取模的时候挺坑的!!! 题意:div(x , b) / mod(x , b) = k( 1 <= k <= a).求x的和 分析: 我们知道mod(x % b)的取值范围为 1  - (b-1).那么我们可以从这一点入口来进行解题.. mod (x, b) = 1 时, x  =  b + 1, 2b + 1, 3b + 1..... a * b + 1. mod (x , b) = 2 时, x =  2b + 2, 4b + 2, 6b + 2, ..... 2a * b

Codeforces Round #225 (Div. 1) C. Propagating tree dfs序+树状数组

C. Propagating tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/383/problem/C Description Iahub likes trees very much. Recently he discovered an interesting tree named propagating tree. The tree consists of n nodes numb