您的位置:首页 > 其它

UVA1108 HDU3844 Mining Your Own Business

2015-03-21 09:45 330 查看

Mining Your Own Business

Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1583 Accepted Submission(s): 255



[align=left]Problem Description[/align]
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.

[align=left]Input[/align]
The input consists of several test cases. The first line of each case contains a positive integer N (N <= 5×10^4) 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.

[align=left]Output[/align]
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.

[align=left]Sample Input[/align]

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


[align=left]Sample Output[/align]

Case 1: 2 4
Case 2: 4 1


[align=left]Source[/align]
2011WorldFinal

题意:一座地下稀有金属矿由n条隧道和一些连接点组成,每条隧道连接两点。任意两点之间最多一条隧道。你的任务是在一些连接点处安装逃生装置,使得不管哪个连接点倒塌,不在此连接点的所有矿工都能到达太平井逃生。为了节约成本,你应当在尽量少的连接点安装太平井。输出数目和方案数。

分析:当图无割点时,可以任选两个点,ans = (v-1)*v/2;

求出图的双联通分量,对于只含一个割点并且由多个点构成的双联通分量,可以任选一个非割点。

#pragma comment(linker, "/STACK:102400000,102400000")
//点过多,不加会暴栈
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<stack>
#include<queue>
#include<vector>
#include<climits>
using namespace std;
const int N = 60000;
int INF = INT_MAX;
typedef long long LL;
vector<int>G
;
int vis1
,vis2
,low
,dfn
,inst
,in
,out
;
stack<int>st;
vector<int>fl
; //保存双联通分量
int num,tim;
int root;
void tarjan1(int u) //求割点
{
vis1[u] = 1;
low[u] = dfn[u] = ++tim;
for(int i=0; i<G[u].size(); i++) {
int v = G[u][i];
if(!vis1[v]) {
tarjan1(v);
low[u] = min(low[u],low[v]);
if(dfn[u] <= low[v]) vis1[u]++;
}
else low[u] = min(low[u],dfn[v]);
}
if(u==root&&vis1[u]>2 || u!=root&&vis1[u]>1) vis1[u] = 2;
else vis1[u] = 1;
}
void tarjan2(int u, int fa) //求双联通分量
{
vis2[u] = 1;
low[u] = dfn[u] = ++tim;
inst[u] = 1;
st.push(u);
for(int i=0; i<G[u].size(); i++) {
int v = G[u][i];
if(!vis2[v]) {
tarjan2(v,u);
low[u] = min(low[u],low[v]);
if(dfn[u] <= low[v]) {
int j;
num++;
do {
j = st.top(); st.pop();
fl[num].push_back(j);
} while(j!=v);
fl[num].push_back(u);
}
}
else if(inst[v] && fa!=v) low[u] = min(low[u],dfn[v]);
}
}
int main()
{
int cas = 0;
int n,m;
while(scanf("%d",&m) == 1 && m) {
for(int i=1; i<N; i++) G[i].clear();
int i,j;
n = -1;
for(int k=1; k<=m; k++) {
scanf("%d %d",&i,&j);
G[i].push_back(j);
G[j].push_back(i);
n = max(n,max(i,j));
}
memset(vis1,0,sizeof(vis1));
tim = 0;
for(i=1; i<=n; i++)
if(!vis1[i]) {
root = i;
tarjan1(i);
}

memset(vis2,0,sizeof(vis2));
while(!st.empty()) st.pop();
tim = num = 0;
memset(inst,0,sizeof(inst));
for(i=1; i<=n; i++) fl[i].clear();
for(int i=1; i<=n; i++)
if(!vis2[i]) tarjan2(i,0);
if(num == 1) printf("Case %d: 2 %I64d\n",++cas,(LL)fl[1].size()*(fl[1].size()-1)>>1);
else {
int ans1 = 0;
LL ans2 = 1;
for(i=1; i<=num; i++) {
int a = 0, b = 0;
for(j=0; j< fl[i].size(); j++) {
int u = fl[i][j];
if(vis1[u] == 2) a++;
else b++;
}
if(a == 1) ans1++, ans2*=b;
}
printf("Case %d: %d %d\n",++cas,ans1,ans2);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: