UVA 10765 Doves and bombs 双连通分量

//#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!='
')return ch;
    }
    return EOF;
}

const int maxn=10100;

struct result
{
    int id,v;
    bool operator < (const result &ant ) const
    {
        if(v!=ant.v)return v>ant.v;
        else return id<ant.id;
    }
}res[maxn];
struct Edge
{
    int u,v;
};
int pre[maxn],bccno[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])
        {
            child++;
            s.push(e);
            int lowv=dfs(v,u);
            lowu=min(lowu,lowv);
            if(lowv>=pre[u])
            {
                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);
        }
    }
    return lowu;
}
void find_bcc(int n)
{
    memset(bccno,0,sizeof(bccno));
    memset(pre,0,sizeof(pre));
    dfs_clock=bcc_cnt=0;
    for(int i=0;i<n;i++)
        dfs(i,-1);
}
int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF&&(n||m))
    {
        for(int i=0;i<n;i++)
            g[i].clear();
        while(1)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            if(u==-1)break;
            g[u].push_back(v);
            g[v].push_back(u);
        }
        find_bcc(n);
        for(int i=0;i<n;i++)
        {
            res[i]=(result){i,0};
        }
        for(int i=1;i<=bcc_cnt;i++)
        {
            for(int j=0;j<bcc[i].size();j++)
            {
                res[bcc[i][j]].v++;
            }
        }
        sort(res,res+n);
        for(int i=0;i<m;i++)
            printf("%d %d
",res[i].id,res[i].v);
        printf("
");
    }
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/BMan/p/3619311.html