POJ-3660 Cow Contest

#include <cstdio>
#include <cstring>

using namespace std;

const int n_max = 100 + 5;
const int m_max = 4500 + 5;
int graph[n_max][n_max];
int n, m;
int a, b, cnt, ans;

void Floyd()
{

    for(int k = 1; k <= n; k ++)
        for(int i = 1; i <= n; i ++)
            for(int j = 1; j <= n; j ++)
                graph[i][j] = graph[i][j] || (graph[i][k] && graph[k][j]);
}

int main()
{
    while(~scanf("%d %d", & n, & m))
    {
        memset(graph, 0, sizeof(graph));
        for(int i = 0; i < m; i ++)
        {
            scanf("%d %d", & a, & b);
            graph[a][b] = 1;
        }
        Floyd();
        ans = 0;
        for(int i = 1; i <= n; i ++)
        {
            cnt = 0;
            for(int j = 1; j <= n; j ++)
                if(graph[i][j] || graph[j][i])
                    cnt ++;
            if(cnt == n - 1)
                ans ++;
        }
        printf("%d\n", ans);
    }
}

题目

有n头牛比赛,m种比赛结果,最后问你一共有多少头牛的排名被确定了,其中如果a战胜b,b战胜c,则也可以说a战胜c,即可以传递胜负。

题解:

用Floyd算法比较简单。就是求每个点与其他点是否连通。如果 入点的个数 加上 出点的个数 等于 n - 1,则说明这个点的排名已经确定。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值