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

 
解题:传递闭包
 
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define pii pair<int,int>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = ;
bool g[maxn][maxn];
int n,m;
void Floyd() {
for(int k = ; k <= n; ++k) {
for(int i = ; i <= n; ++i) {
if(g[i][k]) {
for(int j = ; j <= n; ++j) {
if(!g[i][j]) g[i][j] = g[i][k]&&g[k][j];
}
}
}
}
}
int main() {
int u,v,x,y,T;
scanf("%d",&T);
while(T--){
scanf("%d %d",&n,&m);
memset(g,false,sizeof(g));
for(int i = ; i < m; ++i){
scanf("%d %d",&u,&v);
g[u][v] = true;
}
Floyd();
int ans = ;
for(int i = ; i <= n; ++i){
x = y = ;
for(int j = ; j <= n; ++j){
if(g[i][j]) x++;
if(g[j][i]) y++;
}
if(x > (n-)>> || y > (n-)>>) ans++;
}
cout<<ans<<endl;
}
return ;
}

POJ 1975 Median Weight Bead的更多相关文章

  1. poj 1975 Median Weight Bead(传递闭包 Floyd)

    链接:poj 1975 题意:n个珠子,给定它们之间的重量关系.按重量排序.求确定肯定不排在中间的珠子的个数 分析:由于n为奇数.中间为(n+1)/2,对于某个珠子.若有至少有(n+1)/2个珠子比它 ...

  2. POJ-1975 Median Weight Bead(Floyed)

    Median Weight Bead Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 3162 Accepted: 1630 De ...

  3. POJ1975 Median Weight Bead floyd传递闭包

    Description There are N beads which of the same shape and size, but with different weights. N is an ...

  4. 珍珠 Median Weight Bead 977

    描述 There are N beads which of the same shape and size, but with different weights. N is an odd numbe ...

  5. Median Weight Bead(最短路—floyed传递闭包)

    Description There are N beads which of the same shape and size, but with different weights. N is an ...

  6. Median Weight Bead_floyd

    Description There are N beads which of the same shape and size, but with different weights. N is an ...

  7. poj 3579 Median (二分搜索之查找第k大的值)

    Description Given N numbers, X1, X2, ... , XN, let us calculate the difference of every pair of numb ...

  8. POJ 2409 Let it Bead(polya裸题)

    题目传送:http://poj.org/problem?id=2409 Description "Let it Bead" company is located upstairs ...

  9. POJ 3579 Median(二分答案)

    Median Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11599 Accepted: 4112 Description G ...

随机推荐

  1. 数据库常用sql语句积累

    组合一个新表 select p.*,(select value from as_info where key = 'v51_products') as v51_products from AP_POR ...

  2. 用Arcade表达式添加标签

    Arcade表达式是轻量级的脚本语言,我们可以通过全局变量$feature获取要素属性.比如说,要为城市添加标签,利用CITY_NAME列,我们可以编写语句:$feature.CITY_NAME.Ar ...

  3. [luogu]P4365[九省联考]秘密袭击coat(非官方正解)

    题目背景 警告:滥用本题评测者将被封号 We could have had it all. . . . . . 我们本该,拥有一切 Counting on a tree. . . . . . 何至于此 ...

  4. js 对象的创建方式和对象的区别

    js一个有三种方法创建对象,这里做一个总结. 1.对象直接量 所谓对象直接量,可以看做是一副映射表,这个方法也是最直接的一个方法,个人比较建议, 1 2 3 4 5 6 7 8 9 10 11 12 ...

  5. 表单标签 fieldset legent

    书写表单时可以提供简单样式的标签 <fieldset> <legent></legent> <input type="text" > ...

  6. DQL命令(查询)

     select *或字段1,字段2...     from 表名     [where 条件]       提示:*符号表示取表中所有列:没有where语句表示        查询表中所有记录:有wh ...

  7. VC6.0VB6.0 Scratch等软件

    VC6.0VB6.0 Scratch等软件 http://pan.baidu.com/s/1nv4hJrb

  8. ORA-38760: This database instance failed to turn on flashback database

    ORA-38760: This database instance failed to turn on flashback database 问题背景:        測试数据库运行shutdown ...

  9. HDU - 4758 Walk Through Squares (AC自己主动机+DP)

    Description   On the beaming day of 60th anniversary of NJUST, as a military college which was Secon ...

  10. centos下mysql多实例安装3306、3307实例(2014-10-15)

    背景说明       mysql的安装方法有多种,如二进制安装.源代码编译安装.yum安装等.yum安装仅仅能安装mysql 5.1 版本号:源代码安装编译的过程比較长.若没有对源代码进行改动且要求使 ...