# Training 8 - F题

## Output

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

### Sample Output

Yes
No

#pragma warning (disable:4996)
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <cstring>
#include <string>
#include <cstdio>
#include <cmath>
#include <vector>
#define inf 0X3f3f3f3f
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N = 1e4 + 200;
const int M = 1e5 + 200;

int dfn[M], low[M];
int stack[M], ins[M], c[M];
vector<int> scc[M];
int n, m, num, tot, top, cnt;

{
++tot;
ver[tot] = y;
}

void tarjan(int x)
{
dfn[x] = low[x] = ++num;
stack[++top] = x;
ins[x] = 1;
for (int i = head[x]; i; i = nex[i])
{
if (!dfn[ver[i]])
{
tarjan(ver[i]);
low[x] = min(low[x], low[ver[i]]);
}
else if (ins[ver[i]])
low[x] = min(low[x], dfn[ver[i]]);
}
if (dfn[x] == low[x])
{
cnt++;
int y;
do
{
y = stack[top--];
ins[y] = 0;
c[y] = cnt;
scc[cnt].push_back(y);
} while (x != y);
}
}

int main()
{
while (scanf("%d%d", &n, &m) != EOF)
{
if (n == 0 && m == 0)
break;
memset(ver, 0, sizeof(ver));
memset(nex, 0, sizeof(nex));
memset(dfn, 0, sizeof(dfn));
memset(low, 0, sizeof(low));
memset(stack, 0, sizeof(stack));
memset(ins, 0, sizeof(ins));
memset(c, 0, sizeof(c));
num = tot = top = cnt = 0;
tot = cnt = 0;
for (int i = 1; i <= m; i++)
{
int u, v;
scanf("%d%d", &u, &v);
}
for (int i = 1; i <= n; i++)
if (!dfn[i])
tarjan(i);
if (cnt > 1)
printf("No\n");
else
printf("Yes\n");
for (int i = 1; i <= n; i++)
scc[i].clear();
}
return 0;
}


©️2019 CSDN 皮肤主题: 数字20 设计师: CSDN官方博客