Median Weight Bead_floyd
Description
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 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
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的更多相关文章
- POJ1975 Median Weight Bead floyd传递闭包
Description There are N beads which of the same shape and size, but with different weights. N is an ...
- POJ-1975 Median Weight Bead(Floyed)
Median Weight Bead Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 3162 Accepted: 1630 De ...
- 珍珠 Median Weight Bead 977
描述 There are N beads which of the same shape and size, but with different weights. N is an odd numbe ...
- Median Weight Bead(最短路—floyed传递闭包)
Description There are N beads which of the same shape and size, but with different weights. N is an ...
- POJ 1975 Median Weight Bead
Median Weight Bead Time Limit: 1000ms Memory Limit: 30000KB This problem will be judged on PKU. Orig ...
- poj 1975 Median Weight Bead(传递闭包 Floyd)
链接:poj 1975 题意:n个珠子,给定它们之间的重量关系.按重量排序.求确定肯定不排在中间的珠子的个数 分析:由于n为奇数.中间为(n+1)/2,对于某个珠子.若有至少有(n+1)/2个珠子比它 ...
- 第十届山东省赛L题Median(floyd传递闭包)+ poj1975 (昨晚的课程总结错了,什么就出度出度,那应该是叫讨论一个元素与其余的关系)
Median Time Limit: 1 Second Memory Limit: 65536 KB Recall the definition of the median of elements w ...
- 别人整理的DP大全(转)
动态规划 动态规划 容易: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ...
- [转] POJ DP问题
列表一:经典题目题号:容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1191,1208, 1276, 13 ...
随机推荐
- javaSE之线程联合
首先定义 : 一个线程A在占有CPU资源期间 ,可以让其他线程调用join()和本线程联合. 嗯哈,像书本这个列子: 如: B.join(); 我们称A在运行期间联合了B, 如果线程A在占有CPU资源 ...
- Spring配置文件解析--依赖注入
1.构造器注入基于构造器的DI通过调用带参数的构造器来实现,每个参数代表着一个依赖.此外,还可通过给stattic工厂方法传参数来构造bean.构造器参数解析根据参数类型进行匹配,如果bean的构造器 ...
- ScrollView嵌套StackView提示需要宽度和高度限制
场景: 在一个xib的view中,添加一个ScrollView,再在这个ScrollView中添加一个StackView,StackView中不加控件(用代码动态加). 问题: 提示ScrollVie ...
- 20145236 《Java程序设计》 第十周学习总结
20145236 <Java程序设计> 第十周学习总结 Java网络编程 Java网络编程技术 Java语言是在网络环境下诞生的,所以Java语言虽然不能说是对于网络编程的支持最好的语言, ...
- 20145236 《Java程序设计》实验一实验报告
北京电子科技学院(BESTI)实验报告 课程:Java程序设计 班级:1452 指导教师:娄嘉鹏 实验日期:2016.04.08 实验名称:Java开发环境的熟悉(Linux + Eclipse) 实 ...
- [转载]SAP BASIS学习手册
原文地址:SAP BASIS学习手册作者:sapren 1:)要用scc4定义一个新的client,同时定义好类型(T,P,D等) 2:)用user/pasword: (sap*/pass) ...
- 关于gridview 实现查询功能的方法
protected void btnSearch_Click(object sender, EventArgs e) { TestCon(); } protected void btnAllData_ ...
- BZOJ4003 [JLOI2015]城池攻占
这题有两种做法来着... 第一种就是一开始想到的比较不靠谱,不过貌似可以过掉: 看从$1$号节点开始到$p$号节点最大需要的体力,记录单调上升的体力,询问的时候二分跳着走就可以了 不过精度问题还有可能 ...
- Sticks(poj1011/uva307)
题目大意: 乔治有一些碎木棒,是通过将一些相等长度的原始木棒折断得到的,给出碎木棒的总数和各自的长度,求最小的可能的原始木棒的长度:(就是将一些正整数分组,每组加起来和相等,使和尽可能小) 一开始做p ...
- 迭代输出Map和List<Map<String,Object>>的方法
一.Map<String,Object> String:key的类型 Object:value的类型,value可能是String,或者int类型,什么类型都可以 对于Map接口来说,本身 ...