• 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;
        }
    }
  • 相关阅读:
    扩展一些std::string未提供的常用方法
    Qt子线程中显示窗口部件的一个方法
    Qt子线程中通过QMetaObject::invokeMethod刷新UI控件
    “我的一剂良药”之开源指北
    源码解析之 Mybatis 对 Integer 参数做了什么手脚?
    必知必会面试题之 Spring 基础
    从一部电影史上的趣事了解 Spring 中的循环依赖问题
    Mysql、Oracle、SQL-Server 查询字段值长度
    Cross-Origin Read Blocking (CORB) blocked cross-origin response 问题
    MacOS11.0-brew 卡在Updating Homebrew
  • 原文地址:https://www.cnblogs.com/aiguona/p/7240565.html
Copyright © 2020-2023  润新知