第一道强联通的题目纪念一下! 主要是模版 tarjan算法
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
#include <cmath>
#include <stack>
#include <cstring>
using namespace std;
#define INF 0xfffffff
#define maxn 10005
#define min(a,b) (a<b?a:b) int n, m, time;
vector<int> G[maxn];
stack<int> S;
bool inStack[maxn];
int low[maxn], dfn[maxn]; void tarjan(int u)
{
int i, len = G[u].size(), v;
low[u] = dfn[u] = time ++;
inStack[u] = true;
S.push(u);
for(i=; i<len; i++)
{
int v = G[u][i]; if( !low[v] )
{
tarjan(v);
low[u] = min(low[u], low[v]);
}
else if(inStack[v])
{
low[u] = min(low[u], dfn[v]);
}
}
do
{
v = S.top();
S.pop();
inStack[v] = false;
}while(u != v);
} void init()
{
int i;
memset(low, , sizeof(low));
time = ;
for(i=; i<=n; i++)
G[i].clear();
} bool canSolve()
{
int i;
for(i=; i<=n; i++)
{
if(low[i] != )
return false;
}
return true;
} int main()
{
int i;
while(scanf("%d%d",&n,&m), m+n )
{
int a, b;
init();
for(i=; i<m; i++)
{
scanf("%d%d",&a,&b);
G[a].push_back(b);
} tarjan(); if( canSolve() )
printf("Yes\n");
else
printf("No\n");
}
return ;
}