zoukankan      html  css  js  c++  java
  • POJ3275 [USACO07MAR]Ranking the Cows

    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?"
     
    第一想法用拓扑排序玩传递闭包,毕竟是稀疏图
     1 #include<cstdio>
     2 #include<cstdlib>
     3 #include<stack>
     4 #include<bitset>
     5 #include<vector>
     6 using namespace std;
     7 const int N=1024;
     8 stack<int> S;
     9 bitset<N> a[N];
    10 vector<int> G[N];
    11 int n,m;
    12 int d[N];
    13 int read(){
    14     char c;int x=0,f=1;
    15     for(c=getchar();!isdigit(c);c=getchar())if(c=='-')f=-1;
    16     for(;isdigit(c);c=getchar())x=(x<<3)+(x<<1)+(c^48);
    17     return x*f;
    18 }
    19 void TopSort(){
    20     for(int i=0;i<n;++i)
    21         if(!d[i])S.push(i);
    22     while(!S.empty()){
    23         int u=S.top();S.pop();
    24         for(int i=0,i_end=G[u].size();i<i_end;++i){
    25             int v=G[u][i];
    26             a[v]|=a[u];
    27             --d[v];
    28             if(!d[v])S.push(v);
    29         }
    30     }
    31 }
    32 int main(){
    33     scanf("%d%d",&n,&m);
    34     while(m--){
    35         int u=read()-1,v=read()-1;
    36         G[u].push_back(v);
    37         ++d[v];
    38     }
    39     for(int i=0;i<n;++i)a[i][i]=1;
    40     TopSort();
    41     for(int i=0;i<n;++i)
    42         m+=a[i].count();
    43     printf("%d
    ",n*(n-1)/2-m-1+n);
    44     return 0;
    45 }

    就当是对ZJOI2017Day2的交代吧

  • 相关阅读:
    《Code Complete》第一部分纪要
    深入理解Java虚拟机-JVM内存管理的猜测
    成长经验系列之三-猜想-技术未来
    深入理解Java虚拟机-第三版-前言及第一章笔记
    float与double的精度问题
    成长经验系列之二-方法-成长分享
    工作可能用的一些网站(不定时更新)
    Walkthrough: Write your first client script
    Make a Field Required in a Dynamics CRM Dialog / PowerApps
    Refresh Power BI Dataset programmatically from Dynamics 365 CRM/PowerApps
  • 原文地址:https://www.cnblogs.com/ndqzhang1111/p/6915938.html
Copyright © 2011-2022 走看看