您的位置:首页 > 其它

poj 2553 The Bottom of a Graph

2012-03-02 13:55 295 查看
#include<iostream>
#include<vector>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<stack>
#include<string>
#include<map>
#include<set>
#include<cmath>
#include<cassert>
#include<cstring>
#include<iomanip>
using namespace std;

#ifdef _WIN32
#define i64 __int64
#define out64 "%I64d\n"
#define in64 "%I64d"
#else
#define i64 long long
#define out64 "%lld\n"
#define in64 "%lld"
#endif

#define FOR(i,a,b)      for( int i = (a) ; i <= (b) ; i ++)
#define FF(i,a)         for( int i = 0 ; i < (a) ; i ++)
#define FFD(i,a)        for( int i = (a)-1 ; i >= 0 ; i --)
#define S64(a)          scanf(in64,&a)
#define SS(a)           scanf("%d",&a)
#define LL(a)           ((a)<<1)
#define RR(a)           (((a)<<1)+1)
#define SZ(a)           ((int)a.size())
#define PP(n,m,a)		puts("---");FF(i,n){FF(j,m)cout << a[i][j] << ' ';puts("");}
#define pb              push_back
#define CL(Q)           while(!Q.empty())Q.pop()
#define MM(name,what)   memset(name,what,sizeof(name))
#define read            freopen("in.txt","r",stdin)
#define write           freopen("out.txt","w",stdout)

const int inf = 0x3f3f3f3f;
const i64 inf64 = 0x3f3f3f3f3f3f3f3fLL;
const double oo = 10e9;
const double eps = 10e-10;
const double pi = acos(-1.0);
const int maxn = 5100;

struct zz
{
    int from;
    int to;
}zx;

int n,m;
vector<int>g[maxn];
vector<int>tv[maxn];
vector<int>v;
vector<int>fv;
vector<int>s;
int dfn[maxn];
int low[maxn];
int ins[maxn];
int be[maxn];
int df,nb;

void tarjan(int now)
{
    int to;
    low[now] = dfn[now] = df++;
    s.pb(now);
    ins[now] = true;
    for(int i=0;i<g[now].size();i++)
    {
        to = g[now][i];
        if(!dfn[to])
        {
            tarjan(to);
            low[now] = min(low[to],low[now]);
        }
        else if(ins[to])
        {
            low[now] = min(low[to],low[now]);
        }
    }
    if(dfn[now]==low[now])
    {
        while(s.back() != now)
        {
            to = s.back();
            be[to] = nb;
            ins[to] = false;
            s.pop_back();
        }
        to = s.back();
        ins[to] = false;
        be[to] = nb++;
        s.pop_back();
    }
    return ;
}

bool find(int x)
{
    FF(i,v.size())
    {
        if(be[x] == v[i])
        {
            return true;
        }
    }
    return false;
}

void start()
{
    s.clear();
    nb = df = 1;
    MM(ins,0);
    MM(low,0);
    MM(dfn,0);
    MM(be,0);
    for(int i=1;i<=n;i++)
    {
        if(!dfn[i])
        {
            tarjan(i);
        }
    }
    nb--;
    int from,to;
    FOR(i,1,n)
    {
        from = be[i];
        FF(j,g[i].size())
        {
            to = be[g[i][j]];
            if(from != to)
            {
                tv[from].pb(to);
            }
        }
    }
    FOR(i,1,nb)
    {
        if(tv[i].empty())
        {
            v.pb(i);
        }
    }
    FOR(i,1,n)
    {
        if(find(i))
        {
            fv.pb(i);
        }
    }
    sort(fv.begin(),fv.end());
    return ;
}

int main()
{
    while(cin>>n)
    {
        if(!n)
        {
            break;
        }
        for(int i=1;i<=n;i++)
        {
            g[i].clear();
            tv[i].clear();
        }
        v.clear();
        fv.clear();
        cin>>m;
        int now,to;
        for(int i=1;i<=m;i++)
        {
            cin>>now>>to;
            g[now].pb(to);
        }
        start();
     /*   for(int i=1;i<=n;i++)
        {
            cout<<i<<" | "<<be[i]<<endl;
        }  */
        if(fv.empty())
        {
            cout<<endl;
            continue;
        }
        else
        {
            cout<<fv[0];
            FOR(i,1,fv.size()-1)
            {
                cout<<" "<<fv[i];
            }
            cout<<endl;
        }
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: