***强连通图概念:从任意一点都可以到达任一点***
#include<cstdio> #include<cstdlib> #include<cmath> #include<iostream> #include<algorithm> #include<cstring> #include<vector> using namespace std; #define N 10005 int n, m; int dfn[N], low[N]; int Stack[N], InStack[N]; int Time, top, cnt, bloks; vector<vector<int> > G; void Init() { G.clear(); G.resize(n+5); memset(dfn, 0, sizeof(dfn)); memset(low, 0, sizeof(low)); memset(Stack, 0, sizeof(Stack)); memset(InStack, 0, sizeof(InStack)); Time=top=cnt=bloks=0; } void Tarjan(int u) { dfn[u]=low[u]=++Time; Stack[top++]=u; InStack[u]=1; int len=G[u].size(), v; for(int i=0; i<len; i++) { v=G[u][i]; if(!dfn[v]) { Tarjan(v); low[u]=min(low[u], low[v]); } else if(InStack[v]) { low[u]=min(low[u], dfn[v]); } } if(dfn[u]==low[u]) { do { cnt++; v=Stack[--top]; InStack[v]=0; }while(u!=v); bloks++; } } int main() { while(scanf("%d%d", &n, &m), n+m) { Init(); int a, b; while(m--) { scanf("%d%d", &a, &b); G[a].push_back(b); } Tarjan(1); if(cnt==n&&bloks==1) printf("Yes "); else printf("No "); } return 0; }