【C】拓扑排序

问题 B: 确定比赛名次

时间限制: 1 Sec   内存限制: 32 MB
提交: 61   解决: 39
[ 提交][ 状态][ TK题库][命题人: ]

题目描述

有N个比赛队(1<=N<=500),编号依次为1,2,3,。。。。,N进行比赛,比赛结束后,裁判委员会要将所有参赛队伍从前往后依次排名,但现在裁判委员会不能直接获得每个队的比赛成绩,只知道每场比赛的结果,即P1赢P2,用P1,P2表示,排名时P1在P2之前。现在请你编程序确定排名。

输入

输入有若干组,每组中的第一行为二个数N(1<=N<=500),M;其中N表示队伍的个数,M表示接着有M行的输入数据。接下来的M行数据中,每行也有两个整数P1,P2表示即P1队赢了P2队。

输出


给出一个符合要求的排名。输出时队伍号之间有空格,最后一名后面没有空格。
其他说明:符合条件的排名可能不是唯一的,此时要求输出时编号小的队伍在前;输入数据保证是正确的,即输入数据确保一定能有一个符合要求的排名。


样例输入

3 2
3 1
3 2
17 16
16 1
13 2
7 3
12 4
12 5
17 6
10 7
11 8
11 9
16 10
13 11
15 12
15 13
17 14
17 15
17 16
0 0

样例输出

3 1 2
17 6 14 15 12 4 5 13 2 11 8 9 16 1 10 7 3

#include<stdio.h>
#include<queue>
#include<iostream>
#include<vector>
using namespace std;
const int maxn=510;

int n,m,inDegree[maxn]={0};
void topologicalSort(vector<int> G[]){
	int num=0;
	int i;
	priority_queue<int,vector<int>,greater<int> > q;
	for(i=1;i<=n;i++){
		if(inDegree[i]==0){
			q.push(i);
		}
	}
	while(!q.empty()){
		int t=q.top();
		q.pop();num++;
		if(num==n) printf("%d\n",t);
		else printf("%d ",t);
		for(i=0;i<G[t].size();i++){
			inDegree[G[t][i]]--;
			if(inDegree[G[t][i]]==0) q.push(G[t][i]);
		}
	}
}
int main(){
	cin>>n>>m;
	int i;
	while(n!=0){
		vector<int> G[maxn];
		if(m==0) printf("%d\n",n);
		else{
			for(i=0;i<m;i++){
				int a,b;
				cin>>a>>b;
				G[a].push_back(b);
				inDegree[b]++;
			}
			topologicalSort(G);
		}
		cin>>n>>m;
	}
	return 0;
}

问题 C: Legal or Not

时间限制: 1 Sec   内存限制: 32 MB
提交: 50   解决: 21
[ 提交][ 状态][ TK题库][命题人: ]

题目描述

ACM-DIY is a large QQ group where many excellent acmers get together. It is so harmonious that just like a big family. Every day,many "holy cows" like HH, hh, AC, ZT, lcc, BF, Qinz and so on chat on-line to exchange their ideas. When someone has questions, many warm-hearted cows like Lost will come to help. Then the one being helped will call Lost "master", and Lost will have a nice "prentice". By and by, there are many pairs of "master and prentice". But then problem occurs: there are too many masters and too many prentices, how can we know whether it is legal or not?We all know a master can have many prentices and a prentice may have a lot of masters too, it's legal. Nevertheless,some cows are not so honest, they hold illegal relationship. Take HH and 3xian for instant, HH is 3xian's master and, at the same time, 3xian is HH's master,which is quite illegal! To avoid this,please help us to judge whether their relationship is legal or not. Please note that the "master and prentice" relation is transitive. It means that if A is B's master ans B is C's master, then A is C's master.

输入

The input consists of several test cases. For each case, the first line contains two integers, N (members to be tested) and M (relationships to be tested)(2 <= N, M <= 100). Then M lines follow, each contains a pair of (x, y) which means x is y's master and y is x's prentice. The input is terminated by N = 0.TO MAKE IT SIMPLE, we give every one a number (0, 1, 2,..., N-1). We use their numbers instead of their names.

输出

For each test case, print in one line the judgement of the messy relationship.If it is legal, output "YES", otherwise "NO".

样例输入

4 3
0 1
1 2
2 3
3 3
0 1
1 2
2 0
0 1

样例输出

YES
NO

#include<queue>
#include<iostream>
#include<stdio.h>
#include<vector>
using namespace std;
int n,m;
bool topological(vector<int> G[],int inDegree[]){
	queue<int> q;
	int i;
	int num=0;
	for(i=0;i<n;i++){
		if(inDegree[i]==0) q.push(i);
	}
	while(!q.empty()){
		int t=q.front();
		q.pop();num++;
		for(i=0;i<G[t].size();i++){
			int v=G[t][i];
			inDegree[v]--;
			if(inDegree[v]==0){
				q.push(v);
			}
		}
		if(num>n) return false;
	}
	if(num==n) return true;
	else return false;
}
int main(){
	int i;
	cin>>n>>m;
	while(n!=0){
		vector<int> G[110];
		int inDegree[110]={0};
		for(i=0;i<m;i++){
			int a,b;
			cin>>a>>b;
			G[a].push_back(b);
			inDegree[b]++;
		}
		if(topological(G,inDegree)) cout<<"YES"<<endl;
		else cout<<"NO"<<endl;
		cin>>n>>m;
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/li_jiaqian/article/details/79461832
今日推荐