pat1146 Topological Order (拓扑排序)

 

题目链接:click here

思路:拓扑排序弱化版,甚至连拓扑序列都不用求,话说和拓扑排序相关的题目都很水啊。

 

 

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
 
using namespace std;
 
const int MAXN = 1005;
const int INF = INT_MAX;

int indegree[MAXN], indegreemirror[MAXN], N;
vector graph[MAXN];

bool IsTuoPuSeq(vector seq){
	bool flag = true;
	for(int i = 0; i < seq.size(); i++){
		int from = seq[i], to;
		if(indegreemirror[from] != 0) flag = false;
		for(int j = 0; j < graph[from].size(); j++){
			to = graph[from][j];
			indegreemirror[to]--;
		}
	}
	return flag;
}

void Initial(){
	for(int i = 1; i <= N; i++){
		graph[i].clear();
		indegree[i] = 0;
	}
}

int main(){
//	freopen("in.txt", "r", stdin);
	int M, K;
	while(~scanf("%d %d", &N, &M)){
		Initial();
		int from, to, point;
		for(int i = 0; i < M; i++){
			scanf("%d %d", &from, &to);
			graph[from].push_back(to);
			indegree[to]++;
		}
		scanf("%d", &K);
		vector ans;
		for(int i = 0; i < K; i++){
			for(int j = 1; j <= N; j++){
				indegreemirror[j] = indegree[j];
			}
			vector seq;
			for(int j = 0; j < N; j++){
				scanf("%d", &point);
				seq.push_back(point);
			}
			if(!IsTuoPuSeq(seq)) ans.push_back(i);
		}
		bool flag = false;
		for(int i = 0; i < ans.size(); i++){
			if(flag) printf(" ");
			flag = true;
			printf("%d", ans[i]);
		}
		printf("\n");
	}
	return 0;
}

 

你可能感兴趣的:(图论-拓扑排序,其他oj)