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

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对牛的相对排名(比如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时枚举每个转折点的入边和出边。

 #include<cstdio>
#include<cstring>
#include<iostream>
#include<vector>
using namespace std; int n,m,ans;
bool mp[][];
vector<int>ru[],chu[]; int main()
{
scanf("%d%d",&n,&m); for (int i = ; i < m; i++)
{
int a,b;
scanf("%d%d", &a, &b);
ru[b].push_back(a);
chu[a].push_back(b);
mp[a][b] = true;
} for (int k = ; k <= n; k++)
for (int a = ; a < ru[k].size(); a++)
for (int b = ; b < chu[k].size(); b++)
{//C++11的“int i:ru[k]”POJ貌似编译不过去
int i = ru[k][a];
int j = chu[k][b];
if(!mp[i][j])
{
ru[j].push_back(i);
chu[i].push_back(j);
mp[i][j] = true;
ans++;
}
} printf("%d\n",(n-)*n/-m-ans); return ;
}

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

 #include<cstdio>
#include<bitset>
#include<iostream>
using namespace std; const int maxn=+;
int n,m,ans;
bitset<maxn>bit[maxn];//类似于上面那个方法的mp二维数组 int main()
{
scanf("%d%d",&n,&m); for (int i = ; i < m; i++)
{
int a,b;
scanf("%d%d",&a,&b);
bit[a].set(b);//将bit[a][b]设为1
} for (int i = ; i <= n; i++)
for (int j = ; j <= n; j++)
if (bit[j][i])//这其实就是个暴力的Floyd
bit[j] |= bit[i];//或运算使得i中为1的点(即有向图中i指向的点),j也指向它 for (int i = ; i <= n; i++)
for (int j = ; j <= n; j++)
if (bit[i][j])
ans++;
//这里的ans是枚举之后得到的所有边,已经Floyd处理过了,所以包括隐藏的 cout << n*(n-)/-ans << endl; return ;
}

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. Python pip源,解决安装慢的问题

    经常在使用Python的时候需要安装各种模块,而pip是很强大的模块安装工具,但是由于国外官方pypi经常被墙,导致不可用,所以我们最好是将自己使用的pip源更换一下,这样就能解决被墙导致的装不上库的 ...

  2. [NOIP2011提高组day2]-1-计算系数

    1.计算系数 (factor.cpp/c/pas) [问题描述] k n m给定一个多项式(ax+by)^k ,请求出多项式展开后(x^n)*(y^m)项的系数. [输入] 输入文件名为 factor ...

  3. UVA11752 The Super Powers —— 数论、枚举技巧

    题目链接:https://vjudge.net/problem/UVA-11752 题意: 一个超级数是能够至少能表示为两个数的幂,求1~2^64-1内的超级数. 题解: 1.可知对于 n = a^b ...

  4. HDU3723 Delta Wave —— 卡特兰数

    题目链接:https://vjudge.net/problem/HDU-3723 Delta Wave Time Limit: 6000/3000 MS (Java/Others)    Memory ...

  5. .NET CORE2.0后台管理系统(一)配置API

    一:引用关系图 要写一个项目首先离不开的就是一个清晰的流程图,当然我这里很简单. 上诉完成后打开api下的Startup.cs文件,因为我是配置好了所在我直接上传代码然后介绍一下: using Sys ...

  6. [ZJOI 2010] 排列计数

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=2111 [算法] 一种比较好的理解方式是将该序列看成是一棵堆式存储的二叉树 那么问题转 ...

  7. poj1151 Atlantis——扫描线+线段树

    题目:http://poj.org/problem?id=1151 经典的扫描线问题: 可以用线段树的每个点代表横向被矩形上下边分割开的每一格,这样将一个矩形的出现或消失化为线段树上的单点修改: 每个 ...

  8. PTA 哈希查找 除留取余法

    PTA 电话聊天狂人(25 分) 给定大量手机用户通话记录,找出其中通话次数最多的聊天狂人. 输入格式: 输入首先给出正整数N(≤10​5​​),为通话记录条数.随后N行,每行给出一条通话记录.简单起 ...

  9. vue watch 深度监听以及立即监听

    vue watch对象可以监听数据,数据发生变化,处理函数 watch虽可以监听,但只是浅监听,只监听数据第一层或者第二层.比如对于整个对象的监听,需要用到深度监听 vm.$watch('obj',f ...

  10. u盘启动安装系统

    七彩虹主板如何设置U盘启动,本文就以七彩虹CG41主板为例详细的讲讲U盘启动设置方法. 几天前,想用U盘启动的时候,发现CG41主板启动顺序里找不到USB项,Boot Device Priority( ...