二分图染色。
#include <cstdio>
#include <cstring>
#include <cctype>
#include <vector>
using namespace std;
const int maxn = 105;
vector<int> G[maxn];
vector<int> U;
int mark[maxn];
void dfs(int v) {
if (mark[v] == 0) {
U.push_back(v);
}
for (int i = 0; i < G[v].size(); i++) {
int u = G[v][i];
if (mark[u] == -1) {
mark[u] = (mark[v] ^ 1);
dfs(u);
}
}
}
int main(int argc, char const *argv[]) {
int n;
scanf("%d", &n);
getchar();
for (int v = 1; v <= n; v++) {
int u;
while (scanf("%d", &u) == 1 && u) {
G[v].push_back(u);
}
}
memset(mark, -1, sizeof(mark));
for (int v = 1; v <= n; v++) {
if (mark[v] == -1) {
mark[v] = 1;
dfs(v);
}
}
printf("%lu\n", U.size());
for (int i = 0; i < U.size(); i++) {
printf("%d%c", U[i], i < U.size() - 1 ? ' ' : '\n');
}
return 0;
}