Advertisement
Guest User

Untitled

a guest
Apr 5th, 2020
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.33 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. /*
  5. #include <ext/pb_ds/assoc_container.hpp>
  6. #include <ext/pb_ds/tree_policy.hpp>
  7. using namespace __gnu_pbds;
  8.  
  9. typedef tree<int, null_type,
  10.         less_equal<int>,
  11.         rb_tree_tag,tree_order_statistics_node_update> ordered_mset;
  12.        
  13. typedef tree<int, null_type,
  14.         less<int>,
  15.         rb_tree_tag,tree_order_statistics_node_update> ordered_set;
  16. */
  17.  
  18. /*
  19.  
  20. PBDS
  21. -------------------------------------------------
  22.             0 based indexing
  23. -------------------------------------------------            
  24. 1) insert(value)
  25. 2) erase(value)
  26. 3) order_of_key(value) // Number of items strictly smaller than value
  27. 4) *find_by_order(k) : K-th element in a set (counting from zero)
  28.  
  29. */
  30.  
  31. typedef long long ll;
  32. typedef long double ld;
  33. typedef pair<int,int> pii;
  34. typedef pair<ll,ll> pll;
  35.  
  36. //mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());  
  37.  
  38. const int mx=1e5;
  39.  
  40. int color[mx];
  41. vector<int>g[mx];
  42.  
  43. void dfs(int u,bool flag,vector<pii> &edge)
  44. {
  45.     color[u]=1;
  46.     for(auto it: g[u])
  47.     {
  48.         if(!color[it])
  49.         {
  50.             if(flag) edge.push_back({u,it});
  51.             else edge.push_back({it,u});
  52.             dfs(it,flag^1,edge);
  53.         }
  54.     }
  55. }
  56.  
  57. /*
  58.  * maximum length of the path will always be 1
  59.  * say, u->v->w is an undirected path in dfs
  60.  * then in the modified graph it will look like u->v<-w
  61.  * see the following example:
  62.  *
  63.  *                1
  64.  *               / \        
  65.  *              2   3
  66.  *             / \ / \
  67.  *            4  5 6  7
  68.  *                 |
  69.  *                 8
  70.  *                 |           
  71.  *                 9   
  72.  *
  73.  * here the modified graph will look like
  74.  * 1->2, 1->3, 4->2, 5->2, 6->3, 7->3, 6->8, 9->8
  75.  *
  76.  * making the longest path length 1
  77. */
  78.  
  79. /* test case for the above graph:
  80. 1
  81. 9
  82. 1 2
  83. 1 3
  84. 2 4
  85. 2 5
  86. 3 6
  87. 3 7
  88. 6 8
  89. 8 9
  90. */
  91.  
  92. void solve()
  93. {
  94.     int n;
  95.     cin>>n;
  96.    
  97.     for(int i=1;i<=n;i++)
  98.     {
  99.         if(!g[i].empty()) g[i].clear();
  100.         color[i]=0;
  101.     }
  102.    
  103.     for(int i=1;i<n;i++)
  104.     {
  105.         int u,v;
  106.         cin>>u>>v;
  107.         g[u].push_back(v);
  108.         g[v].push_back(u);
  109.     }
  110.    
  111.     vector<pii>edges;
  112.    
  113.     dfs(1,true,edges);
  114.    
  115.     for(auto it: edges)
  116.     {
  117.         cout<<it.first<<" "<<it.second<<"\n";
  118.     }
  119. }
  120.    
  121.  
  122. int main()
  123. {
  124.     ios::sync_with_stdio(false);
  125.     cin.tie(0); cout.tie(0);
  126.    
  127.     int t;
  128.     cin>>t;
  129.    
  130.     for(int i=1;i<=t;i++)
  131.     {
  132.         cout<<"Case "<<i<<":\n";
  133.         solve();
  134.     }
  135.    
  136.     //cerr<<"\nTime elapsed: " << 1000.0 * clock() / CLOCKS_PER_SEC << " ms\n";
  137.    
  138.     return 0;
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement