问题描述
有一个n个结点m条边的有向图,请输出他的关联矩阵。
输入格式
第一行两个整数n、m,表示图中结点和边的数目。n<=100,m<=1000。
接下来m行,每行两个整数a、b,表示图中有(a,b)边。
注意图中可能含有重边,但不会有自环。
接下来m行,每行两个整数a、b,表示图中有(a,b)边。
注意图中可能含有重边,但不会有自环。
输出格式
输出该图的关联矩阵,注意请勿改变边和结点的顺序。
样例输入
5 9
1 2
3 1
1 5
2 5
2 3
2 3
3 2
4 3
5 4
1 2
3 1
1 5
2 5
2 3
2 3
3 2
4 3
5 4
样例输出
1 -1 1 0 0 0 0 0 0
-1 0 0 1 1 1 -1 0 0
0 1 0 0 -1 -1 1 -1 0
0 0 0 0 0 0 0 1 -1
-1 0 0 1 1 1 -1 0 0
0 1 0 0 -1 -1 1 -1 0
0 0 0 0 0 0 0 1 -1
0 0 -1 -1 0 0 0 0 1
package com.xjj.lanqiao;
import java.util.Scanner;
/*----关联矩阵-----
*
*
* */
public class Lq3_48 {
public static int n;
public static int m;
public static void main(String[] args) {
System.out.println();
Scanner scanner = new Scanner(System.in);
n = scanner.nextInt(); //节点数
m = scanner.nextInt(); //边数
int[][] a = new int[n][m]; //行,边;第n个节点对第m条边有连接
int[][] b = new int[m][2];
for(int i = 0; i < m; i++)
for(int j = 0; j < 2; j++){
b[i][j] = scanner.nextInt();
}
for(int i = 0; i < m; i++){
int x = b[i][0];
int y = b[i][1];
a[x-1][i] = 1; //第n的节点对第i条边有出边
a[y-1][i] = -1; //第n的节点对第i条边有进边
}
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
System.out.print(a[i][j] + " ");
}
System.out.println();
}
}
}