離散題目18(傳遞閉包)

離散題目18
Time Limit: 1000MS Memory Limit: 65536KB
Submit Statistic
Problem Description

給出一個集合A和A上的關係R,求關係R的傳遞閉包。
例如:
A={0,1,2} , R={<0,0>,<1,0>,<2,2>,<1,2>,<2,1>}
t(R) = {<0,0>,<1,0>,<2,2>,<2,1>,<1,2>,<1,1>,<2,0>};
Input

多組輸入,輸入n、m,集合A={0, 1, …, n-1 };m代表關係的數量,n、m不超過20.
Output

每組輸入輸出t(R),根據t(R)中序偶的第一個數字升序排序,如果第一個數字相同,根據第二個升序排序。
Example Input

3 5
0 0
1 0
2 2
1 2
2 1
Example Output

0 0
1 0
1 1
1 2
2 0
2 1
2 2

沃舍爾算法

include <stdio.h>
include <stdlib.h>
include<string.h>

struct
{
    int x,y;
} s1[10000000]
;
int a[1000][1000];
int main()
{
    char s[10000];
    int i,j,f,n,k,t,m,b[1000],x,y;

    while(~scanf("%d%d",&n,&m))
    {
        memset(a,0,sizeof(a));
        f=1;
        for(i=0;i<m;i++)
        {
                scanf("%d%d",&x,&y);
                a[x][y]=1;
        }
        for(i=0;i<n;i++)
            for(j=0;j<n;j++)
        {
            if(a[j][i]>=1)
            {
                for(k=0;k<n;k++)
                {
                    a[j][k]=a[i][k]+a[j][k];
                }
            }
        }
        for(i=0;i<n;i++)
            for(j=0;j<n;j++)
            if(a[i][j]>=1)
            printf("%d %d\n",i,j);


    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章