zoukankan      html  css  js  c++  java
  • POJ 1975 Median Weight Bead (Floyd)

    Median Weight Bead
    Time Limit: 1000MS   Memory Limit: 30000K
    Total Submissions: 2494   Accepted: 1249

    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 Input

    1
    5 4
    2 1
    4 3
    5 1
    4 2
    

    Sample Output

    2

    Source

     
     
    题意:有n个bead 给出m种轻重关系 求有多少人bead肯定不可能是 in the median weight

    思路: floyd 求出每个bead 与其它bead的关系 1表示比其它的重,-1表示轻于其它的。
    如果它重于其它的个数,或轻于其它的个数超过一半 那它就不可能是中间的
     
     
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    
    using namespace std;
    
    const int N=110;
    
    int map[N][N];
    
    int main(){
    
        //freopen("input.txt","r",stdin);
    
        int t,n,m;
        scanf("%d",&t);
        while(t--){
            scanf("%d%d",&n,&m);
            int a,b;
            int i,j,k;
            memset(map,0,sizeof(map));
            for(i=0;i<m;i++){
                scanf("%d%d",&a,&b);
                map[a][b]=1;
                map[b][a]=-1;
            }
            for(k=1;k<=n;k++)
                for(i=1;i<=n;i++)
                    for(j=1;j<=n;j++){
                        if(map[i][k]==1 && map[k][j]==1)
                            map[i][j]=1;
                        if(map[i][k]==-1 && map[k][j]==-1)
                            map[i][j]=-1;
                    }
            int ans=0;
            for(i=1;i<=n;i++){
                a=0,b=0;
                for(j=1;j<=n;j++){
                    if(map[i][j]==1)
                        a++;
                    if(map[i][j]==-1)
                        b++;
                }
                if(a>=(n+1)/2 || b>=(n+1)/2)
                    ans++;
            }
            printf("%d\n",ans);
        }
        return 0;
    }
  • 相关阅读:
    第10组 Beta冲刺(2/5)
    第10组 Beta冲刺(1/5)
    第10组 Alpha事后诸葛亮
    3月7-第十次机试课记录
    3月5-第九次机试课记录
    3月2-第八次机试课记录
    3月1-第六次机试课记录
    2月28-第五次机试课记录
    STL
    2月27-第四次机试课记录
  • 原文地址:https://www.cnblogs.com/jackge/p/3038313.html
Copyright © 2011-2022 走看看