NOJ1119 仙林鼎山遊樂園,noj1119林鼎山

來源:互聯網
上載者:User

NOJ1119 仙林鼎山遊樂園,noj1119林鼎山
題意

給定一個有向無權圖,問圖中是否有環。

思路

用Floyd的形式進行預先處理。eg[i][j]不僅僅表示有一條邊連著i和j,而表示有一條i到j的路。
這樣處理之後遍曆所有的情況,如果發現正著和反著都可以,那就是存在環了。

代碼
#include <cstdio>const int maxn = 55;int n;bool eg[maxn][maxn];int main(){    //freopen("in.txt","r",stdin);    int t;    scanf("%d",&t);    while(t--) {        for(int i = 0 ; i < maxn ; i ++) {            for(int j = 0 ; j < maxn ; j ++) eg[i][j] = false;        }        int c;        scanf("%d%d",&n,&c);        while(c--) {            int a,b;            scanf("%d%d",&a,&b);            eg[a][b] = true;        }        for(int k = 0 ; k < n ; k ++) {            for(int i = 0 ; i < n ; i ++) {                for(int j = 0 ; j < n ; j ++) {                    if(eg[k][i] && eg[i][j]) eg[k][j] = true;//所有可能的路都已經顯示出來了                }            }        }        bool flag = false;        for(int i = 0 ; i < n ; i ++) {            for(int j = 0 ; j < n ; j ++) {                if(eg[i][j] && eg[j][i]) {                    flag = true;                    break;                }            }            if(flag) break;        }        if(flag) printf("YES\n");        else printf("NO\n");    }    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.