UVA1108 HDU3844 Mining Your Own Business

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


Problem Description
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×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.
 

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
 

Source
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[N];
int vis1[N],vis2[N],low[N],dfn[N],inst[N],in[N],out[N];
stack<int>st;
vector<int>fl[N]; //保存双联通分量
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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值