Blog of RuSun

\begin {array}{c} \mathfrak {One Problem Is Difficult} \\\\ \mathfrak {Because You Don't Know} \\\\ \mathfrak {Why It Is Diffucult} \end {array}

CF915D Almost Acyclic Graph

LuoGu: CF915D Almost Acyclic Graph

CF: D. Almost Acyclic Graph

判断环可以用拓扑排序。

考虑删除边对判断环的影响,对一个点入度减少一,使得其可能可以入队。注意到 $n$ 很小,直接枚举该点即可。

查看代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <cstdio>
#include <vector>
#include <queue>
using namespace std;
template <class Type>
void read (Type &x)
{
char c;
bool flag = false;
while ((c = getchar()) < '0' || c > '9')
flag |= c == '-';
x = c - '0';
while ((c = getchar()) >= '0' && c <= '9')
x = (x << 3) + (x << 1) + c - '0';
if (flag) x = ~x + 1;
}
template <class Type, class ...Rest>
void read (Type &x, Rest &...y) { read(x); read(y...); }
template <class Type>
void write (Type x)
{
if (x < 0) putchar('-'), x = ~x + 1;
if (x > 9) write(x / 10);
putchar('0' + x % 10);
}
const int N = 510;
vector <int> g[N];
int n, m, d[N], td[N];
int main ()
{
read(n, m);
for (int i = 1, a, b; i <= m; ++i)
{
read(a, b);
++td[b];
g[a].push_back(b);
}
for (int i = 1; i <= n; ++i)
{
for (int j = 1; j <= n; ++j) d[j] = td[j];
--d[i];
int cnt = 0;
queue <int> q;
for (int j = 1; j <= n; ++j)
if (!d[j]) q.push(j);
while (!q.empty())
{
int t = q.front(); q.pop();
++cnt;
for (int j : g[t])
if (!--d[j]) q.push(j);
}
if (cnt == n) return puts("YES"), 0;
}
puts("NO");
return 0;
}