Mobile coding works. A laptop is faster for long sessions.
Problem
An undirected graph has `n` vertices numbered from `1` to `n` and `m` edges.
Read the edge list and print the adjacency matrix of the graph.
Recall that the adjacency matrix `A` of an undirected graph has
`A[i][j] = 1` if there is an edge between vertices `i` and `j`, and
`A[i][j] = 0` otherwise. The main diagonal contains only zeros
(the graph has no self-loops).
Input:
The first line contains two natural numbers `n` and `m`, separated by a space.
The following `m` lines each contain two natural numbers `x` and `y`
representing an edge between vertices `x` and `y`.
- `1 <= n <= 50`
- `0 <= m <= n*(n-1)/2`
- `1 <= x, y <= n`, `x != y`
- the graph contains no duplicate edges
Output:
The program prints the adjacency matrix, with values on the same row
separated by a single space, one matrix row per output line.
Example:
Input:
5 4
1 2
1 3
2 4
3 5
Output:
0 1 1 0 0
1 0 0 1 0
1 0 0 0 1
0 1 0 0 0
0 0 1 0 0
Stuck?
Use the Get Hint button in the action bar to reveal a guided hint.
💬 Discussion
Loading...💬
No comments yet. Be the first to start the discussion!
💻 No output yet.
Click Run in the editor to execute your code with the custom input.
Solve it your way first
Community solutions unlock after you submit a passing solution. Don't peek.
Ready to solve this challenge?
Create a free account to write code, submit solutions, and track your progress.
⌨️ Keyboard Shortcuts
Code Editor
Run Code
Ctrl
Enter
Submit Code
Ctrl
Shift
Enter
Format Code
Shift
Alt
F
Toggle Comment
Ctrl
/
Undo
Ctrl
Z
Redo
Ctrl
Y
Navigation
Global Search
/
Show Shortcuts
?
Close Modal
Esc