首页 技术 正文
技术 2022年11月23日
0 收藏 508 点赞 3,583 浏览 2349 个字

Median Weight Bead

Time Limit: 1000msMemory 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 解题:传递闭包 

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define pii pair<int,int>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = ;
bool g[maxn][maxn];
int n,m;
void Floyd() {
for(int k = ; k <= n; ++k) {
for(int i = ; i <= n; ++i) {
if(g[i][k]) {
for(int j = ; j <= n; ++j) {
if(!g[i][j]) g[i][j] = g[i][k]&&g[k][j];
}
}
}
}
}
int main() {
int u,v,x,y,T;
scanf("%d",&T);
while(T--){
scanf("%d %d",&n,&m);
memset(g,false,sizeof(g));
for(int i = ; i < m; ++i){
scanf("%d %d",&u,&v);
g[u][v] = true;
}
Floyd();
int ans = ;
for(int i = ; i <= n; ++i){
x = y = ;
for(int j = ; j <= n; ++j){
if(g[i][j]) x++;
if(g[j][i]) y++;
}
if(x > (n-)>> || y > (n-)>>) ans++;
}
cout<<ans<<endl;
}
return ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,023
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,513
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,361
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,143
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,774
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,853