Median Weight Bead_floyd

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=105;
int n,m;
int mp[N][N];
void floyd()
{
    for(int k=1;k<=n;k++)
    {
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                if(mp[i][k]==1&&mp[k][j]==1)
                    mp[i][j]=1;
                if(mp[i][k]==-1&&mp[k][j]==-1)
                    mp[i][j]=-1;
            }
        }
    }

}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
       memset(mp,0,sizeof(mp));
        scanf("%d%d",&n,&m);
        for(int i=1;i<=m;i++)
        {
            int a,b;
            scanf("%d%d",&a,&b);
            mp[a][b]=1;
            mp[b][a]=-1;
        }
        floyd();
        int ans=0;
        for(int i=1;i<=n;i++)
        {
            int l=0,r=0;
            for(int j=1;j<=n;j++)
            {
                if(mp[i][j]==1)
                    r++;
                else if(mp[i][j]==-1)
                    l++;
            }
            if(r>n/2||l>n/2)
                ans++;
        }
        printf("%d\n",ans);
    }
    return 0;
}
时间: 2024-08-25 01:10:25

Median Weight Bead_floyd的相关文章

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

链接:poj 1975 题意:n个珠子,给定它们之间的重量关系,按重量排序,求确定肯定不排在中间的珠子的个数 分析:因为n为奇数,中间为(n+1)/2,对于某个珠子,若有至少有(n+1)/2个珠子比它重或轻,则它肯定不排在中间 可以将能不能确定的权值初始化为0,能确定重量关系的权值设为1 #include<stdio.h> #include<string.h> int a[110][110]; int main() { int T,n,m,i,j,k,d,x,sum; scanf(

TOJ-1317 Median Weight Bead

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 compar

POJ1975:Median Weight Bead(FLOYD)

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 foll

POJ 1975 Median Weight Bead

Median Weight Bead Time Limit: 1000ms Memory Limit: 30000KB This problem will be judged on PKU. Original ID: 197564-bit integer IO format: %lld      Java class name: Main There are N beads which of the same shape and size, but with different weights.

toj 2282: Median Weight Bead (队列+vector)

描述 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 com

DP题目列表/弟屁专题

声明: 1.这份列表不是我原创的,放到这里便于自己浏览和查找题目. ※最近更新:Poj斜率优化题目 1180,2018,3709 列表一:经典题目题号:容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1191,1208, 1276, 1322, 1414, 1456, 1458, 1609, 1644, 1664, 1690, 1699, 1740, 1742, 1887, 1926, 1936, 195

[转] POJ DP问题

列表一:经典题目题号:容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1191,1208, 1276, 1322, 1414, 1456, 1458, 1609, 1644, 1664, 1690, 1699, 1740, 1742, 1887, 1926, 1936, 1952, 1953, 1958, 1959, 1962, 1975, 1989, 2018, 2029, 2039, 2063, 20

HOJ 题目分类

转自:http://blog.sina.com.cn/s/blog_65f3869301011a1o.html ******************************************************************************* 简单题(包括枚举,二分查找,(复杂)模拟,基础数据结构(栈.队列),杂题等 ****************************************************************************

CVPR论文《100+ Times Faster Weighted Median Filter (WMF)》的实现和解析(附源代码)。

四年前第一次看到<100+ Times FasterWeighted Median Filter (WMF)>一文时,因为他附带了源代码,而且还是CVPR论文,因此,当时也对代码进行了一定的整理和解读,但是当时觉得这个算法虽然对原始速度有不少的提高,但是还是比较慢.因此,没有怎么在意,这几天有几位朋友又提到这篇文章,于是把当时的代码和论文又仔细的研读了一番,对论文的思想和其中的实现也有了一些新的新的,再次做个总结和分享. 这篇文章的官网地址是:http://www.cse.cuhk.edu.h