fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. int n, e;
  6. cin >> n >> e;
  7.  
  8. // Create n x n matrix filled with 0
  9. vector<vector<int>> adj(n + 1, vector<int>(n + 1, 0));
  10.  
  11. // Read edges and fill matrix
  12. for (int i = 0; i < e; i++) {
  13. int u, v;
  14. cin >> u >> v;
  15. adj[u][v] = 1; // edge from u to v
  16. adj[v][u] = 1; // for undirected graph
  17. }
  18.  
  19. // Print adjacency matrix
  20. cout << "Adjacency Matrix:\n";
  21. for (int i = 1; i <= n; i++) {
  22. for (int j = 1; j <= n; j++) {
  23. cout << adj[i][j] << " ";
  24. }
  25. cout << endl;
  26. }
  27. }
Success #stdin #stdout 0.01s 5304KB
stdin
Standard input is empty
stdout
Adjacency Matrix: