1.题目描述:

Cow Contest
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 11280   Accepted: 6262

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

2.题意概述:

有n只奶牛,有n个连续的实力,如果u的实力大于v的实力,就能打赢它,
 然后给定m种关系,求最后能确定其排名的奶牛个数。

3.解题思路:

离散的知识——传递闭包:有向图的传递闭包就是指有向图所有传递关系中的最小关系

如果一头牛被x头牛打败,并且可以打败y头牛,如果x+y=n-1,则我们容易知道这头牛的排名就被确定了,所以我们只要将任一头牛,可以打败其他的牛的个数x, 和能打败该牛的牛的个数y求出来,在遍历所有牛判断一下是否满足x+y=n-1,就知道这个牛的排名是否能确定了(而传递闭包,正好将所有能得出关系都求出来了), 再将满足这个条件的牛数目加起来就是所求解。 x可以看成是入度, y是出度。
 
在floyd-warshall求每对顶点间的最短路径算法中,可以通过O(v^3)的方法求出图的传递闭包。可以位每条边赋以权值1,然后运行Floyd-Wareshall。如果从  i  到  j  存在一条路径,则mp(i,j)<N,否则mp(i,j)=MAX。
 
 一种改进的算法是:由于我们需要的只是判断是否从i到j存在一条通路,所以在Floyd-Wareshall中的动态规划比较中,我们可以把min和+操作改为逻辑or( ||  )和逻辑(&&)。也就是将  mp[i][j] = min(mp[i][j],  mp[i][k]+mp[k][j]);    改成    if(mp[i][j] == 1 || (mp[i][k] == 1 && mp[k][j] == 1))   mp[i][j] = 1;
设  mp(i,j) = 1表示从 i 到 j 存在一条通路 p,且 p 的所有中间节点都在0,1,2,...,k中, 否则mp(i,j)=0。我们把边(i,j)加入到E*中当且仅当mp(i,j)=1。

4.AC代码:

#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <functional>
#include <cmath>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
#include <ctime>
#define INF 0x3f3f3f3f
#define maxn 100100
#define N 111
#define eps 1e-6
#define pi acos(-1.0)
#define e exp(1.0)
using namespace std;
const int mod = 1e9 + 7;
typedef long long ll;
typedef unsigned long long ull;
bool mp[N][N];
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 (mp[i][j] || (mp[i][k] && mp[k][j]))mp[i][j] = 1;
}
int main()
{
#ifndef ONLINE_JUDGEfreopen("in.txt", "r", stdin);freopen("out.txt", "w", stdout);long _begin_time = clock();
#endifint n, m;while (~scanf("%d%d", &n, &m)){memset(mp, 0, sizeof(mp));while (m--){int u, v;scanf("%d%d", &u, &v);mp[u][v] = 1;}floyd(n);int cnt = 0;for (int i = 1; i <= n; i++){int win = 0, lose = 0;for (int j = 1; j <= n; j++)if (i != j){if (mp[i][j])win++;if (mp[j][i])lose++;}if (win + lose == n - 1)cnt++;}printf("%d\n", cnt);}
#ifndef ONLINE_JUDGElong _end_time = clock();printf("time = %ld ms.", _end_time - _begin_time);
#endifreturn 0;
}

POJ3660 - Cow Contest - 关系传递闭包(最短路变形)+思维相关推荐

  1. POJ 3660 Cow Contest【传递闭包】

    解题思路:给出n头牛,和这n头牛之间的m场比赛结果,问最后能知道多少头牛的排名. 首先考虑排名怎么想,如果知道一头牛打败了a头牛,以及b头牛打赢了这头牛,那么当且仅当a+b+1=n时可以知道排名,即为 ...

  2. poj3660 Cow Contest

    Cow Contest Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8967   Accepted: 5032 Descr ...

  3. POJ 3660 Cow Contest(传递闭包floyed算法)

    Description N (1 ≤ N ≤ 100) cows, conveniently numbered 1..N, are participating in a programming con ...

  4. H - Cow Contest POJ - 3660(Floyd 传递闭包)

    H - Cow Contest POJ - 3660 题意: 有 n 头牛比赛,边 1 -> 2 代表 1 能赢 2 ,给你 m 条边,问能确定出多少头牛的名次? 思路: 如果 1->2 ...

  5. [传递闭包]POJ#3660 Cow Contest

    题面 传送门 Cow Contest Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 24342 Accepted: 13539 ...

  6. Cow Contest (传递闭包)

    题目链接:https://cn.vjudge.net/problem/POJ-3660#author=freeloop 不懂传递闭包的请戳这:https://blog.csdn.net/acm_136 ...

  7. Cow Contest【最短路-floyd】

    Cow Contest POJ - 3660 N (1 ≤ N ≤ 100) cows, conveniently numbered 1..N, are participating in a prog ...

  8. POJ 3660 Cow Contest 传递闭包+Floyd

    原题链接:http://poj.org/problem?id=3660 Cow Contest Time Limit: 1000MS   Memory Limit: 65536K Total Subm ...

  9. BZOJ 1612: [Usaco2008 Jan]Cow Contest奶牛的比赛【Floyd】

    1612: [Usaco2008 Jan]Cow Contest奶牛的比赛 Time Limit: 5 Sec Memory Limit: 64 MB Description FJ的N(1 <= ...

最新文章

  1. 创建Swap交换空间
  2. 小型荧光驱动电路实验电路
  3. C# cs文件表头模版
  4. 前端开源项目周报0307
  5. JVM初学之类java的类加载器和双亲委派模型
  6. python图像直方图、获取每一个柱的个数_python数字图像处理实现直方图与均衡化...
  7. 3号团队-团队任务4:每日例会(2018-11-28)
  8. Java多线程和并发(一),进程与线程的区别
  9. BZOJ4987:Tree(树形DP)
  10. MSDK手Q邀请透传参数问题:url编解码与base64编解码
  11. sFlow的安装和使用
  12. Think Python读书笔记及课后习题---【前三章】
  13. Android Menu 之 optionsMenu 详解
  14. 干货!软考中级网络工程师备考经验分享
  15. 数据结构与算法(C语言版)---魔王语言
  16. 网狐棋牌代码分析(二) CQueueServiceEvent初步分析
  17. 区块链开发(六)区块链架构与应用PPT
  18. delete this
  19. 5年亏炒股指亏1600万,最后才知道Q群里除了他全是骗子
  20. 艺术签名生成软件有哪些?如何生成艺术签名?

热门文章

  1. iOS 优雅的处理网络数据,你真的会吗?不如看看这篇.
  2. 软件测试方法进行调优,软件测试中性能调优的过程解析
  3. 【07节】Python3+Selenium4自动化 unittest 测试框架详解
  4. 深度剖析:伊朗钢铁厂入侵路径推测及对钢企数字化安全转型启示
  5. 程序员就非要科班出身
  6. php如何使html中的选择,PHP HTML DOM:如何选择所有可见/可读文本?
  7. 同义词/近义词查询易语言代码
  8. 个人所得税 java_java_计算个人所得税
  9. 打工人必备:这10款VS Code摸鱼神器还没安装?
  10. 使用RaGOO将基因组提升至染色体水平