POJ 3660 &&NYOJ 211 Cow Contest(Floyd傳遞閉包),poj3660

來源:互聯網
上載者:User

POJ 3660 &&NYOJ 211 Cow Contest(Floyd傳遞閉包),poj3660

連結:click here

題意:有N頭牛,每個牛有一個唯一且不同的能力等級值.然後他們中的兩頭牛進行M場比賽,並給你這M場的比賽結果.現在的問題是問你有多少頭牛可以確定自己的排名了? 如果對於a勝b且b勝c,那麼肯定a勝c. 且如果已經知道了a勝的牛數目+比a厲害的牛數目正好==N-1,那麼a的排名也肯定可以推出來.

思路:表示資料糾結中~~,突然看懂之後,然後想到:能打敗的個數 加上被打敗的個數恰好等於n-1,則能確定,否則,無法確定。

代碼:

#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;}  


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.