hdu1269强连通水题 如果让我说:我只能说,实力决定一切

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<string.h>
#include<vector>
#include<stack>
#define maxn 11111
using namespace std;
int dfn[maxn],low[maxn],vis[maxn],belong[maxn],ind[maxn],outd[maxn];
int sum,tem;
vector<int>gra[maxn];
stack<int>S;
int MIN(int a,int b)
{
    return a>b?b:a;
}
void tarjan(int pox)
{
    vis[pox]=2;
    dfn[pox]=low[pox]=++sum;
    S.push(pox);
    for(int i=0; i<gra[pox].size(); i++)
    {
        int t=gra[pox][i];
        if(!dfn[t])
        {
            tarjan(t);
            low[pox]=MIN(low[pox],low[t]);
        }
        else if(vis[t]==2)
        {
            low[pox]=MIN(low[pox],dfn[t]);
        }
    }
    if(dfn[pox]==low[pox])
    {
        tem++;
        while(!S.empty())
        {
            int gh=S.top();
            S.pop();
            if(gh==pox)
                break;
        }
    }
}
int main()
{
    int N,M;
    int a,b;
    while(scanf("%d%d",&N,&M),N+M)
    {
        tem=sum=0;
        memset(vis,0,sizeof(vis));
        memset(dfn,0,sizeof(dfn));
        memset(low,0,sizeof(low));
        for(int i=0; i<maxn; i++)
            gra[i].clear();
        while(!S.empty())
            S.pop();
        for(int i=0; i<M; i++)
        {
            scanf("%d%d",&a,&b);
            gra[a].push_back(b);
        }
        for(int i=1; i<=N; i++)
            if(!dfn[i])
                tarjan(i);
        if(tem==1)
            printf("Yes\n");
        else
            printf("No\n");
    }
    return 0;
}

你可能感兴趣的:(hdu1269强连通水题 如果让我说:我只能说,实力决定一切)