夏夜、

心若平似镜、何题不AC。

UVA 1108 Mining Your Own Business 双连通分量 割点

双连通分量:

//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<sstream>
#include<cmath>
#include<climits>
#include<string>
#include<map>
#include<queue>
#include<vector>
#include<stack>
#include<set>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
#define pb(a) push(a)
#define INF 0x1f1f1f1f
#define lson idx<<1,l,mid
#define rson idx<<1|1,mid+1,r
#define PI  3.1415926535898
template<class T> T min(const T& a,const T& b,const T& c) {
    return min(min(a,b),min(a,c));
}
template<class T> T max(const T& a,const T& b,const T& c) {
    return max(max(a,b),max(a,c));
}
void debug() {
#ifdef ONLINE_JUDGE
#else

    freopen("d:\\in1.txt","r",stdin);
    freopen("d:\\out1.txt","w",stdout);
#endif
}
int getch() {
    int ch;
    while((ch=getchar())!=EOF) {
        if(ch!=' '&&ch!='\n')return ch;
    }
    return EOF;
}

struct Edge
{
    int u,v;
};

const int maxn=50005;

int pre[maxn],bccno[maxn],iscut[maxn];
int dfs_clock,bcc_cnt;

vector<int>  g[maxn],bcc[maxn];

stack<Edge> s;

int dfs(int u,int fa)
{
    int lowu=pre[u]=++dfs_clock;
    int child=0;
    for(int i=0;i<g[u].size();i++)
    {
        int v=g[u][i];
        Edge e = (Edge){u,v};
        if(!pre[v])
        {
            s.push(e);
            child++;
            int lowv=dfs(v,u);
            lowu=min(lowu,lowv);
            if(lowv>=pre[u])
            {
                iscut[u]=1;
                bcc_cnt++;bcc[bcc_cnt].clear();
                while(1)
                {
                    Edge x = s.top();s.pop();
                    if(bccno[x.u]!=bcc_cnt)
                    {
                        bccno[x.u]=bcc_cnt;
                        bcc[bcc_cnt].push_back(x.u);
                    }

                    if(bccno[x.v]!=bcc_cnt)
                    {
                        bccno[x.v]=bcc_cnt;
                        bcc[bcc_cnt].push_back(x.v);
                    }
                    if(x.u==u&&x.v==v)break;
                }
            }
        }else if(pre[v]<=pre[u]&&v!=fa)
        {
            lowu=min(lowu,pre[v]);
            s.push(e);
        }
    }
    if(child==1&&fa<0)iscut[u]=0;
    return lowu;
}

void find_bcc()
{
    memset(bccno,0,sizeof(bccno));
    memset(pre,0,sizeof(bccno));
    memset(iscut,0,sizeof(iscut));
    dfs_clock=bcc_cnt=0;
    dfs(1,-1);
}

int main()
{
    int n;
    int ca=0;
    while(scanf("%d",&n)!=EOF&&n)
    {
        for(int i=1;i<=n+1;i++)
            g[i].clear();
        for(int i=1;i<=n;i++)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            g[u].push_back(v);
            g[v].push_back(u);
        }

        find_bcc();
//        printf("test begin:-----------------------------\n");
//        for(int i=1;i<=bcc_cnt;i++)
//        {
//            for(int j=0;j<bcc[i].size();j++)
//                printf("%d ",bcc[i][j]);
//            printf("\n");
//        }
//        printf("test end  :-----------------------------\n");
        long long ans1=0,ans2=1;
        for(int i=1;i<=bcc_cnt;i++)
        {
            int cut_cnt=0;
            for(int j=0;j<bcc[i].size();j++)
            {
                if(iscut[bcc[i][j]])cut_cnt++;
            }
            if(cut_cnt==1)
            {
                ans1++;ans2*=(long long)(bcc[i].size()-cut_cnt);
            }
        }
        if(bcc_cnt==1)
        {
            ans1=2;
            ans2=(long long)bcc[1].size()*(bcc[1].size()-1)/2;
        }
        printf("Case %d: %lld %lld\n",++ca,ans1,ans2);
    }
    return 0;
}
View Code

割点,两次DFS:

//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<sstream>
#include<cmath>
#include<climits>
#include<string>
#include<map>
#include<queue>
#include<vector>
#include<stack>
#include<set>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
#define pb(a) push(a)
#define INF 0x1f1f1f1f
#define lson idx<<1,l,mid
#define rson idx<<1|1,mid+1,r
#define PI  3.1415926535898
template<class T> T min(const T& a,const T& b,const T& c) {
    return min(min(a,b),min(a,c));
}
template<class T> T max(const T& a,const T& b,const T& c) {
    return max(max(a,b),max(a,c));
}
void debug() {
#ifdef ONLINE_JUDGE
#else

    freopen("d:\\in1.txt","r",stdin);
    freopen("d:\\out1.txt","w",stdout);
#endif
}
int getch() {
    int ch;
    while((ch=getchar())!=EOF) {
        if(ch!=' '&&ch!='\n')return ch;
    }
    return EOF;
}

struct Edge
{
    int u,v;
};

const int maxn=50005;
int pre[maxn],iscut[maxn],dfs_clock;
int vis[maxn];
vector<int> g[maxn];
int dfs1(int u,int fa)
{
    int lowu=pre[u]=++dfs_clock;
    int child=0;
    for(int i=0;i<g[u].size();i++)
    {
        int v=g[u][i];
        if(!pre[v])
        {
            child++;
            int lowv=dfs1(v,u);
            lowu=min(lowu,lowv);
            if(lowv>=pre[u])
            {
                iscut[u]=1;
            }
        }else if(pre[v]<pre[u]&&v!=fa)
        {
            lowu=min(lowu,pre[v]);
        }

    }
    if(child==1&&fa==-1)
        iscut[u]=0;
    return lowu;
}
void init(int n)
{
    memset(iscut,0,sizeof(iscut));
    memset(pre,0,sizeof(pre));
    memset(vis,0,sizeof(vis));
    dfs_clock=0;
    for(int i=1;i<=n+1;i++)
        g[i].clear();
}
int read(int n)
{
    int maxid=0;
    for(int i=1;i<=n;i++)
    {
        int u,v;
        scanf("%d%d",&u,&v);
        g[u].push_back(v);
        g[v].push_back(u);
        maxid=max(maxid,u,v);
    }
    return maxid;
}

void dfs2(int u,int &num,int &cut_cnt,int k)
{
    num++;
    vis[u]=1;
    for(int i=0;i<g[u].size();i++)
    {
        int v=g[u][i];
        if(!iscut[v]&&!vis[v])
            dfs2(v,num,cut_cnt,k);
        else if(iscut[v]&&vis[v]!=k)
        {
            cut_cnt++;
            vis[v]=k;
        }
    }
}
int main()
{
    int n;
    int ca=0;
    while(scanf("%d",&n)!=EOF&&n)
    {
        init(n);
        int m=read(n);
        dfs1(1,-1);
        long long ans1=0,ans2=1;
        int bcc_cnt=0;
        for(int i=1;i<=m;i++)
        {
            if(!iscut[i]&&!vis[i])
            {
                int num=0,cut_cnt=0;
                dfs2(i,num,cut_cnt,++bcc_cnt);
                if(cut_cnt==1)
                {
                    ans1++;
                    ans2*=(long long)num;
                }else if(cut_cnt==0)
                {
                    ans1=2;
                    ans2=(long long)num*(num-1)/2;
                }
            }
        }
        printf("Case %d: %lld %lld\n",++ca,ans1,ans2);
    }
    return 0;
}
View Code

 

posted on 2014-03-19 23:15  BMan、  阅读(296)  评论(0编辑  收藏  举报

导航