您的位置:首页 > 其它

POJ3660 Cow Contest(Floyd求传递闭包)

2017-04-09 18:35 423 查看
Floyd 多源最短路 && 传递闭包 && 最小环 :
http://blog.csdn.net/wzw1376124061/article/details/69870161
                                                                                  Cow Contest


Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 11170 Accepted: 6203
Description

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 cowA has a greater skill level than cow
B (1 ≤ A ≤ N; 1 ≤B ≤ N; A ≠
B), then cow A will always beat cowB.

Farmer John is trying to rank the cows by skill level. Given a list the results ofM (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

 

Sample Input
5 5
4 3
4 2
3 2
1 2
2 5

Sample Output

2


题目大意:

有n只奶牛,有n个连续的实力,如果u的实力大于v的实力,就能打赢它,

然后给定m种关系,求最后能确定其排名的奶牛个数。

解题思路:

有N头牛,每个牛有一个唯一且不同的能力等级值.然后他们中的两头牛进行M场比赛,并给你这M场的比赛结果.现在的问题是问你有多少头牛可以确定自己的排名了?
如果对于a胜b且b胜c,那么肯定a胜c. 且如果已经知道了a胜的牛数目+比a厉害的牛数目正好==N-1,那么a的排名也肯定可以推出来.

/*************************************************************************
> Author: wzw-cnyali
> Created Time: 2017/4/9 18:17:48
************************************************************************/

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>

using namespace std;

typedef long long LL;

typedef unsigned long long uLL;

#define REP(i, a, b) for(register int i = (a), i##_end_ = (b); i <= i##_end_; ++ i)
#define DREP(i, a, b) for(register int i = (a), i##_end_ = (b); i >= i##_end_; -- i)
#define EREP(i, a) for(register int i = (be[a]); i != -1; i = nxt[i])
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define mem(a, b) memset((a), b, sizeof(a))

template<typename T> inline bool chkmin(T &a, const T &b) { return a > b ? a = b, 1 : 0; }
template<typename T> inline bool chkmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }

template <class T>
T read(T sum = 0, T fg = 0)
{
char c = getchar();
while(c < '0' || c > '9') { fg |= c == '-'; c = getchar(); }
while(c >= '0' && c <= '9') { sum = sum * 10 + c - '0'; c = getchar(); }
return fg ? -sum : sum;
}

const int inf = 0x3f3f3f3f;

const int Size = 1000;

const int maxn = 100000;

const int maxm = 100000;

bool map[Size][Size];

int n, m;
void floyd()
{
REP(k, 1, n) REP(i, 1, n) REP(j, 1, n)
map[i][j] = (map[i][j] || (map[i][k] & map[k][j]));
}

int main()
{
#ifndef ONLINE_JUDGE
freopen("input.in", "r", stdin);
freopen("output.out", "w", stdout);
#endif
while(scanf("%d %d", &n, &m) == 2)
{
REP(i, 1, m) map[read<int>()][read<int>()] = 1;
floyd();
int ans = 0;
REP(i, 1, n)
{
int cnt = 0;
REP(j, 1, n) cnt += map[i][j] + map[j][i];
if(cnt == n - 1) ans++;
}
printf("%d\n", ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: