Mining Your Own Business UVALive - 5135

John Digger is the owner of a large illudium phosdex mine. The mine is made up of a series of tunnels that meet at various large junctions. Unlike some owners, Digger actually cares about the welfare of his workers and has a concern about the layout of the mine. Specifically, he worries that there may a junction which, in case of collapse, will cut off workers in one section of the mine from other workers (illudium phosdex, as you know, is highly unstable). To counter this, he wants to install special escape shafts from the junctions to the surface. He could install one escape shaft at each junction, but Digger doesn’t care about his workers that much. Instead, he wants to install the minimum number of escape shafts so that if any of the junctions collapses, all the workers who survive the junction collapse will have a path to the surface. Write a program to calculate the minimum number of escape shafts and the total number of ways in which this minimum number of escape shafts can be installed.
Input
The input consists of several test cases. The first line of each case contains a positive integer N (N ≤ 5·104) indicating the number of mine tunnels. Following this are N lines each containing two distinct integers s and t, where s and t are junction numbers. Junctions are numbered consecutively starting at 1. Each pair of junctions is joined by at most a single tunnel. Each set of mine tunnels forms one connected unit (that is, you can get from any one junction to any other). The last test case is followed by a line containing a single zero.
Output
For each test case, display its case number followed by the minimum number of escape shafts needed for the system of mine tunnels and the total number of ways these escape shafts can be installed. You may assume that the result fits in a signed 64-bit integer. Follow the format of the sample output.
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


本来是刚学点双连通分量来当作模板题写的,谁知道这么多坑。

题意:给一个连通图,问你在地下假如一个点不能走了,必须建造几个逃生通道让每个点的人都能够回到地面(即每个点的人都能到达建造通道的点)。

思路:点双连通分量。找出所有的割点。

坑点(自己没想出来。。。):图中没有割点,需要涂2个,即n*(n-1)/2 中方案。(1个不能走就走另一个),那么如果连通分量有2个以上割点就也不需要。

然后就是普通的,只有一个割点的,累乘每个连通块的点数-割点数(1个)即可

Code:

#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int AX = 1e5+66;
int index1 , tot ;
int head[AX];
struct Node{
	int u , v;
	int next1;
}G[AX*2];
int n , m ;
int bcc_cnt;
int DFN[AX];
int iscut[AX];
int bccno[AX];
int LOW[AX];
stack<Node>S;
vector<int> bcc[AX];

void init(){
	tot = 0 ;
	memset(head , -1 , sizeof(head));
}

void add( int u , int v ){
	G[tot].u = u ; G[tot].v = v;
	G[tot].next1 = head[u];
	head[u] = tot ++;

	G[tot].u = v; G[tot].v = u;
	G[tot].next1 = head[v];
	head[v] = tot++;
}

void Tarjan( int x , int fa ){
	int son = 0;
	DFN[x] = LOW[x] = ++index1;
	for( int i = head[x] ; ~i ; i = G[i].next1 ){
		if( !DFN[G[i].v] ){
			S.push(G[i]);
			son ++ ;
			Tarjan( G[i].v , x );
			LOW[x] = min( LOW[x] , LOW[G[i].v] );
			if( LOW[G[i].v] >= DFN[x] ){
				iscut[x] = 1;
				++bcc_cnt;
				bcc[bcc_cnt].clear();
				while(1){
					Node tmp = S.top(); S.pop();
					if( bccno[tmp.u] != bcc_cnt ){
						bcc[bcc_cnt].push_back(tmp.u);
						bccno[tmp.u] = bcc_cnt;
					}
					if( bccno[tmp.v] != bcc_cnt ){
						bcc[bcc_cnt].push_back(tmp.v);
						bccno[tmp.v] = bcc_cnt;
					}
					if( tmp.u == x && tmp.v == G[i].v ){
						break;
					}
				}
			}
		}else{
			if( DFN[G[i].v] < DFN[x] && G[i].v != fa ){
				S.push(G[i]);
				LOW[x] = min( LOW[x] , DFN[G[i].v] );
			}
		}
	}
	if( fa < 0 && son == 1 ){
		iscut[x] = 0;
	}
}

void Find_Cut( ){
	bcc_cnt = 0;
	index1 = 0;
	memset( DFN , 0 ,sizeof(DFN) );
	memset( LOW , 0 ,sizeof(LOW) );
	memset( iscut , 0 ,sizeof(iscut) );
	memset( bccno , 0 ,sizeof(bccno) );
	for( int i = 1 ; i <= n ; i++ ){
		if( !DFN[i] ){
			Tarjan( i , -1 );
		}
	}
}

int main(){
	int C = 0;
	while( ~scanf("%d",&m ) && m ){
		init();
		int x , y ;
		n = 0;
		for( int i = 0 ; i < m ; i++ ){
			cin >> x >> y;
			n = max( n , max(x,y) );
			add( x , y );
		}
		Find_Cut();
	/*	cout << "-------" << endl;
		for( int i = 1 ; i <= bcc_cnt ; i ++ ){
			for( int j = 0 ; j < bcc[i].size() ; j ++ ){
				cout << bcc[i][j] << ' ';
			}
			cout << endl;
		}
		for( int i = 1 ; i <= n ;i ++ ){
			if( iscut[i] ) cout << i << endl;
		}
		cout << "-------" << endl;*/
		cout << "Case "<< ++C << ": " ;
		if( bcc_cnt == 1 ){
			cout << 2 << ' ' << (LL)n*(n-1) / 2 << endl;
		}else{
			LL res = 1;
			LL ans = 0;
			for( int i = 1 ; i <= bcc_cnt ; i ++ ){
				int size = bcc[i].size();
				int cut_tot = 0;
				for( int j = 0 ; j < size ; j++ ){
					if( iscut[bcc[i][j]] ){
						cut_tot ++ ;
					}
				}	
				if( cut_tot == 1 ){
					ans ++ ;
					res *= (LL)(size-1);
				}
			}
			cout << ans << ' ' << res << endl;
			
		}

	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值