USACO Street Race

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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
ID: wcr19961
PROG: race3
LANG: C++11
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
const int maxn = 55;
vector<int> g[maxn];
int vis[maxn];
int n, num1, num2;
int ans1[maxn], ans2[maxn];
bool bfs(int u) {
queue<int> q;
q.push(0);
vis[0] = 1;
while (!q.empty()) {
int k = q.front();
q.pop();
if (k == n) {
return false;
}
for (int i = 0; i < g[k].size(); ++i) {
int x = g[k][i];
if (x != u && vis[x] != 1) {
vis[x] = 1;
q.push(x);
}
}
}
return true;
}
bool bfs2(int u) {
queue<int> q;
q.push(u);
vis[u] = 2;
while (!q.empty()) {
int k = q.front();
q.pop();
for (int i = 0; i < g[k].size(); ++i) {
int x = g[k][i];
if (vis[x] == 1) {
return false;
}
if (vis[x] != 2) {
vis[x] = 2;
q.push(x);
}
}
}
return true;
}
int main() {
freopen("race3.in", "r", stdin);
freopen("race3.out", "w", stdout);
for (; ; ++n) {
int x;
while (~scanf("%d", &x)) {
if (x < 0) {
break;
}
g[n].push_back(x);
}
if (x == -1) {
break;
}
}
--n;
for (int i = 1; i < n; ++i) {
memset(vis, 0, sizeof vis);
if (bfs(i)) {
ans1[num1++] = i;
if (bfs2(i)) {
ans2[num2++] = i;
}
}
}
printf("%d", num1);
for (int i = 0; i < num1; ++i) {
printf(" %d", ans1[i]);
}
puts("");
printf("%d", num2);
for (int i = 0; i < num2; ++i) {
printf(" %d", ans2[i]);
}
puts("");
return 0;
}

支付宝扫码领红包