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 ...
随机推荐
- delphi如何让程序最小化到任务栏(使用Shell_NotifyIcon API函数)
现在很多的应用程序都有这样一种功能,当用户选择最小化窗口时,窗口不是象平常那样最小化到任务栏上,而是“最小化”成一个任务栏图标.象FoxMail 3.0 NetVampire 3.0等都提供了这样的功 ...
- codeforces A. Point on Spiral 解题报告
题目链接:http://codeforces.com/problemset/problem/279/A 题目意思:给出一个坐标点(x, y),问当从(0, 0) 开始到达该点转过的拐角有多少个.(拐角 ...
- Yii的缓存机制之片段缓存
一.首先在main.php配置缓存组件 在components里面添加cache项.代码如下: // application components 'components'=>array( ...
- Vijos P1794 文化之旅
标签: 搜索图结构 最短路 NOIP普及组2012 ...
- hdu-5742 It's All In The Mind(数学)
题目链接: It's All In The Mind Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (J ...
- Python: PS滤镜--径向模糊
本文用 Python 实现 PS 滤镜中的径向模糊特效,具体的算法原理和效果可以参考之前的博客: http://blog.csdn.net/matrix_space/article/details/3 ...
- BZOJ_2730_ [HNOI2012]矿场搭建_点双联通分量
BZOJ_2730_ [HNOI2012]矿场搭建_点双联通分量 Description 煤矿工地可以看成是由隧道连接挖煤点组成的无向图.为安全起见,希望在工地发生事故时所有挖煤点的工人都能有一条出路 ...
- CF 1009 F Dominant Indices —— 长链剖分+指针
题目:http://codeforces.com/contest/1009/problem/F 也可以用 dsu on tree 的做法,全局记录一个 dep,然后放进堆里,因为字典序要最小,所以再记 ...
- Win10+CUDA9.0+cudnn7.1安装
CUDA下载 cudnn下载 CUDA默认安装即可. cudnn下载解压之后,将对应的文件分别拷贝到CUDA Toolkit中即可: 对应的文件夹为: 若为默认安装,则应分别拷贝到的文件夹如下: C ...
- CS231n 2016 通关 第三章-Softmax 作业
在完成SVM作业的基础上,Softmax的作业相对比较轻松. 完成本作业需要熟悉与掌握的知识: cell 1 设置绘图默认参数 mport random import numpy as np from ...