题目链接

题目

题目描述

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.

输入描述

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.

输出描述

Line 1: A single integer that is the minimum value of C.

示例1

输入

5 5
2 1
1 5
2 3
1 4
3 4

输出

3

说明

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?"

题解

知识点:最短路。

题目要求还需要多少对关系才能知道全部关系,根据不等式传递性,我们很容易知道用floyd处理传递闭包,剩下没有被传递到的就是最终需要的答案。

需要注意的是,最终答案是指未知关系的总数,而最终纳入的关系不能被考虑。例如,我不知道 \(1,5;1,4;1,3\) 三个关系,则答案是 \(3\) ,即便如果只要再知道 \(1,5\) 的关系就能知道其他所有关系,答案也不是 \(1\) 而是 \(3\) 。

最后,这里用了 bitset 优化。

时间复杂度 \(O(n^3+m)\)

空间复杂度 \(O(n^2)\)

代码

#include <bits/stdc++.h>

using namespace std;

bitset<1007> g[1007];

int main() {
std::ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int n, m;
cin >> n >> m;
for (int i = 1;i <= m;i++) {
int u, v;
cin >> u >> v;
g[u][v] = 1;
}
for (int k = 1;k <= n;k++) {
for (int i = 1;i <= n;i++) {
if (g[i][k]) g[i] |= g[k];///i通过k中转能到的点
}
}
int ans = 0;
for (int i = 1;i <= n;i++) {
for (int j = i + 1;j <= n;j++) {
if (!g[i][j] && !g[j][i]) ans++;///注意,这里是朴素比较的此时,不是二分搜索的次数。后者需要上一步结果,而题目答案是在啥都不干的情况下的次数。
}
}
cout << ans << '\n';
return 0;
}

NC25064 [USACO 2007 Mar G]Ranking the Cows的更多相关文章

  1. USACO 2007 “March Gold” Ranking the Cows

    题目链接:https://www.luogu.org/problemnew/show/P2881 题目链接:https://vjudge.net/problem/POJ-3275 题目大意 给定标号为 ...

  2. NC25025 [USACO 2007 Nov G]Sunscreen

    NC25025 [USACO 2007 Nov G]Sunscreen 题目 题目描述 To avoid unsightly burns while tanning, each of the \(C\ ...

  3. Bzoj 1703: [Usaco2007 Mar]Ranking the Cows 奶牛排名 传递闭包,bitset

    1703: [Usaco2007 Mar]Ranking the Cows 奶牛排名 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 323  Solved ...

  4. poj 3275 "Ranking the Cows"(DFS or Floyd+bitset<>)

    传送门 题意: 农场主 FJ 有 n 头奶牛,现在给你 m 对关系(x,y)表示奶牛x的产奶速率高于奶牛y: FJ 想按照奶牛的产奶速率由高到低排列这些奶牛,但是这 m 对关系可能不能精确确定这 n ...

  5. NC24840 [USACO 2009 Mar S]Look Up

    NC24840 [USACO 2009 Mar S]Look Up 题目 题目描述 Farmer John's N (1 <= N <= 100,000) cows, convenient ...

  6. NC25043 [USACO 2007 Jan S]Protecting the Flowers

    NC25043 [USACO 2007 Jan S]Protecting the Flowers 题目 题目描述 Farmer John went to cut some wood and left ...

  7. poj_3275 Ranking the cows

    Ranking the cows Description Each of Farmer John's N cows (1 ≤ N ≤ 1,000) produces milk at a differe ...

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

    Ranking the Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 3301   Accepted: 1511 ...

  9. 便宜的回文 (USACO 2007)(c++)

    2019-08-21便宜的回文(USACO 2007) 内存限制:128 MiB 时间限制:1000 ms 标准输入输出 题目类型:传统 评测方式:文本比较 题目描述 追踪每头奶牛的去向是一件棘手的任 ...

  10. POJ3275 Ranking the Cows floyd的bitset优化

    POJ3275 Ranking the Cows #include <iostream> #include <cstdio> #include <bitset> u ...

随机推荐

  1. 解决在Edge浏览器中使用不了(找不到)new bing的情况

    1.问题 我们有时候看不到下图圈出部分的信息,无法找到New Bing的入口(这边是空的) 2.解决方式 1.选择右上角的三条杠,并选择其中的settings 2.将其中的country一项改为外国即 ...

  2. java - Array 数组反序输出

    通过Array类的静态 sort() 方法可实现对数组排序,sort() 方法提供了许多种重载形式,可对任意类型数组进行升序排序. 然而,Array类中没有类似 js 的 reverse   反序输出 ...

  3. 百度网盘(百度云)SVIP超级会员共享账号每日更新(2024.01.05)

    一.百度网盘SVIP超级会员共享账号 可能很多人不懂这个共享账号是什么意思,小编在这里给大家做一下解答. 我们多知道百度网盘很大的用处就是类似U盘,不同的人把文件上传到百度网盘,别人可以直接下载,避免 ...

  4. 【面试题精讲】JVM中有哪些垃圾收集器

    有时博客内容会有变动,首发博客是最新的,其他博客地址可能未同步,请认准https://blog.zysicyj.top 首发博客地址 系列文章地址 在Java虚拟机(JVM)中,有以下几种常见的垃圾收 ...

  5. [转帖]Cgroups资源限制

    https://cloud.tencent.com/developer/article/2108816?areaSource=105001.13&traceId=QzVtWN5jGl8zeYZ ...

  6. [转帖]Windows磁盘性能压测(2)-Fio

    http://www.manongjc.com/detail/59-qftscgqzitmxpaw.html 目录 一.腾讯云官网硬盘性能指标 二.使用fio测试硬盘性能指标 1. 测试工具相关 2. ...

  7. [转帖]如何通过shell脚本对一个文件中的所有数值相加并求和

    https://developer.aliyun.com/article/886170?spm=a2c6h.24874632.expert-profile.255.7c46cfe9h5DxWK 1.背 ...

  8. [转帖]Springboot配置https访问

    https://www.cnblogs.com/feifuzeng/p/14709372.html 介绍 该篇博文主要介绍如何配置Springboot使其打包部署的服务必须通过HTTPS协议才可访问, ...

  9. 真·Android Zxing 扫码中文乱码解决

    Zxing3.2.1 之前遇到过Android Zxing扫码乱码的问题,网上搜了下解决了下. 今天又遇到了问题. 依然会乱码. 研究总结如下: Zxing 可以在Hints中添加默认编码格式.这个编 ...

  10. vue3.2如何将写hooks呢?

    场景 有些时候,我们需要将一个页面拆分成各个模块. 这些模块包含增加,删除,修改,等 并且这些模块会处理非常复杂的业务逻辑问题 所以,我们最好是将他们分开. 如何将分离新增模块拆离出去 主页面 < ...