首页 > 代码库 > hdu 3844 Mining Your Own Business (点双连通分量)

hdu 3844 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): 1392    Accepted Submission(s): 219


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
 

题意:
给你一个图,将上面一些点涂黑,使得去掉一个图中一个点之后每一个点都能到达一个黑点,求出最小放的点数以及方案数。

思路:
一个点双连通分量中,如果只有一个点为割点,那么这个分量必须在非割点的位置涂黑。问题就变为求解点双连通分量了。有一种特殊情况是,全图为一个分量,那么需任意涂两个点。

代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#define maxn 100005
#define MAXN 100005
#define INF 0x3f3f3f3f
#pragma comment (linker,"/STACK:102400000,102400000")
typedef long long ll;
using namespace std;

int n,m,cnt,tot,flag;
int lev,bcccnt;
int head[maxn];
int dfn[maxn],low[maxn];
bool vis[maxn],ok[maxn];
struct Node
{
    int v,w,next;
} edge[MAXN];
int stau[maxn],stav[maxn],top,bccno[maxn];
vector<int>bcc[maxn];

void addedge(int u,int v,int w)
{
    cnt++;
    edge[cnt].v=v;
    edge[cnt].w=w;
    edge[cnt].next=head[u];
    head[u]=cnt;
}
void Tarjan(int u,int pre)
{
    int i,j,t,v,num=0;
    low[u]=dfn[u]=++lev;
    for(i=head[u];i;i=edge[i].next)
    {
        v=edge[i].v;
        if(vis[v])
        {
            if(v!=pre) low[u]=min(low[u],dfn[v]);  // 桥不能用父亲来更新 割点随意
        }
        else
        {
            vis[v]=1;
            top++;
            stau[top]=u; stav[top]=v;
            num++;
            Tarjan(v,u);
            if(dfn[u]<=low[v])
            {
                if(pre!=0) ok[u]=1;  // 不是根
                bcccnt++;
                bcc[bcccnt].clear();
                while(!(stau[top]==u&&stav[top]==v))
                {
                    if(bccno[stav[top]]!=bcccnt) bccno[stav[top]]=bcccnt,bcc[bcccnt].push_back(stav[top]);
                    if(bccno[stau[top]]!=bcccnt) bccno[stau[top]]=bcccnt,bcc[bcccnt].push_back(stau[top]);
                    top--;
                }
                if(bccno[stav[top]]!=bcccnt) bccno[stav[top]]=bcccnt,bcc[bcccnt].push_back(stav[top]);
                if(bccno[stau[top]]!=bcccnt) bccno[stau[top]]=bcccnt,bcc[bcccnt].push_back(stau[top]);
                top--;
            }
            low[u]=min(low[u],low[v]);
        }
    }
    if(pre==0&&num>1) ok[u]=1;
}
int main()
{
    int i,j,t,u,v,w,ca=0;
    while(scanf("%d",&m),m)
    {
        cnt=n=0;
        memset(head,0,sizeof(head));
        for(i=1; i<=m; i++)  // 建图
        {
            scanf("%d%d",&u,&v);
            addedge(u,v,0);
            addedge(v,u,0);
            n=max(n,u);
            n=max(n,v);
        }
        memset(vis,0,sizeof(vis));
        memset(ok,0,sizeof(ok));
        memset(bccno,0,sizeof(bccno));
        bcccnt=0;
        for(i=1; i<=n; i++)// 割点或者桥的求解
        {
            if(vis[i]) continue ;
            lev=0;
            vis[i]=1;
            top=0;
            Tarjan(i,0);
        }
        ll ans=0,res=1;
        for(i=1;i<=bcccnt;i++)
        {
            int num=0;
            for(j=0;j<bcc[i].size();j++)
            {
                if(ok[bcc[i][j]]) num++;
            }
            if(num==1)
            {
                ans++; res*=(bcc[i].size()-1);
            }
        }
        if(bcccnt==1)
        {
            ans=2; res=ll(n)*ll(n-1)/2;
        }
        printf("Case %d: %I64d %I64d\n",++ca,ans,res);
    }
    return 0;
}

hdu 3844 Mining Your Own Business (点双连通分量)