POJ1975 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 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传递闭包的更多相关文章
- POJ-1975 Median Weight Bead(Floyed)
Median Weight Bead Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 3162 Accepted: 1630 De ...
- poj 1975 Median Weight Bead(传递闭包 Floyd)
链接:poj 1975 题意:n个珠子,给定它们之间的重量关系.按重量排序.求确定肯定不排在中间的珠子的个数 分析:由于n为奇数.中间为(n+1)/2,对于某个珠子.若有至少有(n+1)/2个珠子比它 ...
- POJ 1975 Median Weight Bead
Median Weight Bead Time Limit: 1000ms Memory Limit: 30000KB This problem will be judged on PKU. Orig ...
- Median Weight Bead(最短路—floyed传递闭包)
Description There are N beads which of the same shape and size, but with different weights. N is an ...
- 珍珠 Median Weight Bead 977
描述 There are N beads which of the same shape and size, but with different weights. N is an odd numbe ...
- 第十届山东省赛L题Median(floyd传递闭包)+ poj1975 (昨晚的课程总结错了,什么就出度出度,那应该是叫讨论一个元素与其余的关系)
Median Time Limit: 1 Second Memory Limit: 65536 KB Recall the definition of the median of elements w ...
- Median Weight Bead_floyd
Description There are N beads which of the same shape and size, but with different weights. N is an ...
- UVA 247 电话圈 (floyd传递闭包 + dfs输出连通分量的点)
题意:输出所有的环: 思路:数据比较小,用三层循环的floyd传递闭包(即两条路通为1,不通为0,如果在一个环中,环中的所有点能互相连通),输出路径用dfs,递归还没有出现过的点(vis),输出并递归 ...
- UVA 753 UNIX 插头(EK网络流+Floyd传递闭包)
UNIX 插头 紫书P374 [题目链接]UNIX 插头 [题目类型]EK网络流+Floyd传递闭包 &题解: 看了书之后有那么一点懂了,但当看了刘汝佳代码后就完全明白了,感觉他代码写的好牛逼 ...
随机推荐
- WPF的一些感悟
第一天在博客园写东西,只写一些自己对WPF稚嫩的理解和感悟. 1.Code Snippet代码简写工具 可以创建自己的代码模板管理器——>>>工具菜单,代码片段管理器 考出现有的+更 ...
- Kafka官方文档翻译——简介
简介 Kafka擅长于做什么? 它被用于两大类应用: 在应用间构建实时的数据流通道 构建传输或处理数据流的实时流式应用 几个概念: Kafka以集群模式运行在1或多台服务器上 Kafka以topics ...
- 委托(C#)
委托,delegate 关键字用于声明一个引用类型,该引用类型可用于封装命名方法或匿名方法.委托类似于 C++ 中的函数指针:但是,委托是类型安全和可靠的.委托类型声明的格式如下: public de ...
- 提高驾驶技术:用GAN去除(爱情)动作片中的马赛克和衣服
同步自我的知乎专栏:https://zhuanlan.zhihu.com/p/27199954 作为一名久经片场的老司机,早就想写一些探讨驾驶技术的文章.这篇就介绍利用生成式对抗网络(GAN)的两个基 ...
- 网络组Network Teaming
网络组team:是将多个网卡聚合在一起,从而实现容错和提高吞吐量 1 创建网络组接口 nmcli connection add type team con-name TEAMname ifname I ...
- Mybatis中使用 #{} 和 ${} 向sql传参时的区别
今天在工作时,使用MyBatis中向sql传递两个参数时,一直显示SQL语法错误,仔细检查,才发现传入的参数被加上了引号,导致传入的参数(要传入的参数是表名)附近出现语法错误. 错误写法: } a } ...
- 【LeetCode】152. Maximum Product Subarray
题目: Find the contiguous subarray within an array (containing at least one number) which has the larg ...
- Django+MySQL开发项目:内容管理系统cms(一)
Baker-Miller Pink被科学方法证实可以平静情绪并且抑制食欲的颜色,具有amazing的效果.基百里面说实验结果表明该颜色具有: "a marked effect on lowe ...
- Python开发简单爬虫(一)
一 .简单爬虫架构: 爬虫调度端:启动爬虫,停止爬虫,监视爬虫运行情况 URL管理器:对将要爬取的和已经爬取过的URL进行管理:可取出带爬取的URL,将其传送给“网页下载器” 网页下载器:将URL指定 ...
- 使用matplotlib绘制多轴图
一个绘图对象(figure)可以包含多个轴(axis),在Matplotlib中用轴表示一个绘图区域,可以将其理解为子图.上面的第一个例子中,绘图对象只包括一个轴,因此只显示了一个轴(子图).我们可以 ...