Advertisement
momo2345

directed undirected graph

Jan 6th, 2022
1,048
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.96 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define suni ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
  4. #define ll long long
  5. #define pb push_back
  6. #define F first
  7. #define S second
  8. #define endl "\n"
  9. //undirected
  10. const int mx=1e5+10;
  11. vector<int> adjm[mx];
  12. int main()
  13. {
  14.     suni;
  15.     int n,m;
  16.     cin>>n>>m;
  17.     for(int i=1; i<=m; i++)
  18.     {
  19.         int u,v;
  20.         cin>>u>>v;
  21.         adjm[u].pb(v);
  22.         adjm[v].pb(u);
  23.     }
  24.     for(int i=1; i<=n; i++)
  25.     {
  26.        cout<<i<<" ";
  27.         for(auto u : adjm[i]) cout<<u<<" ";
  28.         cout<<endl;
  29.     }
  30. }
  31.  
  32. //directed
  33. /*const int mx=1e5+10;
  34. vector<int> adjm[mx];
  35. int main()
  36. {
  37.     suni;
  38.     int n,m;
  39.     cin>>n>>m;
  40.     for(int i=1; i<=m; i++)
  41.     {
  42.         int u,v;
  43.         cin>>u>>v;
  44.         adjm[u].pb(v);
  45.         adjm[v].pb(u); //undirected;
  46.     }
  47.     for(int i=1; i<=n; i++)
  48.     {
  49.        cout<<i<<" ";
  50.         for(auto u : adjm[i]) cout<<u<<" ";
  51.         cout<<endl;
  52.     }
  53. }*/
  54.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement