首页 > 代码库 > POJ 1975 Median Weight Bead
POJ 1975 Median Weight Bead
Median Weight Bead
Time Limit: 1000ms
Memory Limit: 30000KB
This problem will be judged on PKU. Original ID: 197564-bit integer IO format: %lld Java class name: Main
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.
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.
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.
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
15 42 14 35 14 2
Sample Output
2
Source
Tehran Sharif 2004 Preliminary
解题:传递闭包
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib>10 #include <string>11 #include <set>12 #include <stack>13 #define LL long long14 #define pii pair<int,int>15 #define INF 0x3f3f3f3f16 using namespace std;17 const int maxn = 101;18 bool g[maxn][maxn];19 int n,m;20 void Floyd() {21 for(int k = 1; k <= n; ++k) {22 for(int i = 1; i <= n; ++i) {23 if(g[i][k]) {24 for(int j = 1; j <= n; ++j) {25 if(!g[i][j]) g[i][j] = g[i][k]&&g[k][j];26 }27 }28 }29 }30 }31 int main() {32 int u,v,x,y,T;33 scanf("%d",&T);34 while(T--){35 scanf("%d %d",&n,&m);36 memset(g,false,sizeof(g));37 for(int i = 0; i < m; ++i){38 scanf("%d %d",&u,&v);39 g[u][v] = true;40 }41 Floyd();42 int ans = 0;43 for(int i = 1; i <= n; ++i){44 x = y = 0;45 for(int j = 1; j <= n; ++j){46 if(g[i][j]) x++;47 if(g[j][i]) y++;48 }49 if(x > (n-1)>>1 || y > (n-1)>>1) ans++;50 }51 cout<<ans<<endl;52 }53 return 0;54 }
POJ 1975 Median Weight Bead
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。