AcWing 1639. 拓扑顺序
原题链接
简单
作者:
fwwww
,
2021-03-30 00:41:10
,
所有人可见
,
阅读 454
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
using namespace std;
int n, m, k;
const int N = 1010, M = 10010;
int dd[N], d[N], h[M], ne[M], e[M], top[N], idx;
int cnt = 1;
void add(int a, int b){
e[idx] = b, ne[idx] = h[a], h[a] = idx ++;
}
bool topsort(){
queue<int> q;
for(int i = 1; i <= n; i ++){
q.push(top[i]);
}
while(q.size()){
auto t = q.front();
q.pop();
if(d[t] != 0) return false;
for(int i = h[t]; ~i; i = ne[i]){
int j = e[i];
d[j] --;
}
}
return true;
}
int main(){
cin >> n >> m;
memset(h, -1, sizeof h);
while(m --){
int a, b;
cin >> a >> b;
add(a, b);
dd[b] ++;
}
cin >> k;
for(int j = 0; j < k; j ++){
memset(top, 0, sizeof top);
for(int i = 0; i < N; i ++){
d[i] = dd[i];
}
for(int i = 1; i <= n; i ++) cin >> top[i];
if(!topsort()) cout << j << ' ';
}
}