zoukankan      html  css  js  c++  java
  • POJ1975 Median Weight Bead floyd传递闭包

    Description

    There are N beads which of the same shape and size, but with different weights. N is an odd number and the beads are labeled as 1, 2, ..., N. Your task is to find the bead whose weight is median (the ((N+1)/2)th among all beads). The following comparison has been performed on some pairs of beads: 
    A scale is given to compare the weights of beads. We can determine which one is heavier than the other between two beads. As the result, we now know that some beads are heavier than others. We are going to remove some beads which cannot have the medium weight. 

    For example, the following results show which bead is heavier after M comparisons where M=4 and N=5. 
    1.	Bead 2 is heavier than Bead 1.
    
    2. Bead 4 is heavier than Bead 3.
    3. Bead 5 is heavier than Bead 1.
    4. Bead 4 is heavier than Bead 2.

    From the above results, though we cannot determine exactly which is the median bead, we know that Bead 1 and Bead 4 can never have the median weight: Beads 2, 4, 5 are heavier than Bead 1, and Beads 1, 2, 3 are lighter than Bead 4. Therefore, we can remove these two beads. 

    Write a program to count the number of beads which cannot have the median weight. 

    Input

    The first line of the input file contains a single integer t (1 <= t <= 11), the number of test cases, followed by the input data for each test case. The input for each test case will be as follows: 
    The first line of input data contains an integer N (1 <= N <= 99) denoting the number of beads, and M denoting the number of pairs of beads compared. In each of the next M lines, two numbers are given where the first bead is heavier than the second bead. 

    Output

    There should be one line per test case. Print the number of beads which can never have the medium weight.
     
    Sample
    Sample Input
    1
    5 4
    2 1
    4 3
    5 1
    4 2

    Sample Output 2

    题意:

      有N个珠子,N为奇数,给出一些信息如a b表示a比b重,通过这些信息可以分析出那些珠子按重量排序后,哪个不可能是中间那个,求可以分析出几个。 如果a比b重,b比c重,则a比c重。

    思路:

      和poj3660思路一样,如果确定有(n+1)/2 多个比这个重,或者比这个轻,则表示这个珠子一定不是中间那个。计算出度和入度,如果出度大于(n+1)/2 或者 入度大于 (n+1)/2 ,则表示这个不是中间。

    代码:

    #include<stdio.h>
    #include<iostream>
    #include<algorithm>
    #include<cstring>
    using namespace std;
    int map[110][110];
    int n,m;
    void floyd()
    {
        for(int k=1; k<=n; k++)
            for(int i=1; i<=n; i++)
                for(int j=1; j<=n; j++)
                    if(map[i][k]==1&&map[k][j]==1)//传递
                        map[i][j]=1;
    }
    int main()
    {
        int T;
        cin>>T;
        while(T--)
        {
            cin>>n>>m;
            memset(map,0,sizeof(map));
            for(int i=0; i<m; i++)
            {
                int a,b;
                cin>>a>>b;
                map[a][b]=1;
            }
            floyd();
            int ans=0;
            for(int i=1; i<=n; i++)
            {
                int d=0,x=0;
                for(int j=1; j<=n; j++)
                {
                    if(map[i][j])//计算出度
                        d++;
                    else if(map[j][i])//计算入度
                        x++;
                }
                if(d>=(n+1)/2||x>=(n+1)/2)//出度或者入度其中有一个大于(n+1)/2就能证明不是中间
                    ans++;
            }
            cout<<ans<<endl;
        }
    }
  • 相关阅读:
    ES6(严格模式,let&const,箭头函数,解构赋值,字符串扩展方法,Symbol,Set和Map,生成器函数)
    动画实现-微信语音小喇叭样式
    JS与React分别实现倒计时(天时分秒)
    MacOS下如何设置hosts?
    原生JS实现‘点击元素切换背景及字体等’
    mysql数据库设计规范
    如何对 ElasticSearch 集群进行压力测试
    设计实现SAM--无服务器应用模型
    韩立刚计算机网络笔记-第11章 因特网上的音频视频-无线网络
    韩立刚计算机网络笔记-第10章 网络安全
  • 原文地址:https://www.cnblogs.com/aiguona/p/7240565.html
Copyright © 2011-2022 走看看