UVAlive 5135 Mining Your Own Business [点双连通分量] [求割顶]

来源:互联网 发布:php split 用法 编辑:程序博客网 时间:2024/05/21 22:54

Mining Your Own Business
Time Limit: 5000MS 64bit IO Format: %lld & %llu

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 ≤ 50000 ) 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


第一问求出最少安装多少个逃生口,使得任意矿井坏掉的时候任意其他井口都能逃生,第二问要求安装方案的个数。
那么首先安装在割顶是不划算的,因为割顶崩塌就必须还要至少两个逃生井,如果一个BCC中有两个割顶安装也是不划算的,因为分别都可以从两边的逃生井逃生,所以只需要在有且只有一个割顶的BCC中安装一个逃生井即可。。
另外第一个进去的点要特判一下。。
注意特判整体只有一个BCC的情况,答案就是(2,C(size,2))。。

#include<iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<cmath>#include<vector>#include<queue>#include<stack>#include<map>#include<set>#include<string>#include<iomanip>#include<ctime>#include<climits>#include<cctype>#include<algorithm>#ifdef WIN32#define AUTO "%I64d"#else#define AUTO "%lld"#endifusing namespace std;typedef long long LL;const int maxn=100005;const int INF=0x3f3f3f3f;int n,m;struct Edge{    int to,next;}edge[maxn];int head[maxn];int maxedge;inline void addedge(int u,int v){    edge[++maxedge]=(Edge){v,head[u]};    head[u]=maxedge;    edge[++maxedge]=(Edge){u,head[v]};    head[v]=maxedge;}bool init(){    scanf("%d",&m);    if(!m) return false;    memset(head,-1,sizeof(head));    maxedge=-1;n=-INF;    for(int i=1;i<=m;i++)    {        int s,t;        scanf("%d%d",&s,&t);        addedge(s,t);        n=max(n,s),n=max(n,t);    }    return true;}int dfs_clock;int dfn[maxn],bccno[maxn];bool iscut[maxn];struct EDGE{    int u,v;    bool operator == (const EDGE t) const    {        return u==t.u && v==t.v;    }};stack <EDGE> sta;struct BCC{    int cnt;    vector <int> bcc[maxn];    void clear()    {        cnt=0;        for(int i=1;i<=n+1;i++) bcc[i].clear();    }    void add(EDGE x)    {        if(bccno[x.u]!=cnt) bcc[cnt].push_back(x.u),bccno[x.u]=cnt;        if(bccno[x.v]!=cnt) bcc[cnt].push_back(x.v),bccno[x.v]=cnt;    }}Bcc;int dfs(int u,int fa){    int lowu=dfn[u]=++dfs_clock;    int child=0;    for(int i=head[u];~i;i=edge[i].next)    {        int v=edge[i].to;        EDGE e=(EDGE){u,v};        if(!dfn[v])        {            child++;            sta.push(e);            int lowv=dfs(v,u);            lowu=min(lowu,lowv);            if(lowv>=dfn[u])            {                Bcc.cnt++;iscut[u]=true;                EDGE tmp;                do                {                    tmp=sta.top();sta.pop();                    Bcc.add(tmp);                }while(!(tmp==e));            }        }        else if(dfn[v]<dfn[u]&&v!=fa) lowu=min(lowu,dfn[v]);    }    if(!~fa && !(child^1)) iscut[u]=false;//always remember to regret!!    return lowu;}void tarjan(){    memset(dfn,0,sizeof(dfn));    memset(iscut,0,sizeof(iscut));    memset(bccno,0,sizeof(bccno));    dfs_clock=0;Bcc.clear();    dfs(1,-1);}int main(){    #ifdef Local    freopen("mine.in","r",stdin);    freopen("mine.out","w",stdout);    #endif    int cas=0;    while(init())    {        cas++;        tarjan();        LL ans1=0,ans2=1;        if(Bcc.cnt^1)            for(int k=1;k<=Bcc.cnt;k++)            {                int cut_cnt=0,bcc_cnt=Bcc.bcc[k].size();                for(int i=0;i<bcc_cnt;i++)                    if(iscut[Bcc.bcc[k][i]]) cut_cnt++;                if(cut_cnt==1) ans1++,ans2*=(LL)(bcc_cnt-1);            }        else ans1=2,ans2=Bcc.bcc[1].size()*(Bcc.bcc[1].size()-1)/2;        printf("Case %d: " AUTO " " AUTO "\n",cas,ans1,ans2);    }    return 0;}
0 0
原创粉丝点击