POJ 1975 Median Weight Bead

Median Weight Bead

Time Limit: 1000ms

Memory Limit: 30000KB

This problem will be judged on PKU. Original ID: 1975
64-bit integer IO format: %lld      Java class name: Main

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

Source

Tehran Sharif 2004 Preliminary

解题:传递闭包

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <climits>
 7 #include <vector>
 8 #include <queue>
 9 #include <cstdlib>
10 #include <string>
11 #include <set>
12 #include <stack>
13 #define LL long long
14 #define pii pair<int,int>
15 #define INF 0x3f3f3f3f
16 using namespace std;
17 const int maxn = 101;
18 bool g[maxn][maxn];
19 int n,m;
20 void Floyd() {
21     for(int k = 1; k <= n; ++k) {
22         for(int i = 1; i <= n; ++i) {
23             if(g[i][k]) {
24                 for(int j = 1; j <= n; ++j) {
25                     if(!g[i][j]) g[i][j] = g[i][k]&&g[k][j];
26                 }
27             }
28         }
29     }
30 }
31 int main() {
32     int u,v,x,y,T;
33     scanf("%d",&T);
34     while(T--){
35         scanf("%d %d",&n,&m);
36         memset(g,false,sizeof(g));
37         for(int i = 0; i < m; ++i){
38             scanf("%d %d",&u,&v);
39             g[u][v] = true;
40         }
41         Floyd();
42         int ans = 0;
43         for(int i = 1; i <= n; ++i){
44             x = y = 0;
45             for(int j = 1; j <= n; ++j){
46                 if(g[i][j]) x++;
47                 if(g[j][i]) y++;
48             }
49             if(x > (n-1)>>1 || y > (n-1)>>1) ans++;
50         }
51         cout<<ans<<endl;
52     }
53     return 0;
54 }

时间: 2024-10-08 20:37:43

POJ 1975 Median Weight Bead的相关文章

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

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

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

NYOJ 280 LK的项链 &amp;&amp;POJ 2409 Let it Bead(polya 定理)

NYOJ 280 LK的项链  :click here POJ 2409 Let it Bead:click here 题意:一盒有红.蓝.绿三种颜色的珠子,每种颜色珠子的个数都大于24,现在LK想用这一盒珠子穿出一条项链,项链上的珠子个数为n(0<=n<=24),请你帮她计算一下一共可以用这一盒珠子可以穿出多少条不同的项链.通过旋转.翻转达到同一种状态的被认为是相同的项链. poj 上是c种颜色,s个珠子组成,数据比24小. 思路:今天刚接触到polya 定理: Polya定理:设G是n个对

POJ 2409 Let it Bead(Polya简单应用)

Let it Bead 大意:给你m种颜色,n个珠子串起来,旋转跟反转相同算相同,问有多少种不同的涂色组合方式. 思路:Polya的简单应用. 1 /************************************************************************* 2 > File Name: POJ2409.cpp 3 > Author: GLSilence 4 > Created Time: 2014年07月29日 星期二 22时56分58秒 5 **

[ACM] POJ 2409 Let it Bead (Polya计数)

Let it Bead Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4434   Accepted: 2916 Description "Let it Bead" company is located upstairs at 700 Cannery Row in Monterey, CA. As you can deduce from the company name, their business is b

poj 1286 Necklace of Beads &amp; poj 2409 Let it Bead(初涉polya定理)

http://poj.org/problem?id=1286 题意:有红.绿.蓝三种颜色的n个珠子,要把它们构成一个项链,问有多少种不同的方法.旋转和翻转后相同的属于同一种方法. polya计数. 搜了一篇论文Pólya原理及其应用看了看polya到底是什么东东,它主要计算全部互异的组合的个数.对置换群还是似懂略懂.用polya定理解决问题的关键是找出置换群的个数及哪些置换群,每种置换的循环节数.像这种不同颜色的珠子构成项链的问题可以把N个珠子看成正N边形. Polya定理:(1)设G是p个对象