首页 > 其他分享 >NC25064 [USACO 2007 Mar G]Ranking the Cows

NC25064 [USACO 2007 Mar G]Ranking the Cows

时间:2023-01-03 22:36:17浏览次数:76  
标签:Ranking Mar cow int USACO will cows milk he

题目链接

题目

题目描述

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;
}

标签:Ranking,Mar,cow,int,USACO,will,cows,milk,he
From: https://www.cnblogs.com/BlankYang/p/17023537.html

相关文章

  • NC25005 [USACO 2008 Ope S]Clear And Present Danger
    题目链接题目题目描述FarmerJohnisonaboatseekingfabledtreasureononeoftheN(1<=N<=100)islandsconvenientlylabeled1..NintheCowribbeanSea......
  • USACO 2019 US Open Contest, Silver
    USACO2019USOpenContest,SilverT1.SleepyCowHerding这道题是个结论题我们先找出最大连续子段,如果全部连续,则不用计算求最大值,就尽量把所有的空位都踩一遍,我们......
  • NC24370 [USACO 2012 Dec S]Milk Routing
    题目链接题目题目描述FarmerJohn'sfarmhasanoutdatednetworkofMpipes(1<=M<=500)forpumpingmilkfromthebarntohismilkstoragetank.Hewants......
  • NC24755 [USACO 2010 Dec S]Apple Delivery
    题目链接题目题目描述Bessiehastwocrispredapplestodelivertotwoofherfriendsintheherd.Ofcourse,shetravelstheC(1<=C<=200,000)cowpaths......
  • NC24858 [USACO 2009 Nov S]Job Hunt
    题目链接题目题目描述Bessieisrunningoutofmoneyandissearchingforjobs.FarmerJohnknowsthisandwantsthecowstotravelaroundsohehasimposeda......
  • markdown语法
    #Markdown学习#标题:##二级标题###三级标题####四级标题#字体:**helloworld***helloworld****helloworld***~~helloworld~~#引用:狂神说java#分割线:**......
  • 第一篇blog“MarkDown学习”
    MarkDown学习 标题 字体点击每行字即可查看Hello,World!Hello,World!Hello,World!Hello,World!Hello,World! 引用">"+空格选择狂神说Java 分割线......
  • mariadb/mysql建立主从
    前提本方案是两节点主从方案,只要建立好主从,及时数据库挂掉又拉起主从模式不会失效。保证时间同步保证都安装了​​mysql/mariadb​​建立主从的过程这里介绍的是两节点主从......
  • 一种用markdown写PPT的方法,再也不用费劲排版了
    今天看​​jeremyxu的技术点滴​​​,发现分享了一个​​markdown​​​写​​PPT​​的插件,惊为天人,先来看看官方效果图。再看看jeremyxu写的效果,我学完了都没学会是......
  • MarkDown基本使用方法
    Markdown基本语法使用一级标题二级标题三级标题字体加粗斜体斜体加粗去除线引用引用语法的使用>>分割线图片本地图片网络图片超链接baidu列表......