3A: cow contest

题目来源
N (1 ≤ N ≤ 100) cows, conveniently numbered 1…N, are participating in a programming contest. As we all know, some cows code better than others. Each cow has a certain constant skill rating that is unique among the competitors.
The contest is conducted in several head-to-head rounds, each between two cows. If cow A has a greater skill level than cow B (1 ≤ A ≤ N; 1 ≤ B ≤ N; A ≠ B), then cow A will always beat cow B.
Farmer John is trying to rank the cows by skill level. Given a list the results of M (1 ≤ M ≤ 4,500) two-cow rounds, determine the number of cows whose ranks can be precisely determined from the results. It is guaranteed that the results of the rounds will not be contradictory.

Input

  • Line 1: Two space-separated integers: N and M
  • Lines 2…M+1: Each line contains two space-separated integers that describe the competitors and results (the first integer, A, is the winner) of a single round of competition: A and B

Output

  • Line 1: A single integer representing the number of cows whose ranks can be determined

题意

N(1≤N≤100)头母牛,方便地编号1…N,正在参加编程竞赛。众所周知,有些母牛的编码要好于其他。每头母牛都有一定的恒定技能等级,这在竞争者中是独一无二的。比赛分几个头对头进行,每回合在两头母牛之间进行。如果母牛A的技能水平高于母牛B(1≤A≤N; 1≤B≤N; A≠B),那么母牛A总是会击败母牛B。农夫约翰试图按技能水平对母牛进行排名。给出一个清单,列出两轮M(1≤M≤4,500)M的结果,确定可以从结果精确确定其等级的母牛的数量。可以保证回合的结果不会矛盾。
输入项
第1行:两个以空格分隔的整数:N和M
第2…M + 1行:每行包含两个以空格分隔的整数,它们描述单个竞赛回合的竞争者和结果(第一个整数A是获胜者):A和B
输出量

第1行:一个整数,代表可以确定其等级的母牛的数量

解法

n个人打架, 只要确定他打不过的人和打得过的人加起来等于n-1, 就可以知道他打架的能力怎么样, 也就是这个人的入度和出度加起来等于n-1即可(即有n-1个人可以直接或间接的相连)(floyd真香

代码

#include <iostream>
#include <cstring>
using namespace std;

bool map[105][105];

void floyd(int n){
	for(int k=1; k<=n; ++k){
		for(int i=1; i<=n; ++i){
			for(int j=1; j<=n; ++j){
				if(map[i][k] && map[k][j])
					map[i][j] = 1;
			}
		}
	}
}

int main(){
	int n, m;
	cin >> n >> m;
	
	int a, b;
	for(int i=0; i<m; ++i){
		cin >> a >> b;
		map[a][b] = 1;
	} 
	floyd(n);
	
	int ans = 0;
	for(int i=1; i<=n; ++i){
		int sum = 0;
		for(int j=1; j<=n; ++j){
			if(map[i][j] || map[j][i]) sum++;
		}
		if(sum==n-1) ans++;
	}
	cout << ans << endl;
	
	return 0;
}
发布了14 篇原创文章 · 获赞 0 · 访问量 144

猜你喜欢

转载自blog.csdn.net/loaf_/article/details/104010820