fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. // your code goes here
  6. int n, m;
  7. cin >> n >> m;
  8. vector<vector<int>> matrix(n, vector<int>(n, 0));
  9.  
  10. for (int i = 1; i <= m; i++){
  11. int a, b;
  12. cin >> a >> b;
  13. matrix[a][b] = 1;
  14. matrix[b][a] = 1;
  15. }
  16.  
  17. for (int i = 0; i < n; i++){
  18. int count = 0;
  19. for (int j = 0; j < n; j++){
  20. if(matrix[i][j] == 1) count++;
  21. }
  22.  
  23. cout << "The nodes connected to " << i << " is: " << count << endl;
  24. }
  25. return 0;
  26. }
Success #stdin #stdout 0.01s 5288KB
stdin
6 5
0 1
1 5
1 2
2 3
2 4
stdout
The nodes connected to 0 is: 1
The nodes connected to 1 is: 3
The nodes connected to 2 is: 3
The nodes connected to 3 is: 1
The nodes connected to 4 is: 1
The nodes connected to 5 is: 1