夏夜、

心若平似镜、何题不AC。

UVA 12167 Proving Equivalences 强连通分量

//#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=20005;
int pre[maxn],lowlink[maxn],sccno[maxn];
int dfs_clock,scc_cnt;

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

stack<int> s;

void dfs(int u,int fa)
{
    lowlink[u]=pre[u]=++dfs_clock;
    s.push(u);
    for(int i=0;i<g[u].size();i++)
    {
        int v=g[u][i];
        if(!pre[v])
        {
            dfs(v,u);
            lowlink[u]=min(lowlink[u],lowlink[v]);
        }else if(!sccno[v])
        {
            lowlink[u]=min(lowlink[u],pre[v]);
        }
    }
    if(lowlink[u]==pre[u])
    {
        scc_cnt++;
        scc[scc_cnt].clear();
        while(1)
        {
            int x=s.top();s.pop();
            //printf("%d\n",x);
            if(sccno[x]!=scc_cnt)
            {
                sccno[x]=scc_cnt;
                scc[scc_cnt].push_back(x);
            }
            if(x==u)break;
        }
    }
}

void tarjan(int n)
{
    memset(pre,0,sizeof(pre));
    memset(sccno,0,sizeof(sccno));
    memset(lowlink,0,sizeof(lowlink));
    dfs_clock=scc_cnt=0;
    for(int i=1;i<=n;i++)
        if(!pre[i])dfs(i,-1);
}


int in0[maxn],out0[maxn];
int main()
{
    int t;
    scanf("%d",&t);
    for(int ca=1;ca<=t;ca++)
    {
        int n,m;
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)g[i].clear();
        for(int i=1;i<=m;i++)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            g[u].push_back(v);
        }
        tarjan(n);

        memset(in0,0,sizeof(in0));
        memset(out0,0,sizeof(out0));

        for(int i=1;i<=n;i++)
        {
            for(int j=0;j<g[i].size();j++)
            {
                int u=i,v=g[i][j];
                if(sccno[u]!=sccno[v])
                {
                    out0[sccno[u]]=in0[sccno[v]]=1;
                }
            }
        }
        int a=0,b=0;
        for(int i=1;i<=scc_cnt;i++)
        {
            if(in0[i]==0)a++;
            if(out0[i]==0)b++;
        }
        int res=max(a,b);
        if(scc_cnt==1)
            res=0;
        printf("%d\n",res);
    }
    return 0;
}
View Code

 

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

导航