Ranking the Cows
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 2248   Accepted: 1045

Description

Each of Farmer John's N cows (1 ≤ N ≤ 1,000) produces milk at a different positive rate, and FJ would like to order his cows according to these rates from the fastest milk producer to the slowest.

FJ has already compared the milk output rate for M (1 ≤ M ≤ 10,000) pairs of cows. He wants to make a list of C additional pairs of cows such that, if he now compares those C pairs, he will definitely be able to deduce the correct ordering of all N cows. Please help him determine the minimum value of C for which such a list is possible.

Input

Line 1: Two space-separated integers: N and M 
Lines 2..M+1: Two space-separated integers, respectively: X and Y. Both X and Y are in the range 1...N and describe a comparison where cow X was ranked higher than cow Y.

Output

Line 1: A single integer that is the minimum value of C.

Sample Input

5 5
2 1
1 5
2 3
1 4
3 4

Sample Output

3

Hint

From the information in the 5 test results, Farmer John knows that since cow 2 > cow 1 > cow 5 and cow 2 > cow 3 > cow 4, cow 2 has the highest rank. However, he needs to know whether cow 1 > cow 3 to determine the cow with the second highest rank. Also, he will need one more question to determine the ordering between cow 4 and cow 5. After that, he will need to know if cow 5 > cow 3 if cow 1 has higher rank than cow 3. He will have to ask three questions in order to be sure he has the rankings: "Is cow 1 > cow 3? Is cow 4 > cow 5? Is cow 5 > cow 3?"

Source

USACO 2007 March Gold

题目:FJ想按照奶牛产奶的能力给她们排序。现在已知有N头奶牛(1 ≤ N ≤ 1,000)。FJ通过比较,已经知道了M(1 ≤ M ≤ 10,000)对相对关系。每一对关系表示为“X Y”,意指X的产奶能力强于Y。现在FJ想要知道,他至少还要调查多少对关系才能完成整个排序。

思路:如果排序可以确定了,潜台词就是任意两头牛之间的关系都可以确定了。N头奶牛一共有C(N, 2) = N * (N - 1) / 2对关系。由现在已知的关系如能确认K对关系,则要调查的次数就是C(N, 2) - K。

问题再思考一下就能发现,若u强于v,就连一条由u到v的有向路。两个节点之间有联系,就等价于这两个节点之间有一条有向路。这样就变成了任两点之间是否存在路径问题,用floyd传递闭包就可以了。

但最多有1,000头奶牛,时间复杂度为O(N^3)。肯定会超,思考一下就会发现,枚举中间节点K之后就开始枚举起点u和终点v,若u与K,或者 v与K之间根本就不联通,那么绝对无法松弛。所以说更高效的方式就是起点只检查能通向K的节点,终点只检查K能通向的节点,这样就会把复杂度大大降低,因为边最多只有10000条。floyd 用边表实现,学习了。。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4
 5 using namespace std;
 6
 7 const int N=1010;
 8
 9 int map[N][N],pre[N][N],suc[N][N];  //pre记录前驱,suc记录后继 pre[v][0]代表v的前驱的个数,suc[u][0]代表u的后继的个数
10
11 int main(){
12
13     //freopen("input.txt","r",stdin);
14
15     int n,m;
16     while(~scanf("%d%d",&n,&m)){
17         memset(map,0,sizeof(map));
18         memset(pre,0,sizeof(pre));
19         memset(suc,0,sizeof(suc));
20         int u,v;
21         int cnt=m;
22         while(m--){
23             scanf("%d%d",&u,&v);
24             map[u][v]=1;
25             pre[v][++pre[v][0]]=u;  //v的前驱是u
26             suc[u][++suc[u][0]]=v;  //u的后继是v
27         }
28         int i,j,k;
29         for(k=1;k<=n;k++)
30             for(i=1;i<=pre[k][0];i++){
31                 u=pre[k][i];         //k的前驱是u
32                 for(j=1;j<=suc[k][0];j++){
33                     v=suc[k][j];        //k的后继是v
34                     if(!map[u][v]){ //u是k的前驱,v是k的后继 所以u是v的前驱
35                         pre[v][++pre[v][0]]=u;   //u 和 v 之间也建立关系
36                         suc[u][++suc[u][0]]=v;
37                         map[u][v]=1;
38                         cnt++;
39                     }
40                 }
41             }
42         printf("%d\n",n*(n-1)/2-cnt);
43     }
44     return 0;
45 }

POJ 3275 Ranking the Cows (floyd传递闭包)相关推荐

  1. poj 3275 Ranking the Cows 搜索

    题意:给你n个数和m个大小关系,问给出了这些大小关系之后还有多少对数的大小关系不知道. 分析:根据给出的大小关系建有向边,每次对每个点延有向边搜索到底,确定该边的所有连边关系,最后统计没有关系的边对数 ...

  2. POJ 3660 Cow ContestCow(Floyd传递闭包)题解

    题意:给出m个关系,问你能确定机头牛的排名 思路:要确定排名那必须要把他和其他n-1头牛比过才行,所以Floyd传递闭包,如果赢的+输的有n-1就能确定排名. 代码: #include<cstd ...

  3. 【POJ No. 3275】奶牛排序 Ranking the Cows

    [POJ No. 3275]奶牛排序 Ranking the Cows 官方题目地址 [题意] 约翰想按照奶牛的产奶能力给它们排序. 已知有N (1≤N ≤1 000)头奶牛,而且知道这些奶牛的M ( ...

  4. POJ - 1094 Sorting It All Out(拓扑排序+floyd传递闭包)

    题目链接:点击查看 题目大意:给出N个点以及M个比较关系,问在第几个数字可以确定出唯一的序列,或者判断出矛盾的序列,或者最后也无法确定出一个唯一的序列 题目分析:关于这个题目可以直接分类讨论,可以直接 ...

  5. 【POJ - 2594】Treasure Exploration(floyd传递闭包 + 最小路径覆盖,图论)

    题干: Have you ever read any book about treasure exploration? Have you ever see any film about treasur ...

  6. POJ3275 Ranking the Cows【关系闭包】

    Ranking the Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 3510 Accepted: 1608 Desc ...

  7. POJ1683 Puzzlestan ——Floyd传递闭包+Dfs

    好久没写Dfs了,拿来练手. WA了一次,没有判断中间的情况-- 解法:先用Floyd传递闭包处理哪些点一定要在一起.哪些点一定不能在一起,六重循环. 然后深搜,res[i][j]表示1,i这个物品在 ...

  8. CIA3 NOI接站(tarjan缩环+Floyd传递闭包+可相交最小路径覆盖)

    可以发现,题目求得就是一个可相交最小路径覆盖,但是有环.所以我们先用tarjan把环都缩掉,然后Floyd传递闭包,求二分图最大匹配,答案就是scc-ans. #include <cstdio& ...

  9. POJ - 3179 Corral the Cows(二分,离散化,前缀和)

    POJ - 3179 Corral the Cows #include<iostream> #include<vector> #include<algorithm> ...

最新文章

  1. python中turtle画酷炫图案-用python打造可视化爬虫监控系统,酷炫的图形化界面
  2. 华为RH2288V3服务器部署指南
  3. 【网址收藏】linux namespace和cgroup
  4. 上天入海又怎样?阿里的运动达人纷纷表示不服
  5. 计算机软件记不住设置,想知道电脑密码记不住了怎么办
  6. windows运行对话框_如何在Windows运行对话框中添加文本快捷方式?
  7. 缓存淘汰策略—LRU算法(java代码实现)
  8. Python 分析二手房源信息,揭晓土地交易现状
  9. Lua学习小记——语言
  10. WPF 语言格式化文本控件
  11. zul使用java_java – 从Jar加载ZUL
  12. 桌面计算机名水印,去掉电脑桌面的Windows10教育版水印的方法
  13. 这才是程序员的元宵节打开方式:亲手做一盏花灯,轻松学三维绘图
  14. windows下iexplore的命令行参数
  15. webgl天空盒边界缝隙_基于webGL技术的3D库ThingJS支持天空盒技术实现
  16. 아 / 어/여서与고 的区别
  17. 【算法学习】笨拙的奶牛
  18. USRP系列(三):NI 与Ettus Research的USRP区别
  19. CyberLink PowerDVD Ultra v19.0.2005.62极致中文破解版
  20. Linux操作系统同时启动多个服务脚本

热门文章

  1. 【hdu5285】wyh2000 and pupil
  2. CSU 1806 Toll 自适应simpson积分+最短路
  3. Yslow on Nodejs server
  4. etcd+calico集群的部署
  5. 圆桌问题 2011-12-29
  6. 安卓常用功能——已封装好
  7. 从oracle10g 10.2.0.1 升级到10.2.0.4碰到的问题及解决
  8. 网上看的一篇文章,感觉会给程序员一些启发
  9. 【数字信号处理】离散时间信号 ( 模拟信号、离散时间信号、数字信号 | 采样导致时间离散 | 量化导致幅度离散 )
  10. 【Windows 逆向】OD 调试器工具 ( 推荐汉化版的 OD 调试工具 | 吾爱破解专用版Ollydbg | 备选工具 )