Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %I64d & %I64u

Description

Input

Output

Sample Input

3 3
1 2
2 3
3 1
3 3
1 2
2 3
3 2
0 0

Sample Output

Yes
No

Source

Tarjan求强连通分量并判断个数是否为1，即判断所给图是否为强连通图。注意n=1, m=0是成立的，判断输入结束那里WA了一发。。。
AC代码：
#include <iostream>
#include <cstring>
#include <stack>
#include <algorithm>
using namespace std;
int n, m;
const int maxn = 10010, maxm = 100010;
struct node
{
int v, next;
}e[maxm];
{
e[k].v = v;
}
void input()
{
k = 1;
for (int i = 0; i < m; i++)
{
int a, b;
cin >> a >> b;
}
}
int dfn[maxn], low[maxn], belong[maxn];
int cnt, idx;
bool ins[maxn];
stack <int> s;
void tarjan(int u)
{
int v;
low[u] = dfn[u] = ++idx;
s.push(u);
ins[u] = 1;
for (int i = head[u]; i != -1; i = e[i].next)
{
v = e[i].v;
if (!dfn[v])
{
tarjan(v);
low[u] = min(low[u], low[v]);
}
else if (ins[v])
low[u] = min(low[u], dfn[v]);
}
if (low[u] == dfn[u])
{
cnt++;
while (u != v)
{
v = s.top();
s.pop();
ins[v] = 0;
belong[v] = cnt;
}
}
}
void solve()
{
memset(dfn, 0, sizeof(dfn));
memset(ins, 0, sizeof(ins));
cnt = idx = 0;
for (int i = 1; i <= n; i++)
if (!dfn[i])
tarjan(i);
if (cnt == 1)
cout << "Yes\n";
else
cout << "No\n";
}
int main()
{
while (cin >> n >> m && (n || m))
{
input();
solve();
}
return 0;
}