关联矩阵

来源:互联网 发布:阿尔法软件使用视频 编辑:程序博客网 时间:2024/04/30 19:09

首要问题,什么是关联矩阵,我是没有仔细看概念,但是我找到了规律,知道了怎么回事,先找到规律在看概念就知道怎么回事了,简单。

问题描述
  有一个n个结点m条边的有向图,请输出他的关联矩阵。
输入格式
  第一行两个整数n、m,表示图中结点和边的数目。n<=100,m<=1000。
  接下来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 -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
0 0 -1 -1 0 0 0 0 1



import java.util.*;  import java.math.*;public class Main{public static void main(String[] args){Scanner sc=new Scanner(System.in);int n=sc.nextInt();int m=sc.nextInt();int m1[][]=new int[n][m];for(int i=0;i<m;i++){int x1=sc.nextInt();int x2=sc.nextInt();m1[x1-1][i]=1;m1[x2-1][i]=-1;}for(int i=0;i<m1.length;i++){for(int j=0;j<m1[0].length;j++){System.out.print(m1[i][j]+" ");}System.out.println();}}}



0 0
原创粉丝点击