Cow Contest Floyd
来源:互联网 发布:编写吉他谱的软件 编辑:程序博客网 时间:2024/04/27 19:34
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 54 34 23 21 22 5
Sample Output
2
N头牛M场比赛结果,求能够确定排名的牛的数量,还真没想到和最短路径有啥联系。。。
Floyd求传递闭包,,,一开始我并不知道有这种操作。要想确定牛的排名,就得知道它与剩下的n-1头牛的胜负关系,所以这里我们不妨将胜看成入度,输看成出度,一头牛与其他牛的胜负关系即度数和为n-1,那么它的排名就确定了
#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>using namespace std;const int N = 110;int g[N][N];int n, m;void Floyd(){ for(int k = 1; k <= n; k++) for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) g[i][j] = g[i][j] || (g[i][k] && g[k][j]); //是否存在胜负关系,这种写法不是我自己想的。}int main(){ int u, v; memset(g, 0, sizeof(g)); scanf("%d%d", &n, &m); while(m--) { scanf("%d%d", &u, &v); g[u][v] = 1; } Floyd(); int deg, cnt = 0; for(int i = 1; i <= n; i++) { deg = 0; for(int j = 1; j <= n; j++) { if(g[i][j] || g[j][i]) //入度和出度即为胜负关系 deg++; } if(deg == n-1) //与剩下的n-1头牛的胜负关系确定 cnt++; } printf("%d", cnt); return 0;}
- [floyd]poj3660 Cow Contest
- POJ3660 Cow Contest(floyd)
- Cow Contest(Floyd)
- Cow Contest Floyd
- POJ3660 Cow Contest Floyd算法
- 最短路(floyd) Cow Contest
- poj 3660 Cow Contest (Floyd)
- poj 3660 Cow Contest floyd
- poj 3660 Cow Contest【floyd】
- POJ 3660 Cow Contest(Floyd)
- POJ - 3660 Cow Contest(Floyd)
- Cow Contest [最短路][floyd]
- poj3660-cow contest-Floyd算法
- POJ 3660 Cow Contest [Floyd]
- POJ3660 Cow Contest(floyd)
- poj 3660 Cow Contest (Floyd+思维)
- Cow Contest(floyd+传递闭包)
- 【POJ3660】Cow Contest Floyd传递闭包
- 图像处理之霍夫变换圆检测算法
- [caioj]1491: 基于连通性状态压缩的动态规划问题:Tony's Tour
- 排序(HDU)
- java在线支付---13.java在线支付所有源码
- MD5加密算法
- Cow Contest Floyd
- hbuilder+myeclipse开发解决hbuilder修改静态文件,要刷新myeclipse项目的问题
- chrome浏览器的跨域设置——包括版本49前后两种设置
- LeetCode 504. Base 7
- java中定义常量
- 异步加载数据和图片综合案例
- 解决手机浏览器中div append元素失败
- spring环境配置
- 使用mybatis-generator插件后的maven打包问题