POJ3660 Cow Contest (floyd传递闭包)

Cow Contest
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 14489 Accepted: 8119

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 ≤ NA ≠ 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

Source

题意应该还是比较显然的,给你若干场比赛的结果和胜利者,问你能确定几个排名。

需要的知识是传递闭包。仅作为个人理解 传递闭包: 关系之间具有传递性(例如a> b, b> c, 那么a> c), 在那些已给出的关系基础上, 通过传递性, 把所有可能的关系都找出来。

如果一头牛被x头牛打败,并且可以打败y头牛,如果x+y=n-1,则我们容易知道这头牛的排名就被确定了,所以我们只要将任一头牛,可以打败其他的牛的个数x, 和能打败该牛的牛的个数y求出来,在遍历所有牛判断一下是否满足x+y=n-1,就知道这个牛的排名是否能确定了(而传递闭包,正好将所有能得出关系都求出来了), 再将满足这个条件的牛数目加起来就是所求解。 x可以看成是入度, y是出度。

我们可以把floyd算法变形从原来的求最短路变成两点是否可以到达。

也就是说如果maze[i][k]=1(i、k可达)并且maze[k][j]=1(k、j可达)那么i和j可达。这样其实就做完了。

然后再遍历好了,如果有一个点从它出发到达的点和其他点到它的是所有的话,说明他的排名可以确定。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <string>
#include <iostream>
#include <stack>
#include <math.h>
#include <queue>
#include <vector>
#include <climits>
using namespace std;
const int  Max_M= 1e3 + 10;
const int inf = 0x01010101;
int N,M;
int maze[Max_M][Max_M];
void floyd()//floyd计算传递闭包
{
    for(int k = 1; k <= N; k++)
        for(int i = 1; i <= N; i++)
            for(int j = 1; j <= N; j++)
            {
                if(maze[i][k] == 1 && maze[k][j] == 1)
                    maze[i][j] = 1;
            }
}
int main()
{
    // freopen("out.txt","w",stdout);
    ios::sync_with_stdio(false);
    cin.tie(0);
    while(cin>>N>>M)
    {
        memset(maze,0,sizeof(maze));
        while(M--)
        {
            int a,b;
            cin>>a>>b;
            maze[a][b] = 1;
        }
        floyd();
        int cnt = 0;
        for(int i = 1; i <= N; i++)
        {
            int temp = 0;
            for(int j = 1; j <= N; j++)
                if(maze[i][j] || maze[j][i])
                    temp++;
            if(temp == N-1)
                cnt++;
        }
        cout<<cnt<<endl;
    }

    return 0;
}

当然你也可以用朴素的dfs,正向建图跑一遍记录所有能到达的结点。

再反向建图同样跑一遍,用一个visit数组记录是否到达了所有点即可。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <string>
#include <iostream>
#include <stack>
#include <math.h>
#include <queue>
#include <vector>
#include <climits>
using namespace std;
const int  Max_M= 1e3 + 10;
const int inf = 0x01010101;
int N,M;
int maze[Max_M][Max_M];
int rev_maze[Max_M][Max_M];
bool visit[Max_M];//记录一个结点能否到达其他点
bool used[Max_M];
void dfs(int graph[Max_M][Max_M],int s)
{
    used[s] = 1;
    for(int i = 1; i <= N; i++)
        if(graph[s][i] == 1 && used[i] == 0)
        {
            visit[i] = 1;
            dfs(graph,i);
        }


}
int main()
{
    // freopen("out.txt","w",stdout);
    ios::sync_with_stdio(false);
    cin.tie(0);
    while(cin>>N>>M)
    {
        memset(maze,0,sizeof(maze));
        memset(rev_maze,0,sizeof(rev_maze));
        while(M--)
        {
            int a,b;
            cin>>a>>b;
            maze[a][b] = 1;
            rev_maze[b][a] = 1;
        }

        int cnt = 0;
        for(int i = 1; i <= N; i++)
        {
            memset(visit,0,sizeof(visit));
            memset(used,0,sizeof(used));
            dfs(maze,i);
            memset(used,0,sizeof(used));
            dfs(rev_maze,i);
            visit[i] = 1;
            int j;
            for(j = 1; j <= N; j++)
                if(!visit[j])
                    break;
            if(j == N+1) cnt++;
        }
        cout<<cnt<<endl;
    }

    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值