NYOJ 211 闭包的传递 Floyd

来源:互联网 发布:淘宝店智能版 编辑:程序博客网 时间:2024/06/05 17:41

原题链接

题意:有N头牛,每个牛有一个唯一且不同的能力等级值.然后他们中的两头牛进行M场比赛,并给你这M场的比赛结果.现在的问题是问你有多少头牛可以确定自己的排名了? 如果对于a胜b且b胜c,那么肯定a胜c. 且如果已经知道了a胜的牛数目+比a厉害的牛数目正好==N-1,那么a的排名也肯定可以推出来.

思路:表示数据纠结中~~,突然看懂之后,然后想到:能打败的个数 加上被打败的个数恰好等于n-1,则能确定,否则,无法确定。

抽象为简单的floyd传递闭包算法,在加上每个顶点的出度与入度 (出度+入度=顶点数-1,则能够确定其编号)。

传递闭包的定义:

G的传递闭包定义为G*=(V,E*),其中E={(i,j):图G中存在一条从i到j的路径}。


#include <stdio.h>   //(传递闭包floyed算法)#include <string.h>#include <iostream>#include <algorithm>using namespace std;const int maxn=110;int map[maxn][maxn];int n, m;int main(){    int i, j, k, x, y,cnt1,cnt2;    while(~scanf("%d%d",&n,&m),n+m)    {        memset(map, 0, sizeof(map));        while(m--)        {            scanf("%d%d",&x,&y);            map[x][y] = 1;        }        for(k = 1; k <= n; k++)        {            for(i = 1; i <= n; i++)            {                for(j = 1; j <= n; j++)                {                    if(map[i][k]&& map[k][j])                    {                        // printf("$%d %d %d %d$",i,k,k,j);                        map[i][j] = 1;       //赋给关系                    }                }            }        }        int count = 0;        for(i = 1; i <= n; i++)        {            cnt1=0;            cnt2=0;            for(j = 1; j <= n; j++)            {                //if(i==j) continue;                //if(map[i][j]==0&&map[j][i]==0) break;                if(map[i][j])                {                    //printf("#i=%d j=%d#",i,j);                    cnt1++;                }                 if(map[j][i])                {                    //printf("#j=%d i=%d#",j,i);                    cnt2++;                }            }            if(cnt1+cnt2==(n-1))     //能打败的个数 加上被打败的个数恰好等于n-1            {                count++;            }        }        printf("%d\n", count);    }    return 0;}  




0 0
原创粉丝点击