SWUST OJ 1055: Adjacency Matrix to Adjacency List C++ Implementation

topic description

Assuming that the undirected graph G is stored in an adjacency matrix, write an algorithm to output the adjacency list.

enter

The first line is an integer n, indicating the number of vertices (the vertex number is 0 to n-1), and the next is an integer matrix of n*n size, indicating the adjacency relationship of the graph. A number of 0 means no adjacency, 1 means adjacency.

output

Output the adjacency list of graph G. The first row indicates the number of vertices directly reachable from vertex 0. The other lines are defined the same.
#include<bits/stdc++.h>
using namespace std;
int a[50][50], m;
int main(){
	cin>>m;
	for(int i = 0; i < m; i++) for(int j = 0; j < m; j++) cin>>a[i][j];
	for(int i = 0; i < m; i++){
		 for(int j = 0; j < m; j++) if(a[i][j] == 1) cout<<j;
		 cout<<endl;
	}
	return 0;
}

 

Guess you like

Origin blog.csdn.net/Ljy_Cxy/article/details/131465183