街道赛跑 题解
题目在主页,如有出错请指出
include <bits/stdc++.h>
using namespace std;
int ans[55], ans2[55];
int vis[55], vis2[55];
vector < int > a[55];
void dfs(int x)
{
vis[x] = 1;
for (auto i : a[x])
if (vis[i] == 0) dfs(i);
}
void dfs2(int x)
{
vis2[x] = 1;
for (auto i : a[x])
if (vis2[i] == 0) dfs2(i);
}
int main()
{
int x, cnt = 0;
while (cin >> x)
{
if (x == -1) break;
if (x == -2)
{
cnt++;
continue;
}
a[cnt].push_back(x);
}
int n = --cnt;
cnt = 0;
for (int i = 1; i <= n - 1; i++)
{
memset(vis, 0, sizeof vis);
vis[i] = -1;
dfs(0);
if (vis[n] == 0) ans[++cnt] = i;
}
cout << cnt;
for (int i = 1; i <= cnt; i++)
cout << " " << ans[i];
cout << endl;
int total = 0;
for (int i = 1; i <= cnt; i++)
{
memset(vis, 0, sizeof vis);
vis[ans[i]] = -1;
dfs(0);
memset(vis2, 0, sizeof vis2);
dfs2(ans[i]);
bool flag = 1;
for (int j = 0; j <= n; j++)
{
if (vis[j] == 1 && vis2[j] == 1 )
{
flag = 0;
break;
}
}
if (flag) ans2[++total] = ans[i];
}
cout << total;
for (int i = 1; i <= total; i++)
cout << " " << ans2[i];
cout << endl;
return 0;
}