fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. int n, m;
  6. cout << "N: ";
  7. cin >> n;
  8. cout << "M: ";
  9. cin >> m;
  10.  
  11. vector<int> g[n]; // Create adjacency list for n nodes
  12.  
  13. cout << "Taking input of undirected graph:\n";
  14. for (int i = 0; i < m; i++) {
  15. int x, y;
  16. cin >> x >> y;
  17. g[x].push_back(y);
  18. g[y].push_back(x);
  19. }
  20.  
  21. cout << "Node | Degree\n";
  22. for (int i = 0; i < n; i++) {
  23. cout << i << " -> " << g[i].size() << "\n";
  24. }
  25.  
  26. return 0;
  27. }
  28.  
Success #stdin #stdout 0.01s 5288KB
stdin
5
4
0 1
1 2
2 3
2 4
stdout
N: M: Taking input of undirected graph:
Node | Degree
0 -> 1
1 -> 2
2 -> 3
3 -> 1
4 -> 1