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. iOS应用架构现状分析

    iOS从2007年诞生至今已有近10年的历史,10年的时间对iOS技术圈来说足够产生相当可观的沉淀,尤其这几年的技术分享氛围无论国内国外都显得异常活跃.本文就iOS架构这一主题,结合开发圈里讨论较多的 ...

  2. 让ie678支持css一些属性及html标签

    昨天写的一个页面,用的css3及html5的一些样式与标签,在ie8下看是没有效果的,然后就在晚上查找了一下如何能让ie8也能实现这些效果. 1.添加respond.js文件,Respond.js让I ...

  3. Hadoop常见错误及处理方法

    1.Hadoop-root-datanode-master.log 中有如下错误: ERROR org.apache.hadoop.hdfs.server.datanode.DataNode: jav ...

  4. boxplot

    x1 = rand(20,6); x2 = .5+rand(20,6); x3 = randn(20,6); x = [x1;x2;x3]; x = x(:); g1 = [ones(size(x1) ...

  5. 登陆验证前对用户名和密码加密之后传输数据---base64加密

    以下这种方法是加密传输的简单实现 1,base64.js /** * * Base64 encode / decode * * */ function Base64() { // private pr ...

  6. Objective-C:Foundation框架-概述

    iOS的整体架构(以iOS8为例)图如下: 从Cocoa Touch到Core OS下面四层包含了开发iOS应用程序所用到的所有API(第三方框架也是基于这几个层的).每个层又都包含了许多框架.框架就 ...

  7. PHP 单引号和双引号的区别

    $a = 'jfdjaff';$b = '234125';$c = '"jj $a $b"'.PHP_EOL;echo $c;$c = 'jj $a $b'.PHP_EOL;ech ...

  8. eclipse-mysql-tomcat搭建jspk开发环境

    ...本来不想写,刚刚给女朋友又安了一次发现几乎忘了,还是记一下吧.. 1.默认安装好jdk以及eclipse或相关ide. 2.检查jdk环境变量是否配置成功:cmd下输入 java -versio ...

  9. PHP+socket游戏数据统计平台发包接包类库

    <?php /** * @title: PHP+socket游戏数据统计平台发包接包类库 * @version: 1.0 * @author: perry <perry@1kyou.com ...

  10. Could not find artifact com.sun:tools:jar:1.5.0

    问题: Failed to execute goal on project petroleum: Could not resolve dependencies for project petroleu ...