您的位置:首页 > 其它

POJ 3660 Cow Contest (floyd求联通关系)

2016-08-09 00:16 411 查看

Cow Contest

题目链接:

http://acm.hust.edu.cn/vjudge/contest/122685#problem/H

Description

N (1 ≤ N ≤ 100) cows, conveniently numbered 1..N, are participating in a programming contest. As we all know, some cows code better than others. Each cow has a certain constant skill rating that is unique among the competitors.

The contest is conducted in several head-to-head rounds, each between two cows. If cow A has a greater skill level than cow B (1 ≤ A ≤ N; 1 ≤ B ≤ N; A ≠ B), then cow A will always beat cow B.

Farmer John is trying to rank the cows by skill level. Given a list the results of M (1 ≤ M ≤ 4,500) two-cow rounds, determine the number of cows whose ranks can be precisely determined from the results. It is guaranteed that the results of the rounds will not be contradictory.


Input

Line 1: Two space-separated integers: N and M

Lines 2..M+1: Each line contains two space-separated integers that describe the competitors and results (the first integer, A, is the winner) of a single round of competition: A and B

Output

Line 1: A single integer representing the number of cows whose ranks can be determined

Sample Input

5 5

4 3

4 2

3 2

1 2

2 5

Sample Output

2

Hint

题意:

给出N个点,M个点对:

每条点对 A B 意味着A点的权值大于B点.

现在要对这些点进行权值排名,求有多少个点的排名能够确定.

题解:

将样例画一遍就比较容易看出来:

若某点跟其他n-1个点都联通,则这个点的排名可以确定. 否则不能.

问题就转换为了求n个点之间的联通关系.

而floyd算法正好可以求任意两点的联通关系,只需要把求最短路时的松弛操作修改一下即可.

dis[i][j] = dis[i][j] || (dis[i][k] && dis[k][j]);

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#define mid(a,b) ((a+b)>>1)
#define LL long long
#define maxn 110
#define inf 0x3f3f3f3f
#define IN freopen("in.txt","r",stdin);
using namespace std;

int n, m;
bool dis[maxn][maxn];

void floyd() {
for(int k=1; k<=n; k++)
for(int i=1; i<=n; i++)
for(int j=1; j<=n; j++)
dis[i][j] = dis[i][j] || (dis[i][k] && dis[k][j]);
}

int main(int argc, char const *argv[])
{
//IN;

while(scanf("%d %d", &n,&m) != EOF)
{
memset(dis, 0, sizeof(dis));
for(int i=1; i<=n; i++) dis[i][i] = 1;

for(int i=1; i<=m; i++) {
int u,v; scanf("%d %d", &u,&v);
dis[u][v] = 1;
}

floyd();

int ans = 0;
for(int i=1; i<=n; i++) {
int cnt1=0, cnt2=0;
for(int j=1; j<=n; j++) {
if(i == j) continue;
if(dis[i][j]) cnt1++;
if(dis[j][i]) cnt2++;
}
if(cnt1+cnt2 == n-1) ans++;
}

printf("%d\n", ans);
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: