zoukankan      html  css  js  c++  java
  • 算法训练 关联矩阵

    算法训练 关联矩阵  
    时间限制:1.0s   内存限制:512.0MB
        
    问题描述
      有一个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
    ------------------------------------
    样例输出中1代表出度,-1代表入度。
    ------------------------------------
    import java.util.Scanner;
    public class Main {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            Scanner sc=new Scanner(System.in);
            while(sc.hasNext()){
                int n=sc.nextInt();
                int m=sc.nextInt();
                int a[][]=new int[n][m];
                int fp=0,tp=0;
                for(int i=0;i<m;i++){
                    fp=sc.nextInt();
                    tp=sc.nextInt();
                    a[fp-1][i]=1;
                    a[tp-1][i]=-1;
                }
                for(int i=0;i<n;i++){
                    for(int j=0;j<m;j++){
                        System.out.print(a[i][j]+" ");
                    }
                    System.out.println();
                }
                
            }
            sc.close();
    
        }
    
    }
  • 相关阅读:
    序列化
    python_模块与包
    python_常用内置模块
    python_生成器
    python_文件操作
    你好,mysql
    2017年12月20日 内置对象
    2017年12月17日 ASP.NET 12个表单元素&&简单控件/复合控件
    2017年12月16日 ASP.NET基本用法
    2017年12月14日 LinQ高级查&&Asp.net WebForm Asp.net MVC
  • 原文地址:https://www.cnblogs.com/watchfree/p/5336266.html
Copyright © 2011-2022 走看看