PAT 1146. 拓扑顺序
原题链接
简单
作者:
YAX_AC
,
2024-11-18 09:38:09
,
所有人可见
,
阅读 2
// Topological Order拓扑顺序 directed graph有向图
//there must no extra space at the beginning or the end of the line.
//行首和行尾不得有多余空格
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const int N = 1010,M = 10010;
int n,m;
struct Edge
{
int a,b;
}e[M];
int p[N];
int main()
{
cin>>n>>m;
for(int i = 0; i<m; i++) cin>>e[i].a>>e[i].b;
int k;
cin>>k;
bool is_first = true;
for(int i = 0; i<k; i++)
{
for(int j = 1; j<=n; j++)
{
int x;
cin>>x;
p[x] = j;
}
bool success = true;
for(int j = 0; j<m; j++)
if(p[e[j].a] > p[e[j].b])
{
success = false;
break;
}
if(!success)
{
if(is_first) is_first = false;
else cout<<' ';
cout<<i;
}
}
cout<<endl;
return 0;
}