题目分析
这道题让你求至少需要装多少个escape shafts,并且在满足最少的escape shafts情况下有多少种选择方案。
这样我们就可以对本图求双联通分量,如果如果这个连通分量有2个以及以上割点那么这个连通分量不需要escape shafts,如果只有一个联通分量,那么可以选择一个非割点的escape shafts,那么可以选择的方案数有该连通分量的结点数减1。对于整个图都是一个联通分量的则需要特判,这是需要2个escape shafts,方案数位n*(n-1)/2.
#include <stack>
#include <vector>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 5e4+100;
#define LL long long
int pre[maxn], bccno[maxn], iscut[maxn];
int dfs_clock, bcc_cnt;
vector <int> G[maxn], bcc[maxn];
struct Edge{
int u, v;
};
stack <Edge> S;
int dfs(int u, int fa){
int lowu = pre[u] = ++dfs_clock;
int child = 0;
for(int i = 0; i < G[u].size(); i++){
int v = G[u][i];
Edge e = (Edge){u, v};
if(!pre[v]){
S.push(e);
child++;
int lowv = dfs(v, u);
lowu = min(lowu, lowv);
if(lowv >= pre[u]){
iscut[u] = true;
bcc_cnt++; bcc[bcc_cnt].clear();
for(;;){
Edge x = S.top(); S.pop();
if(bccno[x.u] != bcc_cnt){
bcc[bcc_cnt].push_back(x.u);
bccno[x.u] = bcc_cnt;
}
if(bccno[x.v] != bcc_cnt){
bcc[bcc_cnt].push_back(x.v);
bccno[x.v] = bcc_cnt;
}
if(x.u == u && x.v == v) break;
}
}
}
else if(pre[v] < pre[u] && v != fa){
S.push(e);
lowu = min(lowu, pre[v]);
}
}
if(fa < 0 && child == 1) iscut[u] = 0;
return lowu;
}
void find_bcc(int n){
memset(pre, 0, sizeof(pre));
memset(iscut, 0, sizeof(iscut));
memset(bccno, 0, sizeof(bccno));
dfs_clock = bcc_cnt = 0;
for(int i = 0; i < n; i++)
if(!pre[i]) dfs(i, -1);
}
int main(){
int kase = 0;
int n;
while(scanf("%d", &n) != EOF && n){
for(int i = 0; i < maxn; i++) G[i].clear();
int u, v;
for(int i = 0; i < n; i++){
scanf("%d%d", &u, &v);
u--, v--;
G[u].push_back(v);
G[v].push_back(u);
}
find_bcc(maxn);
LL ans1 = 0, ans2 = 1;
for(int i = 1; i <= bcc_cnt; i++){
int cut_cnt = 0;
for(int j = 0; j < bcc[i].size(); j++)
if(iscut[bcc[i][j]]) cut_cnt++;
if(cut_cnt == 1){ //如果只有一个割点,那么剩余的不是割点的都可以选,如果2个及以上那么可以利用别的双联通分量的井
ans1++;
ans2 *= (LL)(bcc[i].size() - cut_cnt);
}
}
if(bcc_cnt == 1){ //整个图是一个双联通分量需要特判
ans1 = 2LL;
ans2 *= (LL)bcc[1].size()*(bcc[1].size()-1)/2;
}
printf("Case %d: %lld %lld\n", ++kase, ans1, ans2);
}
return 0;
}