描述

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.

.    Bead  is heavier than Bead .
. Bead is heavier than Bead .
. Bead is heavier than Bead .
. Bead is heavier than Bead .

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.

输入

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.

输出

There should be one line per test case. Print the number of beads which can never have the medium weight.

样例输入


样例输出

2

来源

Tehran Sharif 2004 Preliminary

题解

翻译

这种题拿到的第一反应也就是交给谷歌之类的吧…

【问题描述】
有N颗形状和大小都一致的珍珠,它们的重量不相同。N为整数,所有的珍珠从1到N编号。

你的任务是发现哪颗珍珠的重量刚好处于正中间,即所有珍珠的重量中,该珍珠的重量列(N+1)/2位。

下面给出将一对珍珠进行比较的方法:
给你一架天平用来比较珍珠的重量,我们可以比出两个珍珠哪个更重一些,在作出一系列的比较后,我们可以将一些肯定不具备中间重量的拿走。
例如,下列给出对5颗珍珠进行四次比较的情况:
珍珠2比珍珠1重;
珍珠4比珍珠3重;
珍珠5比珍珠1重;
珍珠4比珍珠2重;
根据以上结果,虽然我们不能精确的的出哪个珍珠具有中间重量,但我们可以肯定珍珠1和珍珠4不可能具有中间重量,因为珍珠2,4,5比1重,而珍珠1,2,3比4轻,所以我们可以移走这两颗珍珠。
写一个程序统计出共有多少颗珍珠肯定不会是中间重量。
【输入格式】
输入第1行表示有多组测试用例,每组测试用例第1行包含两个用空格隔开的整数N和M,其中1<=N<=99,且N为奇数,M表示对珍珠进行比较的次数,接下来M行每行包含两个用空格隔开的整数x和y,表示珍珠x比y重。
【输出格式】
每组测试用例,输出仅一行,包含一个整数,表示不可能是中间重量的珍珠总数
【输入样例】
1
5 4
2 1
4 3
5 1
4 2
【输出样例】

2

解析

是一道Floyd算法可解的题。因为题目多源,故使用此算法。

首先需要明确,当有一半珍珠比它重或一半珍珠比它轻的时候,那这枚珍珠也就没什么可能是中间那颗了。

因此我们需要用Floyd算法为我们的邻接表做个排序,之后遍历统计比这枚珍珠重和轻的数量,再与半数Half相比即可。

 #include<iostream>
#include<cstring>
//Floyd
using namespace std;
const int MAXN=;
bool map[MAXN][MAXN];
int count[MAXN];
int main(){
int t;
scanf("%d",&t);
int n,m;
int a,b;
int half;
while(t--){
memset(map,,sizeof(map));
memset(count,,sizeof(count));
scanf("%d%d",&n,&m);
half=(n+)/;
for(int i=;i<=m;i++){
scanf("%d%d",&a,&b);
map[a][b]=true;//表示a珍珠比b珍珠要重
}
for(int k=;k<=n;k++){
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
if(k!=i&&i!=j&&j!=k){
if(map[i][k]&&map[k][j]){
map[i][j]=true;
}
}
}
}
}
for(int i=,flag=;i<=n;i++,flag=){
for(int j=;j<=n;j++){
if(map[i][j]){
count[i]++;
}
}
if(count[i]>=half){
count[]++;
}
else{
for(int j=;j<=n;j++){
if(map[j][i]){
flag++;
}
}
if(flag>=half){
count[]++;
}
}
}
printf("%d\n",count[]);
}
}

珍珠 Median Weight Bead 977的更多相关文章

  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

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

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

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

  4. POJ1975 Median Weight Bead floyd传递闭包

    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(传递闭包 Floyd)

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

  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. 第十届山东省赛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. Office365学习笔记—获取当前用户

    1,页面上有个_spPageContextInfo对象,可以获取一些我们需要的东西. (1)获取当前用户Id var userId=_spPageContextInfo.userId; (2)获取当前 ...

  2. 【Javascript-ECMA6-Fetch详解】

    Fetch 由于Fetch API是基于Promise设计,因此旧的浏览器并不支持该API,需要引用时引用es6-promise. 基本知识 fetch请求返回response格式 body Fetc ...

  3. Spring - 父容器与子容器

    一.Spring容器(父容器) 1.Mapper代理对象 2.Service对象 二.Springmvc(前端控制器)(子容器)        Controller对象 1.标准的配置是这样的:Con ...

  4. CSS 高级选择器

    相信各位如我一样的“抠图崽”和前端大佬们,在写网页样式的实话,总是免不了写下各种各样的选择器,再给选择的元素写入样式.最基本的元素选择器.class选择器.ID选择器等就不再过多的说了,相信大家都熟的 ...

  5. UCanCode发布升级E-Form++可视化源码组件库2018全新版 !

    2018年. 成都 UCanCode发布升级E-Form++可视化源码组件库2018全新版 ! --- 全面性能提升,UCanCode有史以来最强大的版本发布! E-Form++可视化源码组件库企业版 ...

  6. node 版本升级,版本管理,版本切换

    查看node版本 node -v 升级步骤 1,清除node缓存 sudo npm cache clean -f 2,安装n模块 sudo npm install -g n 3,升级到稳定版本 sud ...

  7. 虚拟机下linux 系统网卡配置、固定IP地址

    1.进入该目录下修改内容 vi       /etc/sysconfig/network-scripts/  ifcfg-eth0 TYPE=Ethernet BOOTPROTO=static DEF ...

  8. Java学习笔记二十四:Java中的Object类

    Java中的Object类 一:什么是Object类: Object类是所有类的父类,相当于所有类的老祖宗,如果一个类没有使用extends关键字明确标识继承另外一个类,那么这个类默认继承Object ...

  9. 浅谈fail-fast机制

    fail-fast机制即为快速失败机制,个人认为是一种防护措施,在集合结构发生改变的时候,使尽全力抛出ConcurrentModificationException,所以该机制大部分用途都是用来检测B ...

  10. 北京Uber优步司机奖励政策(2月26日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...