• POJ3275 Ranking the Cows题解


    POJ3275 Ranking the Cows题解

    题目

    链接

    http://poj.org/problem?id=3275

    字面描述

    Ranking the Cows
    Time Limit: 2000MS Memory Limit: 65536K
    Total Submissions: 3893 Accepted: 1754
    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?”
    Source

    USACO 2007 March Gold

    代码实现

    #include
    #include
    using namespace std;
    
    const int maxn=1e3+10;
    int n,m,ans;
    bitset<maxn>mp[maxn];
    int main(){
    	scanf("%d%d",&n,&m);
    	for(int i=1;i<=m;i++){
    		int x,y;
    		scanf("%d%d",&x,&y);
    		mp[x][y]=1;
    	}
    	for(int j=1;j<=n;j++){
    		for(int i=1;i<=n;i++){
    			if(mp[i][j])mp[i]|=mp[j];
    		}
    	}
    	for(int i=1;i<=n;i++){
    		for(int j=1;j<=n;j++){
    			if(mp[i][j])ans++;
    		}
    	}
    	printf("%d\n",n*(n-1)/2-ans);
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
  • 相关阅读:
    vue中常用的修饰符
    【Go事】一眼看穿 Go 的集合和切片
    每日三题 11.03
    《数理统计》第4章
    达梦数据库安装和使用
    【Linux开发】如何在Ubuntu系统创建新用户,并且用MobaXterm连接linux服务器,创建session?
    C++——编译和链接原理笔记
    项目管理【引论二】项目管理的逻辑
    黑白影片智能上色,复原历史重现经典
    java-php-net-python-口红专卖网站计算机毕业设计程序
  • 原文地址:https://blog.csdn.net/weixin_42178241/article/details/125970696