POJ1975 Median Weight Bead

                                                                                   Median Weight Bead
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 2356   Accepted: 1182

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个BEADS,每个BEADS都有一个权值,将第(N+1)/2重的权值定义为中间权值。然后给出M组数据对如:a b,表示a的权值大于b的权值。依据这些可以排除一些BEADS肯定不可能是中间权值的BEADS。N为BEAD是的数目吗,且N为奇数。
        根据题意可以把数据对a,b表示成点a到点b的距离为1(是有向边),然后没有边相连的用MAXINT表示。最后用FLOYD就可以求出没对点之间的最短路径,只要最短路径小于MAXINT就说明可以确定这两个点之间的大小关系。再建立邻接矩阵的同时,建立反连接矩阵,同样使用FLOYD。依据矩阵1就可以求出比点I大的点的个数,依据矩阵2就可以求出比点I小的点个数。然后如果比I大或者小的点的个数超过了或者等于(N+1)/2,则表示点I不可能是中间权值的BEADS。
 
  1 #include <cstdlib>
  2 #include <iostream>
  3 #include <cstdio>
  4 #include <cstring>
  5 #include <cmath>
  6 #include <string>
  7 
  8 
  9 #define MAXINT 99999999
 10 
 11 using namespace std;
 12 
 13 int main(int argc, char *argv[])
 14 {
 15     
 16     int t;
 17     int i,j,k;
 18     int n,m;
 19     
 20     int data[100][100];
 21     
 22     int data2[100][100];
 23     
 24     int ncount=0;
 25     int isVis[100];
 26     
 27     
 28     
 29     scanf("%d",&t);
 30     while(t--)
 31     {
 32               
 33     scanf("%d%d",&n,&m);
 34     
 35     ncount=0;
 36     
 37     
 38     
 39     for(i=1;i<=n;i++)
 40     for(j=1;j<=n;j++)
 41     data2[i][j]=data[i][j]=MAXINT;
 42     
 43     
 44     for(i=1;i<=n;i++)
 45     isVis[i]=0;
 46     
 47     
 48     
 49     for(i=0;i<m;i++)
 50     {
 51                     int a,b;
 52                     scanf("%d%d",&a,&b);
 53                     
 54                     
 55                     data[a][b]=1;
 56                     
 57                     data2[b][a]=1;
 58                     
 59     }
 60     
 61     
 62     for(k=1;k<=n;k++)
 63     for(i=1;i<=n;i++)
 64     for(j=1;j<=n;j++)
 65     {
 66                      if(data[i][j]>data[i][k]+data[k][j])
 67                      data[i][j]=data[i][k]+data[k][j];
 68                      
 69                      if(data2[i][j]>data2[i][k]+data2[k][j])
 70                      data2[i][j]=data2[i][k]+data2[k][j];
 71                      
 72     }
 73     
 74     int midNum=(1+n)/2;
 75     
 76     
 77     for(i=1;i<=n;i++)
 78     {
 79                      int tmpNum=0;
 80                      
 81                      for(j=1;j<=n;j++)
 82                      {
 83                                       if(data[i][j]<MAXINT)
 84                                       tmpNum++;
 85                      }
 86                      
 87                      if((tmpNum>=midNum)&&(isVis[i]==0))
 88                      {ncount++;isVis[i]=1;}
 89     }
 90     
 91     
 92     for(i=1;i<=n;i++)
 93     {
 94                      int tmpNum=0;
 95                      
 96                      for(j=1;j<=n;j++)
 97                      {
 98                                       if(data2[i][j]<MAXINT)
 99                                       tmpNum++;
100                      }
101                      
102                      if((tmpNum>=midNum)&&(isVis[i]==0))
103                      {ncount++;isVis[i]=1;}
104     }
105     
106     
107     printf("%d\n",ncount);
108 }
109 
110 
111                     
112                     
113                     
114                     
115                     
116     
117     
118     
119     
120     
121     //system("PAUSE");
122     return EXIT_SUCCESS;
123 }

 

posted @ 2012-08-17 21:20  cseriscser  阅读(234)  评论(0编辑  收藏  举报