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. pytorch 6 batch_train 批训练

    import torch import torch.utils.data as Data torch.manual_seed(1) # reproducible # BATCH_SIZE = 5 BA ...

  2. 封装HttpClient进行http请求与https请求

    一.https忽略证书 /** * 用于进行Https请求的HttpClient * * @author joey * */ public class SSLClient { public stati ...

  3. Linux下Makefile的automake生成全攻略

    作为Linux下的程序开发人员,大家一定都遇到过Makefile,用make命令来编译自己写的程序确实是很方便.一般情况下,大家都是手工写一个简单Makefile,如果要想写出一个符合自由软件惯例的M ...

  4. HDU 1521

    指数型生成函数.做这题时,回去看看组合数学才知道,指数生成函数求的就是多重集合的r排列数. #include <iostream> #include <cstdio> #inc ...

  5. JDBC-Statement 对象

    Statement 对象 一旦我们获得了数据库的连接,我们就可以和数据库进行交互.JDBC 的 Statement,CallableStatement 和 PreparedStatement 接口定义 ...

  6. 从头认识Spring-3.4 简单的AOP日志实现-扩展添加检查订单功能,以便记录并检測输入的參数

    这一章节我们再上一个章节的基础上加上一个检查订单功能 1.domain 蛋糕类: package com.raylee.my_new_spring.my_new_spring.ch03.topic_1 ...

  7. Linux操作系统是如何工作的

    <实验五——Linux操作系统是如何工作的?破解操作系统的奥秘> 姓名:方超 学号:SA12**6201 Linux操作系统工作的基础 存储程序计算机.堆栈(函数调用堆栈)机制和中断机制是 ...

  8. bzoj1022: [SHOI2008]小约翰的游戏John(博弈SG-nim游戏)

    1022: [SHOI2008]小约翰的游戏John 题目:传送门 题目大意: 一道反nim游戏,即给出n堆石子,每次可以取完任意一堆或一堆中的若干个(至少取1),最后一个取的LOSE  题解: 一道 ...

  9. m_Orchestrate learning system---二十二、html代码如何变的容易

    m_Orchestrate learning system---二十二.html代码如何变的容易 一.总结 一句话总结:(结构清晰之后构建页面就变得超级容易了)(多做多用一下子就熟了) 1.文章显示页 ...

  10. 在vmware下为oracle RAC 创建共享存储的总结

    首先,介绍下用命令行vm-diskmanager形式创建磁盘文件的方法(其实,图形界面添加新磁盘就是调用此命令).       很多网上文章提及plainmaker.exe去创建共享磁盘,是以前的版本 ...