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

    Median Weight Bead

    Time Limit: 1000ms
    Memory Limit: 30000KB
    This 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 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

     
    解题:传递闭包
     
     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 long
    14 #define pii pair<int,int>
    15 #define INF 0x3f3f3f3f
    16 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
  • 相关阅读:
    单例 全局变量
    平安亲人 测试数据
    UIButton
    apple id
    背景图的按钮
    大头针飘移问题
    图文并茂Windows系统使用XAMPP搭建本地mysql数据库导入数据库并使用node.js访问数据库
    JS原生上传文件,读取文件格式,控制文件只可以上传某些格式,并使用fileReader转换格式
    今天学到的新知识使用localtunnel实现内网穿透,感觉很神奇哇~~
    在Mac OS上将Node.js连接到XAMPP MySQL服务器一直报错error connecting: Error: connect ECONNREFUSED
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4071064.html
Copyright © 2011-2022 走看看