Poj 1975 median weight Bead

Source: Internet
Author: User
Median weight beadtime limit: 1000 msmemory limit: 30000 kbthis problem will be judged on PKU. Original ID: 1975
64-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 saved med 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.
  InputThe 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.
  OutputThere shoshould 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
SourceTehran Sharif 2004 preliminary problem solving: Passing Closure
 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 }
View code

 

Poj 1975 median weight Bead

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.