原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1269
强联通分量裸题。。
Tarjan求强联通分量参见:https://www.byvoid.com/blog/scc-tarjan/
#include<bits/stdc++.h>
using namespace std;
const int N = 10100;
struct Tarjan_Scc {
stack<int>s;
bool instack[N];
struct edge { int to, next; }G[N * 10];
int scc, idx, tot, dfn[N], low[N], head[N];
inline void init(int n) {
scc = tot = idx = 0;
while (!s.empty()) s.pop();
for (int i = 0; i < n + 2; i++) {
head[i] = -1;
instack[i] = false;
dfn[i] = low[i] = 0;
}
}
inline void add_edge(int u, int v) {
G[tot].to = v, G[tot].next = head[u], head[u] = tot++;
}
inline void built(int m) {
int u, v;
while (m--) {
scanf("%d %d", &u, &v);
add_edge(u, v);
}
}
inline void tarjan(int u) {
dfn[u] = low[u] = ++idx;
s.push(u);
instack[u] = true;
for (int i = head[u]; ~i; i = G[i].next) {
int &v = G[i].to;
if (!dfn[v]) {
tarjan(v);
low[u] = min(low[u], low[v]);
} else if (instack[v] && dfn[v] < low[u]) {
low[u] = dfn[v];
}
}
if (low[u] == dfn[u]) {
scc++;
int v = 0;
do {
v = s.top(); s.pop();
instack[v] = false;
} while (v != u);
}
}
inline void solve(int n, int m) {
init(n);
built(m);
for (int i = 1; i <= n; i++) {
if (!dfn[i]) tarjan(i);
}
puts(scc == 1 ? "Yes" : "No");
}
}go;
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w+", stdout);
#endif
int n, m;
while (~scanf("%d %d", &n, &m), m + n) {
go.solve(n, m);
}
return 0;
}