fork(1) download
  1. #include <iostream>
  2. #include <vector>
  3. #include <tuple>
  4.  
  5. int main() {
  6.  
  7. // V = number of vertices
  8. // E = number of edges
  9. int V, E;
  10. int x, y, w;
  11. std::cin >> V >> E;
  12.  
  13. std::vector <std::tuple <int, int, int> > edge_list_directed;
  14. std::vector <std::tuple <int, int, int> > edge_list_undirected;
  15.  
  16. // assuming 0-based indexing of vertices
  17. for(int i = 0; i < E; i++) {
  18. std::cin >> x >> y >> w;
  19. edge_list_directed.push_back((std::make_tuple(x, y, w))); // adding a single edge in directed
  20.  
  21. edge_list_undirected.push_back((std::make_tuple(x, y, w)));
  22. edge_list_undirected.push_back((std::make_tuple(y, x, w))); // adding both the edges in undirected
  23. }
  24.  
  25. // printing graph (showing only undirected but the code will be same for both)
  26. for(int i = 0; i < edge_list_undirected.size(); i++) {
  27. std::cout << "{" << std::get<0>(edge_list_undirected[i]) << ", "
  28. << std::get<1>(edge_list_undirected[i]) << ", "
  29. << std::get<2>(edge_list_undirected[i]) << "}" << std::endl;
  30. }
  31. return 0;
  32. }
Success #stdin #stdout 0s 16064KB
stdin
3 3
0 1 10
1 2 15
0 2 20
stdout
{0, 1, 10}
{1, 0, 10}
{1, 2, 15}
{2, 1, 15}
{0, 2, 20}
{2, 0, 20}