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
Sample Input

 2

Sample Output

题意:

  有N个珠子,N为奇数,给出一些信息如a b表示a比b重,通过这些信息可以分析出那些珠子按重量排序后,哪个不可能是中间那个,求可以分析出几个。 如果a比b重,b比c重,则a比c重。

思路:

  和poj3660思路一样,如果确定有(n+1)/2 多个比这个重,或者比这个轻,则表示这个珠子一定不是中间那个。计算出度和入度,如果出度大于(n+1)/2 或者 入度大于 (n+1)/2 ,则表示这个不是中间。

代码:

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
int map[][];
int n,m;
void floyd()
{
for(int k=; k<=n; k++)
for(int i=; i<=n; i++)
for(int j=; j<=n; j++)
if(map[i][k]==&&map[k][j]==)//传递
map[i][j]=;
}
int main()
{
int T;
cin>>T;
while(T--)
{
cin>>n>>m;
memset(map,,sizeof(map));
for(int i=; i<m; i++)
{
int a,b;
cin>>a>>b;
map[a][b]=;
}
floyd();
int ans=;
for(int i=; i<=n; i++)
{
int d=,x=;
for(int j=; j<=n; j++)
{
if(map[i][j])//计算出度
d++;
else if(map[j][i])//计算入度
x++;
}
if(d>=(n+)/||x>=(n+)/)//出度或者入度其中有一个大于(n+1)/2就能证明不是中间
ans++;
}
cout<<ans<<endl;
}
}

POJ1975 Median Weight Bead floyd传递闭包的更多相关文章

  1. POJ-1975 Median Weight Bead(Floyed)

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

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

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

  3. POJ 1975 Median Weight Bead

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

  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. 珍珠 Median Weight Bead 977

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

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

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

  7. Median Weight Bead_floyd

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

  8. UVA 247 电话圈 (floyd传递闭包 + dfs输出连通分量的点)

    题意:输出所有的环: 思路:数据比较小,用三层循环的floyd传递闭包(即两条路通为1,不通为0,如果在一个环中,环中的所有点能互相连通),输出路径用dfs,递归还没有出现过的点(vis),输出并递归 ...

  9. UVA 753 UNIX 插头(EK网络流+Floyd传递闭包)

    UNIX 插头 紫书P374 [题目链接]UNIX 插头 [题目类型]EK网络流+Floyd传递闭包 &题解: 看了书之后有那么一点懂了,但当看了刘汝佳代码后就完全明白了,感觉他代码写的好牛逼 ...

随机推荐

  1. 转化来的图标用法symbol引用‘font-class引用及Unicode引用

  2. R语言重要数据集分析研究——  数据集本身的分析技巧

    数据集本身的分析技巧           作者:王立敏           文章来源:网络 1.数据集 数据集,又称为资料集.数据集合或资料集合,是一种由数据所组成的集合. Data set(或dat ...

  3. 关于JS数组的定义

    关于js数组的定义的一些内容: 数组是一个对象 只用一个变量,储存多个同类型的信息 数组--连续的储存空间 数组的下标从0开始 ps:定义一个数组可以看作是一个旅馆.里面有很多小房子. 1.创建数组- ...

  4. java 线程的死锁问题

    以下的情况可能出现死锁 1.一个对象的同步方法去调用另一个对象的同步方法,同时另一个对象的同步方法也在调用这个对象的同步方法,导致一定几率的死锁,不一定每次都会出现死锁,模拟的代码如下 package ...

  5. mongodb取出最大值与最小值

    $res=self::aggregate([ ['$match'=>[ 'msg_id'=>1007, 'D'=>16, ]], ['$group'=>[ '_id'=> ...

  6. 【Android Developers Training】 27. 序言:和其它应用交互

    注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...

  7. 13.如何生成订单号,用uuid

    String orderNum = UUID.randomUUID().toString().replaceAll("-", "");

  8. Spring Boot框架的搭建

    一.优点: 1.简化了配置,是基于Spring4的一套快速开发整合包,减少复杂度 而Spring MVC基于Spring 的一个MVC框架 2.会有一个statrter整合包,减少样板代码 3.自动配 ...

  9. .NET C#到Java没那么难,MVC篇

    最典型的JAVA MVC就是JSP + servlet + javabean的模式.比较好的MVC,老牌的有Struts.Webwork.新兴的MVC 框架有Spring MVC.Tapestry.J ...

  10. Python库:序列化和反序列化模块pickle介绍

    1 前言 在“通过简单示例来理解什么是机器学习”这篇文章里提到了pickle库的使用,本文来做进一步的阐述. 通过简单示例来理解什么是机器学习 pickle是python语言的一个标准模块,安装pyt ...