题目描述
这是研究生入学考试中给出的一个问题:
以下哪个选项不是从给定的有向图中获得的拓扑序列?
现在,请你编写一个程序来测试每个选项。
输入格式
第一行包含两个整数 N和M,分别表示有向图的点和边的数量。
接下来 M 行,每行给出一条边的起点和终点。
点的编号从 1 到 N。
再一行包含一个整数 K,表示询问次数。
接下来 K 行,每行包含一个所有点的排列。
一行中的数字用空格隔开。
输出格式
在一行中输出所有不是拓扑序列的询问序列的编号。
询问序列编号从 0 开始。
保证存在至少一个解。
6 8
1 2
1 3
5 2
5 4
2 3
2 6
3 4
6 4
5
1 5 2 3 6 4
5 1 2 6 3 4
5 1 2 3 6 4
5 2 1 6 3 4
1 2 3 4 5 6
输出样例:
3 4
代码:
#include <iostream>
using namespace std;
#define N 100005
int n, m;
int cnt[N];
struct Edge{
int a, b;
Edge(){};
Edge(int _a,int _b){
a = _a,b = _b;
}
bool operator < (const Edge & A){
return b < A.b;
}
}a[N];
int main(){
cin >> n >> m;
for(int i = 0; i < m; i++) cin >> a[i].a >> a[i].b;
int k;
cin >> k;
bool t = true;
for(int i = 0; i < k; i++){
for(int j = 1; j <= n; j++){
int x;
cin >> x;
cnt[x] = j;
}
bool tmp = true;
for(int j = 0; j < m; j++){
int x = a[j].a,y = a[j].b;
if(cnt[x] > cnt[y]){
tmp = false;
break;
}
}
if(!tmp){
if(t) t = false;
else cout << " ";
cout << i;
}
}
cout << endl;
return 0;
}