Ranking the Cows
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 3301   Accepted: 1511

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

  1. 5 5
  2. 2 1
  3. 1 5
  4. 2 3
  5. 1 4
  6. 3 4

Sample Output

  1. 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?"
 

概译:农夫有N头牛,他要给牛排名,他已经知道M对牛的相对排名(比如X>Y),求出他还需要知道多少对,就能准确地将所有牛排名。

输入:输入N,M。接下来的M行每行输入X,Y,代表X>Y。

思路:当任意两头牛都明确知道相对rank时,即可以准确排名,此时共须知n*(n-1)/2对。已给M对,再求出这M对所隐藏的排名共ans对(例:2>1,1>5是M对里给出的,则2>5是隐藏的一对),则n*(n-1)/2 - M - ans就是最后的输出。

可视为有向图,2>1则画出一条2指向1的边,用Floyd就可以完成对隐藏路径的连通。O(N³)复杂度较高,此题M(边数)较少,可以用枚举边的方式,输入时记录每个节点的入边和出边,Floyd时枚举每个转折点的入边和出边。

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<iostream>
  4. #include<vector>
  5. using namespace std;
  6.  
  7. int n,m,ans;
  8. bool mp[][];
  9. vector<int>ru[],chu[];
  10.  
  11. int main()
  12. {
  13. scanf("%d%d",&n,&m);
  14.  
  15. for (int i = ; i < m; i++)
  16. {
  17. int a,b;
  18. scanf("%d%d", &a, &b);
  19. ru[b].push_back(a);
  20. chu[a].push_back(b);
  21. mp[a][b] = true;
  22. }
  23.  
  24. for (int k = ; k <= n; k++)
  25. for (int a = ; a < ru[k].size(); a++)
  26. for (int b = ; b < chu[k].size(); b++)
  27. {//C++11的“int i:ru[k]”POJ貌似编译不过去
  28. int i = ru[k][a];
  29. int j = chu[k][b];
  30. if(!mp[i][j])
  31. {
  32. ru[j].push_back(i);
  33. chu[i].push_back(j);
  34. mp[i][j] = true;
  35. ans++;
  36. }
  37. }
  38.  
  39. printf("%d\n",(n-)*n/-m-ans);
  40.  
  41. return ;
  42. }

也可以使用STL容器bitset,它使得mp数组以二进制01串形式进行位运算,通常可以将复杂度除以64.使用详见代码:

  1. #include<cstdio>
  2. #include<bitset>
  3. #include<iostream>
  4. using namespace std;
  5.  
  6. const int maxn=+;
  7. int n,m,ans;
  8. bitset<maxn>bit[maxn];//类似于上面那个方法的mp二维数组
  9.  
  10. int main()
  11. {
  12. scanf("%d%d",&n,&m);
  13.  
  14. for (int i = ; i < m; i++)
  15. {
  16. int a,b;
  17. scanf("%d%d",&a,&b);
  18. bit[a].set(b);//将bit[a][b]设为1
  19. }
  20.  
  21. for (int i = ; i <= n; i++)
  22. for (int j = ; j <= n; j++)
  23. if (bit[j][i])//这其实就是个暴力的Floyd
  24. bit[j] |= bit[i];//或运算使得i中为1的点(即有向图中i指向的点),j也指向它
  25.  
  26. for (int i = ; i <= n; i++)
  27. for (int j = ; j <= n; j++)
  28. if (bit[i][j])
  29. ans++;
  30. //这里的ans是枚举之后得到的所有边,已经Floyd处理过了,所以包括隐藏的
  31.  
  32. cout << n*(n-)/-ans << endl;
  33.  
  34. return ;
  35. }

POJ-3275:Ranking the Cows(Floyd、bitset)的更多相关文章

  1. py库:文本转为语音(pywin32、pyttsx)

    http://blog.csdn.net/marksinoberg/article/details/52137547 Python 文本转语音 文本转为语音(使用Speech API) 需要安装 py ...

  2. Java基础:整型数组(int[]、Integer[])排序

    Windows 10家庭中文版,java version "1.8.0_152",Eclipse Oxygen.1a Release (4.7.1a), 参考链接:http://w ...

  3. 九度OJ 1255:骰子点数概率 (递归、DP)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:316 解决:29 题目描述: 把n个骰子扔在地上,所有骰子朝上一面的点数之和为S.输入n,打印出S的所有可能的值出现的概率. 输入: 输入包 ...

  4. 九度OJ 1159:坠落的蚂蚁 (模拟、排序)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:1098 解决:277 题目描述: 一根长度为1米的木棒上有若干只蚂蚁在爬动.它们的速度为每秒一厘米或静止不动,方向只有两种,向左或者向右.如 ...

  5. 九度OJ 1035:找出直系亲属 (二叉树、递归)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2380 解决:934 题目描述:     如果A,B是C的父母亲,则A,B是C的parent,C是A,B的child,如果A,B是C的(外) ...

  6. ZYNQ笔记(3):GPIO的使用(MIO、EMIO)——led灯

    一.GPIO原理 1.GPIO介绍 程序员通过软件代码可以独立和动态地对每个 GPIO 进行控制,使其作为输入.输出或中断. (1)通过一个加载指令,软件可以读取一个 GPIO 组内所有 GPIO 的 ...

  7. YNOI2016:掉进兔子洞 (莫队+bitset)

    YNOI2016:掉进兔子洞 题意简述: 有 m 个询问,每次询问三个区间,把三个区间中同时出现的数一个一个删掉,问最后三个区间剩下的数的个数和,询问独立. 注意这里删掉指的是一个一个删,不是把等于这 ...

  8. POJ 3349:Snowflake Snow Snowflakes(数的Hash)

    http://poj.org/problem?id=3349 Snowflake Snow Snowflakes Time Limit: 4000MS   Memory Limit: 65536K T ...

  9. POJ 3617:Best Cow Line(贪心,字典序)

    Best Cow Line Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 30684   Accepted: 8185 De ...

随机推荐

  1. 自动化测试框架selenium+java+TestNG——配置篇

    最近来总结下自动化测试 selenium的一些常用框架测试搭配,由简入繁,最简单的就是selenium+java+TestNG了,因为我用的是java,就只是总结下java了. TestNG在线安装: ...

  2. java学习之super关键字

    对于具有public或者protected属性的父类,其子类若想继承父亲的属性或者方法,那么需要用到super 实例:

  3. Linux下抓包工具tcpdump应用详解

      TCPDUMP简介 在传统的网络分析和测试技术中,嗅探器(sniffer)是最常见,也是最重要的技术之一.sniffer工具首先是为网络管理员和网络程序员进行网络分析而设计的.对于网络管理人员来说 ...

  4. 51nod1674:区间的价值2(分治,利用&和|的收敛性)

    lyk拥有一个区间. 它规定一个区间的价值为这个区间中所有数and起来的值与这个区间所有数or起来的值的乘积. 例如3个数2,3,6.它们and起来的值为2,or起来的值为7,这个区间对答案的贡献为2 ...

  5. 「NOIP2014」「Codevs3728」 联合权值(乱搞

    3728 联合权值 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold   题目描述 Description 输入描述 Input Description 输出描述 Ou ...

  6. webpack 基本功能和原理

    依赖管理:方便引用第三方模块.让模块更容易复用.避免全局注入导致的冲突.避免重复加载或加载不需要的模块. 合并代码:把各个分散的模块集中打包成大文件,减少HTTP的请求链接数,配合UglifyJS可以 ...

  7. PhpStorm中如何配置SVN,详细操作方法

    1.简介: PhpStorm是一个轻量级且便捷的PHP IDE,其提供的智能代码补全,快速导航以及即时错误检查等功能大大提高了编码效率.它是一款商业的 PHP 集成开发工具,以其独特的开发便利性,短时 ...

  8. 表单提交Post方法、Get方法

     表单用来接受用户的输入,并将用户的输入以“name=value值对”集合的形式提交到服务器进行处理.那么表单是怎样将数据提交到服务器的?服务器是怎样对表单数据进行处理的?下面我将为大家揭开表单提交背 ...

  9. centos7使用ceph-deploy部署ceph

    准备阶段 准备yum源 删除默认的源,国外的比较慢 yum clean all rm -rf /etc/yum.repos.d/*.repo 下载阿里云的base源 wget -O /etc/yum. ...

  10. 使用libvirt技术监控虚拟机资源利用情况

    使用libvirt技术监控虚拟机资源利用情况 (一)计算资源与内存资源的监控 libvirt中提供virDomainGetInfo方法可以将一个domain的计算资源和内存资源的使用情况封装在一个结构 ...