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. Codeforces 185A Plant( 递推关系 + 矩阵快速幂 )

    链接:传送门 题意:输出第 n 年向上小三角形的个数 % 10^9 + 7 思路: 设 Fn 为第 n 年向上小三角形的个数,经过分析可以得到 Fn = 3 * Fn-1 + ( 4^(n-1) - ...

  2. [LUOGU]2016 Sam数

    我本来想看看SAM,就看见了这个.. 这道题很容易让人想到数位DP,用\(f[i][j]\)表示考虑到第\(i\)位,最后一位是\(j\)的方案数.看到1e18,直接矩阵快速幂加速,因为它每位转移都是 ...

  3. zabbbix4.0升级到4.2

    一.添加Zabbix存储库 1.安装存储库配置包. rpm -ivh https://repo.zabbix.com/zabbix/4.2/rhel/7/x86_64/zabbix-release-4 ...

  4. [luogu] P4105 [HEOI2014]南园满地堆轻絮 (贪心)

    P4105 [HEOI2014]南园满地堆轻絮 题目描述 小 Z 是 ZRP(Zombies' Republic of Poetry,僵尸诗歌共和国)的一名诗歌爱好者,最近 他研究起了诗词音律的问题. ...

  5. objective-c訪问控制符

    objective-c中成员变量的四个訪问控制符: @private:仅仅有当前类的内部才干訪问 @public:全部人都可訪问 @protected:仅仅限当前类和它的子类可以訪问 @package ...

  6. android 联系人中,在超大字体下,加入至联系人界面(ConfirmAddDetailActivity)上有字体显示不全的问题

    联系人(Contacts)中,在超大字体下.加入至联系人界面 (ConfirmAddDetailActivity)上有字母显示不全,如"j"等 这是android布局比較紧凑引起的 ...

  7. 同一个TextView设置不同的颜色和大小

    //strategy1是一个TextView SpannableStringBuilder builder1 = new SpannableStringBuilder(strategy1.getTex ...

  8. Android 中文字体的设置方法和使用技巧

    Android TextView字体颜色等样式具体解释连接:http://blog.csdn.net/pcaxb/article/details/47341249 1.使用字体库(自己定义字体的使用) ...

  9. hdoj 4548 美素数 【打表】

    另类打表:将从1到n的满足美素数条件的数目赋值给prime[n],这样最后仅仅须要用prime[L]减去prime[R-1]就可以: 美素数 Time Limit: 3000/1000 MS (Jav ...

  10. 关于Blog使用

    1.网易博客http://inowtofuture.blog.163.com/blog/#m=0 使用时间:2011年8月-2012年2月 记录内容:主要记录本科參加ACM期间在POJ(北京大学OJ) ...