Codeforces Round #606 E. Two Fairs 【思维】

题目链接

题意

给出一个 n n n 个顶点 m m m 条边的无向图和两个顶点 a 、 b a、b ab,问有多少对顶点 ( u , v ) (u, v) (u,v) 使 u u u v v v 必须经过 a , b a,b a,b 两点( u 、 v u、v uv != a 、 b a、b ab u u u != v v v

题解

可以这样想, u u u v v v 必须经过 a , b a,b a,b,也就是把 a , b a,b a,b 删了的话,这两个点就无法到达,所以我们可以对 a 、 b a、b ab 分别讨论

  1. a a a 为起点 dfs,每个点都能到达, c n t 1 = n cnt1=n cnt1=n
  2. b b b 删掉 dfs,能到达的点数为 cnt2

所以在 a a a 这边有 c n t 1 − c n t 2 − 1 cnt1-cnt2-1 cnt1cnt21 个点
同理统计 b b b 那边的点数
显然, a a a 这边的点要到 b b b 那边的点,必须经过 a , b a,b a,b,答案就是 a a a这边的点数 * b b b 那边的点数

#include<iostream>
#include<sstream>
#include<string>
#include<queue>
#include<map>
#include<unordered_map>
#include<set>
#include<vector>
#include<stack>
#include <utility>
#include<list>
#include<bitset>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<iomanip>
#include<time.h>
#include<random>
using namespace std;
#include<ext/pb_ds/priority_queue.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/hash_policy.hpp>
using namespace __gnu_pbds;
#include<ext/rope>
using namespace __gnu_cxx;

#define int long long
#define PI acos(-1.0)
#define eps 1e-9
#define lowbit(a) ((a)&-(a))
#define mid ((l+r)>>1)
#define mem(x,y) memset(x,y,sizeof x)

const int mod = 1e9+7;
int qpow(int a,int b){
	int ans=1;
	while(b){
		if(b&1)ans=(ans*a)%mod;
		a=(a*a)%mod;
		b>>=1;
	}
	return ans;
}
const int INF = 0x3f3f3f3f;
const int N = 5e5+10;
int cnt,x,y,vis[N],n,m,a,b;
vector<int>g[N];
void dfs(int u){
	vis[u]=1,cnt++;
	for(auto v:g[u])if(!vis[v])dfs(v);
}
#define endl '\n'
signed main(){
	std::ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	int t; cin>>t;
	while(t--){
		cin>>n>>m>>a>>b;
		for(int i=1;i<=n;i++)g[i].clear();
		for(int i=1;i<=m;i++){
			int u,v; cin>>u>>v;
			g[u].push_back(v),g[v].push_back(u);
		}
		
		x=y=n;//a,b都在时,每个点都能到
		
		for(int i=1;i<=n;i++)vis[i]=0; cnt=0;
		vis[b]=1; dfs(a); x=x-cnt-1; 
		
		for(int i=1;i<=n;i++)vis[i]=0; cnt=0;
		vis[a]=1; dfs(b); y=y-cnt-1;
		
		cout<<x*y<<endl;
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值