Blue Bridge Cup training correlation matrix algorithm implemented in Python

Resource limitation
time limit: 1.0s memory limit: 512.0MB
Problem Description
  digraph please his output a correlation matrix of n nodes and m edges.
Input format
  of the first row of two integers n, m, the number of nodes and edges in FIG. FIG. n <= 100, m <= 1000.
  Next m lines of two integers a, b, there are shown in the drawings (a, b) side.
  Note that Fig may contain multiple edges, but no self-loop.
Output format
  output correlation matrix of the figure, note that do not change the order of nodes and edges.
Sample input
. 5. 9
. 1 2
. 3. 1
. 1. 5
2. 5
2. 3
2. 3
. 3 2
. 4. 3
. 5. 4
sample output
. 1 -1 0 0 0 0 0. 1 0
-1 0 0 -1. 1. 1. 1 0 0
0 0. 1 . 1 -1 -1 0 0 -1
0 0 0 -1 0 0 0 0. 1
0 0 -1 -1 0 0 0 0. 1

Analysis: The

title is very simple, mainly to pay attention to the output format, we look at the origin of the correlation matrix: the
Here Insert Picture Description
correlation matrix is easy to find out, but I can not always start AC, the answers are all digital, -1 is signed , 1 and 0 are omitted positive sign, we changed to use the characters in front of 1 and 0 are left a blank space AC.

#### AC Code:
while True:
    try:
        n,m = map(int,input().split())
        s = []
        result = [[' 0' for i in range(0,m+1)]for j in range(0,n+1)]  #要用字符0
        for i in range(m):
            s.append(list(map(int, input().split())))  # 完成输入
        for j in range(m):
            result[s[j][0]][j+1] = ' 1' #要用字符1
            result[s[j][1]][j+1] = '-1'  #要用字符-1
        for x in range(1,n+1):
            for y in range(1,m+1):
                print(result[x][y],'',end='')
            print('')
    except:
        break


Programming White record growth

Published 39 original articles · won praise 6 · views 1310

Guess you like

Origin blog.csdn.net/bianxia123456/article/details/104449056