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 cowY.

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

题意:有N头奶牛,现在给出M对产奶量关系U>V,问至少还需要知道多少奶牛可以做到全部奶牛产奶关系。

思路:有向图,问至少再加多少边,使得任意两点S、T的可以到达(S到达T或者到达S)。闭包传递后不能到达的需要加边,ans++。

至于为什么闭包传递后不连通就就要ans++呢,假设已知了1>2>3>4,5>6>7,我们知道了4>5不就行了吗,ans=1啊。

但是注意题目说的是"确定",而比较了4和5之后可能会4<5,即任然不可以把知道全部顺序。

所以剩下对于C(n,2)对关系里不确定的关系,都有知道才能“确定”所有的大小关系。

#include<cstdio>
#include<bitset>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=;
bitset<maxn>mp[maxn];
int main()
{
int N,M,ans,i,j,k;
while(~scanf("%d%d",&N,&M)){
ans=;memset(mp,,sizeof(mp));
for(i=;i<=M;i++){
scanf("%d%d",&j,&k);
mp[j].set(k);
} for(k=;k<=N;k++)
for(i=;i<=N;i++)
if(mp[i][k])
mp[i]|=mp[k];
for(i=;i<=N;i++)
for(j=i+;j<=N;j++)
if(!mp[i][j]&&!mp[j][i])
ans++;
printf("%d\n",ans);
}
return ;
}

最新文章

  1. cocos2d-x宏定义
  2. SQL语句小总结
  3. web工程依赖的问题
  4. javascript中数组揭秘
  5. coins_多重背包
  6. Map中如何把没有定义操作符&lt;的类作为key
  7. Tomcat架构(二)
  8. BZOJ 1782: [Usaco2010 Feb]slowdown 慢慢游( BIT + dfs )
  9. CentOS6.8安装mysql5.6
  10. java调用wkhtmltopdf生成pdf文件,美观,省事
  11. 常用CSS样式速查
  12. 补齐-Django之Model操作
  13. TCP加速锐速SS(ServerSpeeder)破解版一键安装
  14. 2018.11.01 bzoj4325: NOIP2015 斗地主(贪心+搜索)
  15. Exception in thread main java.lang.NoClassDefFoundError: org/apache/juli/logging/LogFacto
  16. sql server 查询分析器中表名无效,有红线,其实是这张表的
  17. 仿照swpu邮寄系统的登录页面
  18. 【spring源码学习】spring的IOC容器之自定义xml配置标签扩展namspaceHandler向IOC容器中注册bean
  19. 不使用C++ 11的整数转字符串
  20. Git在Xcode中的配置与使用常见问题总结

热门文章

  1. FineUI 获取x_state并解析
  2. Linux基础命令(四)
  3. C#HTML与UBB(纯文本)之间的转换
  4. 我的Android进阶之旅】GitHub 上排名前 100 的 Android 开源库进行简单的介绍
  5. oracle入门(4)——少而常用的命令
  6. jvm堆配置参数
  7. go——变量
  8. LeetCode:前K个高频元素【347】
  9. LeetCode:路径总和II【113】
  10. PAT 天梯赛 L1-008. 求整数段和 【水】