题意:中文题
思路:判断是否为强连通图,跑一遍tarjan,然后看scc_cnt是否等于1即可
#include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <set>
#include <ctime>
#include <cmath>
#include <cctype>
#include <stack>
using namespace std;
#define maxn 10000+100
#define LL long long
int cas=1,T;
vector<int>G[maxn];
int pre[maxn];
int lowlink[maxn];
int sccno[maxn];
int dfs_clock,scc_cnt;
int n,m;
stack<int>S;
void dfs(int u)
{
pre[u]=lowlink[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);
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++;
for (;;)
{
int x = S.top();S.pop();
sccno[x] = scc_cnt;
if (x==u)
break;
}
}
}
void find_scc(int n)
{
dfs_clock=scc_cnt=0;
memset(sccno,0,sizeof(sccno));
memset(pre,0,sizeof(pre));
for (int i = 1;i<=n;i++)
if (!pre[i])
dfs(i);
}
int main()
{
while (scanf("%d%d",&n,&m)!=EOF && n)
{
for (int i = 1;i<=n;i++)
G[i].clear();
for (int i = 0;i<m;i++)
{
int u,v;
scanf("%d%d",&u,&v);
G[u].push_back(v);
}
find_scc(n);
if (scc_cnt==1)
puts("Yes");
else
puts("No");
}
//freopen("in","r",stdin);
//scanf("%d",&T);
//printf("time=%.3lf",(double)clock()/CLOCKS_PER_SEC);
return 0;
}