Problem 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

题意:已知有n头牛,给出m对产量关系 x>y,求至少还要多少对产量关系才能给出所有牛的产量关系。

思路:n 头牛一共有 n*(n-1)/2 对关系,现在给出 m 对关系,假设根据这 m 对关系无法确定的关系有 sum 对,则题目也就是求 (sum-n)/2

枚举中间节点 K 之后就开始枚举起点 x 和终点 y,若 x 与 k,或 y 与 k 之间不联通,则无法确立关系,因此从起点 x 只检查能通向 k 的节点,从终点 y 只检查 k 能通向的节点,使用 Floyd 算法进行传递闭包运算,最后根据无法确定关系的牛的对数输出结果即可。

Source Program

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<cstdlib>
#include<queue>
#include<set>
#include<vector>
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define N 1001
#define MOD 123
#define E 1e-6
using namespace std;
int f[N][N];
int main ()
{int n,m;while(scanf("%d%d",&n,&m)!=EOF){for(int i=1;i<=m;i++){int x,y;scanf("%d%d",&x,&y);f[x][y]=1;}for(int k=1;k<=n;k++)for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)if(f[i][k]&f[k][j])f[i][j]=1;for(int i=1;i<=n;i++)//一旦具自反性,输出-1if(f[i][i]){printf("-1");return 0;}int sum=0;for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)if(!f[i][j]&&!f[j][i])//一旦存在未知sum++;printf("%d",(sum-n)/2);}return 0;
}

Ranking the Cows(POJ-3275 )相关推荐

  1. POJ 3275 Ranking the Cows (floyd传递闭包)

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

  2. Silver Cow Party (POJ - 3268 )

    Silver Cow Party (POJ - 3268 ) 这道题是我做的最短路专题里的一道题,但我还没做这个,结果比赛就出了,真是.......... 题目: One cow from each ...

  3. Bailian2734 十进制到八进制【入门】(POJ NOI0113-45)

    问题链接:POJ NOI0113-45十进制到八进制 2734:十进制到八进制 总时间限制: 1000ms 内存限制: 65536kB 描述 把一个十进制正整数转化成八进制. 输入 一行,仅含一个十进 ...

  4. Bailian2676 整数的个数【入门】(POJ NOI0105-11)

    问题链接:POJ NOI0105-11 整数的个数 2676:整数的个数 总时间限制: 1000ms 内存限制: 65536kB 描述 给定k(1 < k < 100)个正整数,其中每个数 ...

  5. Bailian4029 数字反转【进制】(POJ NOI0105-29)

    问题链接:POJ NOI0105-29 数字反转 4029:数字反转 总时间限制: 1000ms 内存限制: 65535kB 描述 给定一个整数,请将该数各个位上数字反转得到一个新数.新数也应满足整数 ...

  6. Bailian2735 八进制到十进制【入门】(POJ NOI0113-46)

    问题链接:POJ NOI0113-46 八进制到十进制 2735:八进制到十进制 总时间限制: 1000ms 内存限制: 65536kB 描述 把一个八进制正整数转化成十进制. 输入 一行,仅含一个八 ...

  7. 吴昊品游戏核心算法 Round 7 —— 熄灯游戏AI(有人性的Brute Force)(POJ 2811)

    暴力分为两种,一种属于毫无人性的暴力,一种属于有人性 的暴力.前面一种就不说了,对于后面一种情况,我们可以只对其中的部分问题进行枚举,而通过这些子问题而推导到整个的问题中.我称之为有人性的Brute ...

  8. 【二分】Best Cow Fences(poj 2018)

    Best Cow Fences poj 2018 题目大意: 给出一个正整数数列,要你求平均数最大,长度不小于M的字串,结果乘1000取整 输入样例 10 6 6 4 2 10 3 8 5 9 4 1 ...

  9. 昂贵的聘礼(poj 1062)

    Description 年轻的探险家来到了一个印第安部落里.在那里他和酋长的女儿相爱了,于是便向酋长去求亲.酋长要他用10000个金币作为聘礼才答应把女儿嫁给他.探险家拿不出这么多金币,便请求酋长降低 ...

  10. 主席树学习小结(POJ 2104)

    在高中的时候就听到过主席树了,感觉非常高端,在寒假的时候 winter homework中有一题是查找区间第K大的树,当时就开始百度这种网上的博客,发现主席树看不懂,因为那个root[i],还有tx[ ...

最新文章

  1. 使用VC连接Access数据库的两种方法
  2. sorry for yesterday
  3. 大成郡亮相乐居春季房展精装户型16500元
  4. iOS开发之iOS程序偏好设置(Settings Bundle)的使用
  5. 中央空调水系统与制冷系统运行参数
  6. mysql设置user_name唯一_mysql如何设置唯一性
  7. 客户端程序自动更新(升级)的方式
  8. mysql 查询语句 过滤_MySQL全面瓦解7:查询的过滤条件
  9. Python的内建属性和内建函数
  10. c语言题目1120,九度题目1120:全排列
  11. 80个非常酷的WordPress 杂志主题模板
  12. 【导入篇】Robotics:Perception课程_导入篇、四周课程内容、week 1st Perspective Projection
  13. [Errno 256] No more mirrors to try.
  14. Linux安装mysql客户端
  15. matlab的小波分析,MATLAB小波分析学习.pdf
  16. 八爪鱼-自定义模式采集数据_视频教程执行
  17. HTTP Live Streaming (HLS) 协议科普扫盲
  18. Python的函数理解外传
  19. dmg文件 linux,Linux通过命令行建立dmg文件的方法
  20. JAVA设计模式征服之路-00-设计模式简介

热门文章

  1. 手把手教你用Python求最大值和最小值
  2. 从治疗癌症到预测犯罪,细数数据科学在各领域的神奇应用
  3. 从用户不足2000万到27亿,这项技术真要上天了?
  4. 跟着阿里大牛捞干货:2019这5本书带你玩转大数据
  5. STM32之DAC原理
  6. 火遍全网的Hutool,如何使用Builder模式构建线程池
  7. 万字长文剖析 APM 系统?如何设计与实现?
  8. 码码的土狼:架构的原则、范式及治理
  9. JEECG 页面多个用户选择器只显示最后一个
  10. Spark2.3.1在Idea控制台调整日志等级