Description

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

【题意】给出t个例子,有n个形状相同的bead,给出m个他们之间的轻重情况,找出不可能是中间质量的bead的数量

【思路】将轻重情况看成是一个有向图,i重于j就说明i到j有一条边,若i能到超过n/2个点或者i能被超过n/2个点到达,就说明i不是中间质量的bead

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
const int inf=0x3f3f3f3f;
const int N=;
int n,m;
int mp[N][N];
void floyd()
{
for(int k=;k<=n;k++)
{
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
if(mp[i][k]==&&mp[k][j]==)
mp[i][j]=;
if(mp[i][k]==-&&mp[k][j]==-)
mp[i][j]=-;
}
}
} }
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
memset(mp,,sizeof(mp));
scanf("%d%d",&n,&m);
for(int i=;i<=m;i++)
{
int a,b;
scanf("%d%d",&a,&b);
mp[a][b]=;
mp[b][a]=-;
}
floyd();
int ans=;
for(int i=;i<=n;i++)
{
int l=,r=;
for(int j=;j<=n;j++)
{
if(mp[i][j]==)
r++;
else if(mp[i][j]==-)
l++;
}
if(r>n/||l>n/)
ans++;
}
printf("%d\n",ans);
}
return ;
}

Median Weight Bead_floyd的更多相关文章

  1. POJ1975 Median Weight Bead floyd传递闭包

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

  2. POJ-1975 Median Weight Bead(Floyed)

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

  3. 珍珠 Median Weight Bead 977

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

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

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

  5. POJ 1975 Median Weight Bead

    Median Weight Bead Time Limit: 1000ms Memory Limit: 30000KB This problem will be judged on PKU. Orig ...

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

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

  7. 第十届山东省赛L题Median(floyd传递闭包)+ poj1975 (昨晚的课程总结错了,什么就出度出度,那应该是叫讨论一个元素与其余的关系)

    Median Time Limit: 1 Second Memory Limit: 65536 KB Recall the definition of the median of elements w ...

  8. 别人整理的DP大全(转)

    动态规划 动态规划 容易: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ...

  9. [转] POJ DP问题

    列表一:经典题目题号:容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1191,1208, 1276, 13 ...

随机推荐

  1. S2 第二章数据库的实现

    实现增删改查代码 1 select * from student --增加数据 insert into student (name,banji,xuehao) values(,) --修改数据 upd ...

  2. Touch ID集成

    作者感言 这个国庆由于种种原因, 过的不太安稳, 搬家, 办证, 东跑西跑, 忙的压根就不像是在过节....不过算了, 挑最后一天写写博文.最后:如果你有更好的建议或者对这篇文章有不满的地方, 请联系 ...

  3. JSON 换行、JSON \r\n、怎么处理 ?(转载)

    参考地址: http://www.cnblogs.com/mamingbo/archive/2010/11/27/1889583.html 在最后json的字符串上:.Replace("\r ...

  4. bug数量问题研究

    最近感觉很扯蛋的事情就是测试人员提bug的问题.先说下前提,公司测试会以提bug数量来做为一部分员工绩效的成份.再说一下公司从需求到开发 到测试,先是需求出一个文档,开发根据文档做功能的开发,然后测试 ...

  5. matlab c# 混合编程

    MWArray错误: matlab 64位 vs 32位 1. visual studio没有专门的64位版.但32位版可以在64位系统上面正常使用.2.安装VS2010的时候,在安装选项里面,选择了 ...

  6. Sharepoint2010突然之间不能打开页面,报503错误The service is unavailable

    原因:安装Sahrepoint时的账号出现故障,可能是密码过期等等. 解决方案: 新建windows用户ada,密码设置为永不过期,隶属于:administrators/IIS-WPG/WSS-WPG ...

  7. 3D MAX在立方体的使用

    3D  MAX不会“复用”立方体的顶点-----它直接计算该立方体需要12个三角面,每个三角面需要3个顶点,这样一共是36个顶点-----其实有大量顶点的位置是相同的,但3D  MAX不管这些.它认为 ...

  8. C#中三种定时器对象的比较

    ·关于C#中timer类 在C#里关于定时器类就有3个1.定义在System.Windows.Forms里2.定义在System.Threading.Timer类里3.定义在System.Timers ...

  9. 修改PE文件的入口函数OEP

    修改入口函数地址.这个是最省事的办法,在原PE文件中新增加一个节,计算新节的RVA,然后修改入口代码,使其指向新增加的节.当然,如果.text节空隙足够大的话,不用添加新节也可以. BOOL Chan ...

  10. POJ 1125 Stockbroker Grapevine 最短路 难度:0

    http://poj.org/problem?id=1125 #include <iostream> #include <cstring> using namespace st ...