Description
煤礦工地可以看成是由隧道連接挖煤點組成的無向圖。爲安全起見,希望在工地發生事故時所有挖煤點的工人都能有一條出路逃到救援出口處。於是礦主決定在某些挖煤點設立救援出口,使得無論哪一個挖煤點坍塌之後,其他挖煤點的工人都有一條道路通向救援出口。請寫一個程序,用來計算至少需要設置幾個救援出口,以及不同最少救援出口的設置方案總數。
Input
輸入文件有若干組數據,每組數據的第一行是一個正整數 N(N≤500),表示工地的隧道數,接下來的 N 行每行是用空格隔開的兩個整數 S 和 T,表示挖 S 與挖煤點 T 由隧道直接連接。輸入數據以 0 結尾。
Output
輸入文件中有多少組數據,輸出文件 output.txt 中就有多少行。每行對應一組輸入數據的 結果。其中第 i 行以 Case i: 開始(注意大小寫,Case 與 i 之間有空格,i 與:之間無空格,: 之後有空格),其後是用空格隔開的兩個正整數,第一個正整數表示對於第 i 組輸入數據至少需 要設置幾個救援出口,第二個正整數表示對於第 i 組輸入數據不同最少救援出口的設置方案總 數。輸入數據保證答案小於 2^64。輸出格式參照以下輸入輸出樣例。
Sample Input
9
1 3
4 1
3 5
1 2
2 6
1 5
6 3
1 6
3 2
6
1 2
1 3
2 4
2 5
3 6
3 7
0
Sample Output
Case 1: 2 4
Case 2: 4 1
HINT
Case 1 的四組解分別是(2,4),(3,4),(4,5),(4,6);
Case 2 的一組解爲(4,5,6,7)。
題解
一眼過去肯定要求割點嘛。
如果沒有割點呢那麼隨便建兩個出口就好了,方案數
考慮與通過刪掉割點的話就得到很多個聯通塊,如果一個聯通塊與超過一個割點連接,那麼很顯然這個聯通塊內是不需要出口的,因爲你掛掉任意一個割點他還能通過另外的跑到其他聯通塊去。二如果一個聯通塊只與一個割點相連,那他之內一定要建一個出口了,要不然這個割點掛了整個聯通塊就完蛋了。乘法原理一下就是總的方案數。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 500 + 10, M = 1000 + 10;
int n, m, tot, dfs_clock;
int to[M], nxt[M], hd[N], dfn[N], low[N], cut[N], cnt, siz;
ll ans1, ans2;
inline void insert(int u, int v){
to[++tot] = v; nxt[tot] = hd[u]; hd[u] = tot;
to[++tot] = u; nxt[tot] = hd[v]; hd[v] = tot;
}
void init(){
n = tot = dfs_clock = cnt = 0;
memset(hd, 0, sizeof(hd));
memset(dfn, 0, sizeof(dfn));
memset(low, 0, sizeof(low));
memset(cut, 0, sizeof(cut));
int u, v;
for(int i = 1; i <= m; i++){
scanf("%d%d", &u, &v);
n = max(n, u); n = max(n, v);
insert(u, v);
}
}
void tarjan(int u, int fa){
low[u] = dfn[u] = ++dfs_clock;
int v, child = 0;
for(int i = hd[u]; i; i = nxt[i]){
v = to[i];
if(!dfn[to[i]]){
child++;
tarjan(v, u);
low[u] = min(low[u], low[v]);
if(low[v] >= dfn[u]) cut[u] = 1, cnt++;
}
else if(v != fa && dfn[v] < dfn[u])
low[u] = min(low[u], dfn[v]);
}
if(!fa && child == 1) cut[u] = 0, cnt--;
}
void dfs(int u, int clk){
dfn[u] = 1; siz++;
int v;
for(int i = hd[u]; i; i = nxt[i]){
v = to[i];
if(cut[v] && dfn[v] != clk) cnt++, dfn[v] = clk;
else if(!dfn[v]) dfs(v, clk);
}
}
void work(){
int t = 0;
while(~scanf("%d", &m) && m){
init();
for(int i = 1; i <= n; i++) if(!dfn[i]) tarjan(i, 0);
memset(dfn, 0, sizeof(dfn));
ans1 = 0, ans2 = 1;
if(!cnt) ans1 = 2, ans2 = (n * (n-1)) / 2;
else
for(int i = 1; i <= n; i++)
if(!dfn[i] && !cut[i]){
cnt = siz = 0;
dfs(i, i);
if(cnt == 1) ans1++, ans2 *= siz;
}
printf("Case %d: %lld %lld\n", ++t, ans1, ans2);
}
}
int main(){
work();
return 0;
}