POJ3660 Cow Contest【Floyd演算法 傳遞閉包】
Cow Contest
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 16316 | Accepted: 9126 |
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 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
Sample Input
5 5
4 3
4 2
3 2
1 2
2 5
Sample Output
2
Source
問題連結:POJ3660 Cow Contest
問題描述:有n個奶牛(從1開始編號)進行比賽,有m個結果,一個結果用a b描述,表示a奶牛打敗b奶牛,在這種情況下a奶牛也能打敗b奶牛能打敗的奶牛,問通過這m個結果能確定多少個奶牛的名次。
解題思路:共有n奶牛,如果能打敗奶牛a的奶牛有x個,能被奶牛打敗的奶牛有y個,且x+y=n-1那麼奶牛a的名次就是確定的,且其名次為第x+1名。傳遞閉包,使用Floyd演算法進行求解
AC的C++程式:
#include<iostream>
#include<cstring>
using namespace std;
const int N=105;
bool r[N][N];//r[i][j]表示奶牛i能打敗奶牛j
int main()
{
int n,m,i,j,k,a,b;
scanf("%d%d",&n,&m);
memset(r,false,sizeof(r));
while(m--){
scanf("%d%d",&a,&b);
r[a][b]=true;//a能打敗b
}
for(k=1;k<=n;k++)
for(i=1;i<=n;i++)
for(int j=1;j<=n;j++)
if(r[i][k]&&r[k][j])//i能打敗k,k能打敗j,則i就能打敗j;閉包傳遞
r[i][j]=true;
int ans=0;
for(i=1;i<=n;i++){
for(j=1;j<=n;j++){
if(i==j) continue;
if(r[i][j]==false&&r[j][i]==false) break;//不能夠確定i和j的關係
}
if(j>n)
ans++;
}
printf("%d\n",ans);
return 0;
}
相關文章
- POJ 3360-Cow Contest(傳遞閉包)
- BZOJ 1612 [Usaco2008 Jan]Cow Contest奶牛的比賽:floyd傳遞閉包
- poj2594Treasure Exploration【最小路徑覆蓋+floyd傳遞閉包】
- 利用閉包傳遞引數
- [20160213]閉包傳遞5.txt
- [20160212]閉包傳遞4.txt
- [20160211]閉包傳遞3.txt
- [20160210]閉包傳遞2.txt
- [20160125]閉包傳遞問題.txt
- swift 閉包傳值Swift
- POJ 3613 Cow Relays 矩陣乘法Floyd+矩陣快速冪矩陣
- 「日常訓練」「小專題·圖論」 Cow Contest (1-3)圖論
- Floyd最短路演算法演算法
- 【圖論】Floyd演算法圖論演算法
- JS函式表示式——函式遞迴、閉包JS函式遞迴
- 最短路徑(Floyd演算法)演算法
- 最短路-SPFA演算法&Floyd演算法演算法
- 最短路演算法之:floyd 演算法演算法
- 揹包問題的遞迴與非遞迴演算法遞迴演算法
- 資料結構——Floyd演算法資料結構演算法
- Floyd演算法之個人見解演算法
- Floyd演算法——寫給自己(粗略)演算法
- 最短路徑之Floyd演算法演算法
- Repairing a Road(floyd演算法)AI演算法
- python實現Floyd演算法Python演算法
- Floyd演算法學習筆記演算法筆記
- 值傳遞與引用傳遞
- 值傳遞和引用傳遞
- 多源最短路徑演算法:Floyd演算法演算法
- [MATLAB]最短路徑Floyd演算法Matlab演算法
- Floyd演算法(計算最短路徑)演算法
- 閉包
- 用閉包替換遞迴實現斐波拉契數列遞迴
- 閉包 | 淺談JavaScript閉包問題JavaScript
- Swift-逃逸閉包、自動閉包Swift
- Windows 10傳遞最佳化功能有什麼用 Windows 10傳遞最佳化功能怎麼關閉Windows
- 最短路徑——Dijkstra演算法和Floyd演算法演算法
- 最短路徑—Dijkstra演算法和Floyd演算法演算法