洛谷 P3225 矿场搭建 —— tarjan + 点双分析

题目链接:点我啊╭(╯^╰)╮

题目大意:

    无向图,任意一个矿点坍塌以后,其他所有矿点都必须有路到救援出口
    求最少设置几个救援出口,设置最少出口的方案数

解题思路:

    这道题确实有点难。。。

    对于一个点双联通分量,大小为 K K K ,若没有割点与它相连
    说明它与世隔绝,则在这个联通分量里必须设置两个救援出口
    因为只设置一个点可能坍塌,方案数是 C ( k , 2 ) C(k, 2) C(k,2)

    对于一个点双联通分量,若有一个割点与它相连
    只需要设置一个救援出口即可,因为即使这个点坍塌了,其他点也可以通过这个割点到其他分量里
    因此方案数是 k k k

    对于一个点双联通分量,若有 ≥ ≥ 两个割点与它相连
    则不需要设置出口,无论哪个割点坍塌了,都有另一个割点连通到外部
    因此方案数是 0 0 0

    那么会不会出现所有联通分量都有两个以上的割点与它相连呢??
    显示是不存在的,聪明的孩子都知道。
    这样一看好像这题很简单,是错觉吗

#include<bits/stdc++.h>
#define rint register int
#define deb(x) cerr<<#x<<" = "<<(x)<<'\n';
using namespace std;
typedef long long ll;
typedef pair <int,int> pii;
const int maxn = 505;
int n, m, cnt, cntc;
int dfn[maxn], low[maxn], vis[maxn];
int tot, color, iscut[maxn], cas;
ll ans1, ans2;
vector <int> g[maxn];

void tarjan(int u, int fa){
	dfn[u] = low[u] = ++tot;
	int child = 0;
	for(auto v : g[u]){
		if(v == fa) continue;
		if(!dfn[v]) {
			child++;
			tarjan(v, u);
			low[u] = min(low[u], low[v]);
			if(low[v] >= dfn[u]) iscut[u] = 1;
		} else low[u] = min(low[u], dfn[v]);
	}
	if(!fa && child==1) iscut[u] = 0;
}

void dfs(int u, int color){
	vis[u] = color, cnt++;
	for(auto v : g[u]){
		if(iscut[v] && color!=vis[v]) cntc++, vis[v] = color;
		if(!vis[v]) dfs(v, color);
	}
}

signed main() {
	while(~scanf("%d", &m) && m){
		memset(dfn, 0, sizeof(dfn));
		memset(low, 0, sizeof(low));
		memset(vis, 0, sizeof(vis));
		memset(iscut, 0, sizeof(iscut));
		tot = color = ans1 = n = 0, ans2 = 1;
		for(int i=0; i<maxn; i++) g[i].clear();
		for(int i=1, u, v; i<=m; i++) {
			scanf("%d%d", &u, &v);
			g[u].push_back(v);
			g[v].push_back(u);
			n = max(n, max(u, v));
		}
		for(int i=1; i<=n; i++)
			if(!dfn[i]) tarjan(i, 0);
		for(int i=1; i<=n; i++){
			if(iscut[i] || vis[i]) continue;
			color++, cnt = cntc = 0;
			dfs(i, color);
			if(cntc == 0) ans1 += 2, ans2 *= cnt * (cnt - 1) / 2;
			else if(cntc == 1) ans1++, ans2 *= cnt;
		}
		printf("Case %d: %lld %lld\n", ++cas, ans1, ans2);
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值