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 ...
随机推荐
- (转)如何使用Java、Servlet创建二维码
归功于智能手机,QR码逐渐成为主流,它们正变得越来越有用.从候车亭.产品包装.家装卖场.汽车到很多网站,都在自己的网页集成QR码,让人们快速找到它们.随着智能手机的用户量日益增长,二维码的使用正在呈指 ...
- Chapter 3 Shared Assemblies and Strongly Named Assemblies
As time marches on,MS developers and control developer modify their code:they fix bugs,patch securit ...
- Loadrunner脚本自动关联和手动关联
关于Loadrunner关联一.什么时候需要关联 1.关联的含义 关联(correlation):在脚本回放过程中,客户端发出请求,通过关联函数所定义的左右边界值(也就是关联规则),在服 ...
- DB2删除表分区
近日,由于部门数据库读库空间过小,提出删除掉两个月之前日志表的分区(数据库分区是按时间月分区),记述如下: 上网搜索资料发现删除表分区大概分这么几步: 1.查询需要删除掉的分区: select t.D ...
- mongodb给我们提供了fsync+lock机制把数据暴力的刷到硬盘上
能不能把数据暴力的刷到硬盘上,当然是可以的,mongodb给我们提供了fsync+lock机制就能满足我们提的需求. fsync+lock首先会把缓冲区数据暴力刷入硬盘,然后给数据库一个写入锁,其他实 ...
- Preface Numbering
链接 分析:先打表需要用到的罗马数字,然后暴力转换,最后统计一下即可 /* PROB:preface ID:wanghan LANG:C++ */ #include "iostream&qu ...
- 登录加密 md5
实现账户和密码登录的加密 https://github.com/AndreasPizsa/md5-jkmyers
- py-day1简单使用方法及语法使用详解
一.python入门 1.python的标准格式 创建python文件以.py结尾 如:vi hello.py #!/usr/bin/env python #-*- coding:utf-8 -*- ...
- 使用 DDMenuController 类的方法(非常好用的抽屉类)
关于使用 DDMenuController 类的方法笔记:参考 DDMenuController 是一款非常好用的抽屉类文件. #pragma mark - 界面配置左右导航条的按钮 //[self ...
- 关于chrome被篡改主页修复方法
打开chrome的属性. 在目标框中追加以下内容 "PATH\chrome.exe" --flag-switches-begin --flag-switches-end about ...