【离散数学】 SDUT OJ 建图

建图

Time Limit: 1000 ms  Memory Limit: 65536 KiB

Problem Description

编程使得程序可以接受一个图的点边作为输入,然后显示出这个图。

Input

多组测试数据,对于每组测试数据,第一行输入正整数n(1 <= n <= 1000)和m,之后m行输入正整数x、y,表示在点x和点y之间存在有向边相连。

Output

对于每组测试数据,输出图的关系矩阵。

Sample Input

4 5
1 1
2 2
2 4
3 3
4 4

Sample Output

1 0 0 0
0 1 0 1
0 0 1 0
0 0 0 1

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MA 1010
int G[MA][MA];

int main()
{
    int n, m;
    while(~scanf("%d %d", &n, &m))
    {
        int x, y;
        int i, j;
        memset(G, 0, sizeof(G));
        while(m--)
        {
            scanf("%d %d", &x, &y);
            G[x][y] = 1;
        }
        for(i=1; i<=n; i++)
        {
            for(j=1; j<=n-1; j++)
            {
                printf("%d ", G[i][j]);
            }
            printf("%d\n", G[i][j]);
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/winner647520/article/details/80502093