POJ-3275:Ranking the Cows(Floyd、bitset)

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(N3)复杂度较高,此题M(边数)较少,可以用枚举边的方式,输入时记录每个节点的入边和出边,Floyd时枚举每个转折点的入边和出边。

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<vector>
 5 using namespace std;
 6
 7 int n,m,ans;
 8 bool mp[1005][1005];
 9 vector<int>ru[1005],chu[1005];
10
11 int main()
12 {
13     scanf("%d%d",&n,&m);
14
15     for (int i = 0; i < m; i++)
16     {
17         int a,b;
18         scanf("%d%d", &a, &b);
19         ru[b].push_back(a);
20         chu[a].push_back(b);
21         mp[a][b] = true;
22     }
23
24     for (int k = 1; k <= n; k++)
25         for (int a = 0; a < ru[k].size(); a++)
26             for (int b = 0; b < chu[k].size(); b++)
27             {//C++11的“int i:ru[k]”POJ貌似编译不过去
28                 int i = ru[k][a];
29                 int j = chu[k][b];
30                 if(!mp[i][j])
31                 {
32                     ru[j].push_back(i);
33                     chu[i].push_back(j);
34                     mp[i][j] = true;
35                     ans++;
36                 }
37             }
38
39     printf("%d\n",(n-1)*n/2-m-ans);
40
41     return 0;
42 }

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

 1 #include<cstdio>
 2 #include<bitset>
 3 #include<iostream>
 4 using namespace std;
 5
 6 const int maxn=1000+5;
 7 int n,m,ans;
 8 bitset<maxn>bit[maxn];//类似于上面那个方法的mp二维数组
 9
10 int main()
11 {
12     scanf("%d%d",&n,&m);
13
14     for (int i = 0; i < m; i++)
15     {
16         int a,b;
17         scanf("%d%d",&a,&b);
18         bit[a].set(b);//将bit[a][b]设为1
19     }
20
21     for (int i = 1; i <= n; i++)
22         for (int j = 1; j <= n; j++)
23             if (bit[j][i])//这其实就是个暴力的Floyd
24                 bit[j] |= bit[i];//或运算使得i中为1的点(即有向图中i指向的点),j也指向它
25
26     for (int i = 1; i <= n; i++)
27         for (int j = 1; j <= n; j++)
28             if (bit[i][j])
29                 ans++;
30     //这里的ans是枚举之后得到的所有边,已经Floyd处理过了,所以包括隐藏的
31
32     cout << n*(n-1)/2-ans << endl;
33
34     return 0;
35 }

原文地址:https://www.cnblogs.com/AlphaWA/p/9311772.html

时间: 2024-10-03 15:48:45

POJ-3275:Ranking the Cows(Floyd、bitset)的相关文章

Linux:文本编辑器总结(vi、vim)

文件编辑查看命令总结 vi 记事本 a 光标快速移动快捷方式 纵向移动: gg G ngg 横向移动: 0/^ $ b 移动光标转为编辑状态 i I a A o O C cc c 快速编辑文本内容信息 dd ndd yy nyy p np d 特殊操作编辑文本方式 显示或取消显示行号信息 :set nu :set nonu 撤销文本内容编辑操作 u undo ctrl + r redo 快速搜索文本指定内容 /搜索内容 -- n 进行选择 PS:忽略大小写搜索信息 1. 设置方法 :set ic

POJ 3186 Treats for the Cows (简单区间DP)

FJ has purchased N (1 <= N <= 2000) yummy treats for the cows who get money for giving vast amounts of milk. FJ sells one treat per day and wants to maximize the money he receives over a given period time. The treats are interesting for many reasons

POJ 3349:Snowflake Snow Snowflakes(数的Hash)

http://poj.org/problem?id=3349 Snowflake Snow Snowflakes Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 37609   Accepted: 9878 Description You may have heard that no two snowflakes are alike. Your task is to write a program to determine

POJ - 1981 :Circle and Points (圆的扫描线) hihocoder1508

题意:给定N个点,然后给定一个半径为R的圆,问这个圆最多覆盖多少个点. 思路:在圆弧上求扫描线. hihocoder1508的代码. #include<bits/stdc++.h> #define pdd pair<double,int> #define f first #define s second #define rep(i,a,b) for(int i=a;i<=b;i++) using namespace std; const int maxn=200010; co

Linux时间子系统之八:动态时钟框架(CONFIG_NO_HZ、tickless)

在前面章节的讨论中,我们一直基于一个假设:Linux中的时钟事件都是由一个周期时钟提供,不管系统中的clock_event_device是工作于周期触发模式,还是工作于单触发模式,也不管定时器系统是工作于低分辨率模式,还是高精度模式,内核都竭尽所能,用不同的方式提供周期时钟,以产生定期的tick事件,tick事件或者用于全局的时间管理(jiffies和时间的更新),或者用于本地cpu的进程统计.时间轮定时器框架等等.周期性时钟虽然简单有效,但是也带来了一些缺点,尤其在系统的功耗上,因为就算系统目

自适应滤波:最小均方误差滤波器(LMS、NLMS)

作者:桂. 时间:2017-04-02  08:08:31 链接:http://www.cnblogs.com/xingshansi/p/6658203.html 声明:欢迎被转载,不过记得注明出处哦~ [读书笔记08] 前言 西蒙.赫金的<自适应滤波器原理>第四版第五.六章:最小均方自适应滤波器(LMS,Least Mean Square)以及归一化最小均方自适应滤波器(NLMS,Normalized Least Mean Square).全文包括: 1)LMS与维纳滤波器(Wiener F

【POJ 2826】An Easy Problem?!(几何、线段)

两个木条装雨水能装多少. 两线段相交,且不遮盖的情况下才可能装到水. 求出交点,再取两线段的较高端点的较小值h,(h-交点的y)为三角形的高. 三角形的宽即为(h带入两条线段所在直线得到的横坐标的差值). 三角形的面积即为雨水的量. 坑点:如果用G++提交,ans要加上eps才能过,c++提交则没问题. #include <iostream> #include <cmath> #include <cstdio> #define MAX 1<<31 #defi

Cisco系列:Easy-VPN详细剖析(路由器、防火墙)

小生博客:http://xsboke.blog.51cto.com 小生 Q Q:1770058260 -------谢谢您的参考,如有疑问,欢迎交流 一. EASY-VPN简介 Ipsecvpn实现站点到站点的VPN Easyvpn实现远程VPN 1. Easy-vpn面临的问题 1)因为整个ipsec VPN加密传输的根本就是事先配置在设备上的预共享秘钥,一旦秘钥外泄,整个ipsec VPN所作的努力全都会付之东流 2)建立L2L IPSEC VPN时,双方都有固定的ip地址,这样我们才有可

python学习笔记10:python操作数据库(mysql、redis)

一.python操作mysql数据库 python3中操作mysql数据需要安装一个第三方模块,pymysql,使用 pip install pymysql 安装即可 二.python操作redis redis是一个nosql类型的数据库,数据都存在内存中,有很快的读写速度 python3中操作reids需要安装一个第三方模块,redis,使用 pip install redis 安装即可