Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- /*
- #include <ext/pb_ds/assoc_container.hpp>
- #include <ext/pb_ds/tree_policy.hpp>
- using namespace __gnu_pbds;
- typedef tree<int, null_type,
- less_equal<int>,
- rb_tree_tag,tree_order_statistics_node_update> ordered_mset;
- typedef tree<int, null_type,
- less<int>,
- rb_tree_tag,tree_order_statistics_node_update> ordered_set;
- */
- /*
- PBDS
- -------------------------------------------------
- 0 based indexing
- -------------------------------------------------
- 1) insert(value)
- 2) erase(value)
- 3) order_of_key(value) // Number of items strictly smaller than value
- 4) *find_by_order(k) : K-th element in a set (counting from zero)
- */
- typedef long long ll;
- typedef long double ld;
- typedef pair<int,int> pii;
- typedef pair<ll,ll> pll;
- //mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
- const int mx=1e5;
- int color[mx];
- vector<int>g[mx];
- void dfs(int u,bool flag,vector<pii> &edge)
- {
- color[u]=1;
- for(auto it: g[u])
- {
- if(!color[it])
- {
- if(flag) edge.push_back({u,it});
- else edge.push_back({it,u});
- dfs(it,flag^1,edge);
- }
- }
- }
- /*
- * maximum length of the path will always be 1
- * say, u->v->w is an undirected path in dfs
- * then in the modified graph it will look like u->v<-w
- * see the following example:
- *
- * 1
- * / \
- * 2 3
- * / \ / \
- * 4 5 6 7
- * |
- * 8
- * |
- * 9
- *
- * here the modified graph will look like
- * 1->2, 1->3, 4->2, 5->2, 6->3, 7->3, 6->8, 9->8
- *
- * making the longest path length 1
- */
- /* test case for the above graph:
- 1
- 9
- 1 2
- 1 3
- 2 4
- 2 5
- 3 6
- 3 7
- 6 8
- 8 9
- */
- void solve()
- {
- int n;
- cin>>n;
- for(int i=1;i<=n;i++)
- {
- if(!g[i].empty()) g[i].clear();
- color[i]=0;
- }
- for(int i=1;i<n;i++)
- {
- int u,v;
- cin>>u>>v;
- g[u].push_back(v);
- g[v].push_back(u);
- }
- vector<pii>edges;
- dfs(1,true,edges);
- for(auto it: edges)
- {
- cout<<it.first<<" "<<it.second<<"\n";
- }
- }
- int main()
- {
- ios::sync_with_stdio(false);
- cin.tie(0); cout.tie(0);
- int t;
- cin>>t;
- for(int i=1;i<=t;i++)
- {
- cout<<"Case "<<i<<":\n";
- solve();
- }
- //cerr<<"\nTime elapsed: " << 1000.0 * clock() / CLOCKS_PER_SEC << " ms\n";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement