POJ-3660.Cow Contest(有向图的传递闭包)

Cow Contest

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 17797   Accepted: 9893

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 ≤ NA ≠ 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

USACO 2008 January Silver

本题思路:将题目给出的已知边都存入图中,利用传递性求出可能存在的每条边,对于一个学生,用i -> j表示 i 比 j 强,那么对于所有学生,他的排名被确定的条件就是确定他与其它所有同学的排名情况,即Bigger[ i ] + Smaller[ i ] == n - 1。

参考代码:(不建议看,按照上面的思路实现一波就行了)

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <queue>
 4 using namespace std;
 5
 6 const int maxn = 100 + 2, INF = 0x3f3f3f3f;
 7 int n, m, a, b;
 8 bool G[maxn][maxn];
 9
10 int Floyd_Warshall() {
11     int num = 0;
12     for(int k = 1; k <= n; k ++) {
13         for(int i = 1; i <= n; i ++) {
14             for(int j = 1; j <= n; j ++) {
15                 G[i][j] = G[i][j] || (G[i][k] && G[k][j]);
16             }
17         }
18     }
19     for(int i = 1; i <= n; i ++) {
20         int temp = 0;
21         for(int j = 1; j <= n; j ++)
22             if(G[i][j] || G[j][i]) temp ++;
23         if(temp == n - 1) num ++;
24     }
25     return num;
26 }
27
28 int main () {
29     scanf("%d %d", &n, &m);
30     int x, y;
31     for(int i = 0; i < m; i ++) {
32         scanf("%d %d", &x, &y);
33         G[x][y] = true;
34     }
35     int ans = Floyd_Warshall();
36     printf("%d\n", ans);
37     return 0;
38 }

原文地址:https://www.cnblogs.com/bianjunting/p/10705863.html

时间: 2024-08-30 14:48:28

POJ-3660.Cow Contest(有向图的传递闭包)的相关文章

poj 3660 Cow Contest(warshall算法)

poj 3660 Cow Contest 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 co

ACM: POJ 3660 Cow Contest - Floyd算法

链接 Cow Contest Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu 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. Eac

POJ3660:Cow Contest(Floyd传递闭包)

Cow Contest Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16941   Accepted: 9447 题目链接:http://poj.org/problem?id=3660 Description: N (1 ≤ N ≤ 100) cows, conveniently numbered 1..N, are participating in a programming contest. As we all k

【POJ】3660 Cow Contest

题目链接:http://poj.org/problem?id=3660 题意:n头牛比赛,有m场比赛,两两比赛,前面的就是赢家.问你能确认几头牛的名次. 题解:首先介绍个东西,传递闭包,它可以确定尽可能多的元素之间的关系. 然后回到这道题,怎么能确认这头牛的名次,也就是不管它胜还是败都能推导出其他n-1头牛跟它的关系.具体思想看代码.QWQ 代码: 1 #include<cstdio> 2 #include<cstdlib> 3 #include<cstring> 4

P2419 [USACO08JAN]牛大赛Cow Contest

P2419 [USACO08JAN]牛大赛Cow Contest 海星 这题代码比较短 (哪题Floyd代码长的) 太真实了 直接上代码吧 这题就是一个经典的传递闭包问题 可以用拓扑排序啥的 不过还是Floyd简便一下 原文地址:https://www.cnblogs.com/qf-breeze/p/10473684.html

[USACO08JAN]牛大赛Cow Contest

题目链接: Cow Contest 分析: 听说是一个Floyd求传递闭包 被拓扑的标签骗了进去 首先如果整个图不连通那么显然没办法确定,因为两个连通块之间的信息没有办法传递 所以先并查集判一下 然后考虑拓扑排序,一个点能得到确定的排名当且仅当它能被之前所有入过队的点到达 代码: #include<bits/stdc++.h> #define N (300 + 10) #define M (50000 + 10) using namespace std; inline int read() {

poj 1985 Cow Marathon 【树的直径】

题目:poj 1985 Cow Marathon 题意:给出一个树,让你求树的直径. 分析: 树的直径:树上两点之间的最大距离. 我们从任意一点出发,BFS一个最远距离,然后从这个点出发,在BFS一个最远距离,就是树的直径. AC代码: /* POJ:1985 Cow Marathon 2014/10/12/21:18 Yougth*/ #include <cstdio> #include <iostream> #include <algorithm> #include

poj 1975 Median Weight Bead(传递闭包 Floyd)

链接:poj 1975 题意:n个珠子,给定它们之间的重量关系,按重量排序,求确定肯定不排在中间的珠子的个数 分析:因为n为奇数,中间为(n+1)/2,对于某个珠子,若有至少有(n+1)/2个珠子比它重或轻,则它肯定不排在中间 可以将能不能确定的权值初始化为0,能确定重量关系的权值设为1 #include<stdio.h> #include<string.h> int a[110][110]; int main() { int T,n,m,i,j,k,d,x,sum; scanf(

POJ 3613 Cow Relays 恰好n步的最短路径

http://poj.org/problem?id=3613 题目大意: 有T条路,从s到e走n步,求最短路径. 思路: 看了别人的... 先看一下Floyd的核心思想: edge[i][j]=min(edge[i][j],edge[i][k]+edge[k][j]) i到j的最短路是i到j的直接路径或者经过k点的间接路径,但是矩阵的更新总是受到上一次更新的影响 如果每次的更新都存进新矩阵,那么edge[i][k]+edge[k][j]是不是表示只经过三个点两条边的路径呢? min(edge[i