fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm> // for sort
  4.  
  5. using namespace std;
  6.  
  7. struct edge {
  8. int dau, cuoi, w;
  9. };
  10.  
  11. vector<int> ke[1001];
  12.  
  13. int main() {
  14. int n, m;
  15. cin >> n >> m;
  16.  
  17. // Initialize variables before using them
  18. int x, y;
  19.  
  20. for(int i = 0; i < m; i++) {
  21. cin >> x >> y;
  22. ke[x].push_back(y);
  23. ke[y].push_back(x);
  24. }
  25.  
  26. for(int i = 1; i <= n; i++) {
  27. sort(ke[i].begin(), ke[i].end());
  28. cout << i << " : ";
  29. for(int neighbor : ke[i]) {
  30. cout << neighbor << " ";
  31. }
  32. cout << endl;
  33. }
  34.  
  35. return 0;
  36. }
  37.  
Success #stdin #stdout 0s 5304KB
stdin
5 4
2 5
4 1
4 2
4 3
stdout
1 : 4 
2 : 4 5 
3 : 4 
4 : 1 2 3 
5 : 2