POJ-3275:Ranking the Cows(Floyd、bitset)
| 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
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
Sample Input
5 5
2 1
1 5
2 3
1 4
3 4
Sample Output
3
Hint
概译:农夫有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)的更多相关文章
- py库:文本转为语音(pywin32、pyttsx)
		http://blog.csdn.net/marksinoberg/article/details/52137547 Python 文本转语音 文本转为语音(使用Speech API) 需要安装 py ... 
- Java基础:整型数组(int[]、Integer[])排序
		Windows 10家庭中文版,java version "1.8.0_152",Eclipse Oxygen.1a Release (4.7.1a), 参考链接:http://w ... 
- 九度OJ 1255:骰子点数概率 (递归、DP)
		时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:316 解决:29 题目描述: 把n个骰子扔在地上,所有骰子朝上一面的点数之和为S.输入n,打印出S的所有可能的值出现的概率. 输入: 输入包 ... 
- 九度OJ 1159:坠落的蚂蚁 (模拟、排序)
		时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:1098 解决:277 题目描述: 一根长度为1米的木棒上有若干只蚂蚁在爬动.它们的速度为每秒一厘米或静止不动,方向只有两种,向左或者向右.如 ... 
- 九度OJ 1035:找出直系亲属 (二叉树、递归)
		时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2380 解决:934 题目描述: 如果A,B是C的父母亲,则A,B是C的parent,C是A,B的child,如果A,B是C的(外) ... 
- ZYNQ笔记(3):GPIO的使用(MIO、EMIO)——led灯
		一.GPIO原理 1.GPIO介绍 程序员通过软件代码可以独立和动态地对每个 GPIO 进行控制,使其作为输入.输出或中断. (1)通过一个加载指令,软件可以读取一个 GPIO 组内所有 GPIO 的 ... 
- YNOI2016:掉进兔子洞 (莫队+bitset)
		YNOI2016:掉进兔子洞 题意简述: 有 m 个询问,每次询问三个区间,把三个区间中同时出现的数一个一个删掉,问最后三个区间剩下的数的个数和,询问独立. 注意这里删掉指的是一个一个删,不是把等于这 ... 
- POJ 3349:Snowflake Snow Snowflakes(数的Hash)
		http://poj.org/problem?id=3349 Snowflake Snow Snowflakes Time Limit: 4000MS Memory Limit: 65536K T ... 
- POJ 3617:Best Cow Line(贪心,字典序)
		Best Cow Line Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 30684 Accepted: 8185 De ... 
随机推荐
- codeforces776E
			传送门 这题看着很唬人,但实际上是道水题... f[n]通过打表或证明,可以发现就是欧拉函数,g[n]恒等于n,所以题目的意思就是让你求n的k次欧拉函数. 可以发现实际上k次欧拉函数,n的数值减小得很 ... 
- android4.3 蓝牙BLE编程
			一.蓝牙4.0简介 蓝牙4.0标准包含两个蓝牙标准,准确的说,是一个双模的标准,它包含传统蓝牙部分(也有称之为经典蓝牙Classic Bluetooth)和低功耗蓝牙部分(Bluetooth Low ... 
- linux应用之apache服务的安装及配置(centos)
			CentOS Apache服务器安装与配置 一.安装Apache程序,一般有三种安装方式:1.直接网络安装:2.下载rpm包,上传至服务器进行安装:3.通过原代码编译安装: yum -y inst ... 
- jQuery ajax中的get请求方法汇总
			$.get() Defination and Usage 从服务端以HTTP GET方式获取数据 Examples 请求test.php,但是忽略返回的数据 $.get("test.php& ... 
- C++中对类的提前引用声明注意事项
			//或许,友元是VC++6.0心里永远的痛,对于这个BUG我一直很介意.//注:这个程序在VC++6.0里是行不通的,在VS2008里是可以的.#include <iostream> #i ... 
- 51nod1934:受限制的排列 (分治+组合数)
			对于一个 11 到 nn 的排列 p1,p2,⋯,pnp1,p2,⋯,pn ,我们可以轻松地对于任意的 1≤i≤n1≤i≤n 计算出 (li,ri)(li,ri) ,使得对于任意的 1≤L ... 
- 经验收获Linux终端下方便命令
			一.命令行日常系快捷键 如下的快捷方式非常有用,能够极大的提升你的工作效率: CTRL + U - 剪切光标前的内容 CTRL + K - 剪切光标至行末的内容 CTRL + Y - 粘贴 CTRL ... 
- pdf2swf 转换时报This file is too complex to render- SWF only supports 65536 shapes at once
			ERROR ID Table overflow ERROR This file is too complex to render- SWF only supports 65536 shapes ... 
- 准备看的ros相关源码
			进程通信:lcm 导航:navigation 3D可视化工具:rviz Mobile Robot Programming Toolkit:mrpt 其他: 人体肌肉:simbody openslam ... 
- Empire Strikes Back
			题意: 给定$K$个数字,求最小的正整数$n$,使得$\prod_{i=1}^{K}{a_i !} | n!$ 解法: 注意到$$\sum_{p为质数}{1/p} = O(loglogn)$$, 这样 ... 
