小技巧:
- 邻接表中记录的是结点的进边,而不是出边。
- 用deleted数组记录是否当前结点是否被移除
- 用一个vector数组记录错误项,以此防止最后一个错误项后出现空格。
#include<cstdio>
#include<vector>
#define maxn 1010
using namespace std;
vector<int> origin[maxn];
bool deleted[maxn];
int N, M;
bool check_delete(int now) {
if (origin[now].size() == 0) {
deleted[now] = true;
return true;
}
for (int i = 0; i < origin[now].size(); i++) {
int temp = origin[now][i];
if (deleted[temp] == false) return false;
}
deleted[now] = true;
return true;
}
int main() {
vector<int> wrongone;
scanf("%d %d", &N, &M);
int s, e; // start, end
for (int i = 0; i < M; i++) {
scanf("%d %d", &s, &e);
origin[e].push_back(s);
}
int K;
scanf("%d", &K);
for (int i = 0; i < K; i++) {
for (int j = 0; j < N; j++) {
deleted[j] = false;
}
int testone[maxn];
for (int j = 0; j < N; j++) {
scanf("%d", &testone[j]);
}
for (int j = 0; j < N; j++) {
if (!check_delete(testone[j])) {
wrongone.push_back(i);
break;
}
}
}
for (int i = 0; i < wrongone.size(); i++) {
printf("%d", wrongone[i]);
if (i != wrongone.size() - 1) printf(" ");
else printf("\n");
}
return 0;
}