建图

建图

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<string.h>


int a[1001][1001];


int main(void)
{
    int m, n, i, j, x, y;


    while(~scanf("%d %d", &n, &m))
    {
        memset(a, 0, sizeof(a)); //此处注意二维数组是怎样初始化的


        for(i = 1; i <= m; i++)
        {
            scanf("%d %d", &x, &y);
            a[x][y] = 1;
        }


        for(i = 1; i <= n; i++)
        {
            for(j = 1; j <= n; j++)
            {
                printf("%d%c", a[i][j], j == n?'\n':' ');
            }
        }
    }


    return 0;
}

猜你喜欢

转载自blog.csdn.net/eider1998/article/details/80549721