Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define endl "\n"
- const int maxn=1e5+5;
- struct road
- {
- int y,w;
- road (int _y,int _w)
- {
- y=_y; w=_w;
- }
- };
- struct euler
- {
- int depth, node;
- euler(){};
- euler(int _node,int _depth)
- {
- node=_node;
- depth=_depth;
- }
- bool operator <(euler v)const
- {
- return depth<v.depth;
- }
- };
- euler e[4*maxn];///build euler path
- euler sparse[32][4*maxn];///store sparse tables
- int n,cnt;
- int first_encounter[maxn],cost[maxn],paw[32];
- int par[maxn],l[maxn],r[maxn],max_length[maxn];
- stack<road>st;
- vector<pair<int, int> >v[maxn];
- ///----------------------------------------------
- void pow_pow()
- {
- int i=0;
- paw[0]=1;
- for(int i=1;i<32;i++)
- {
- paw[i]=paw[i-1]*2;
- }
- }
- ///----------------------------------------------
- void init_dsu()
- {
- for(int i=0;i<=n;i++)
- {
- par[i]=i;
- l[i]=i;
- r[i]=i;
- }
- }
- ///----------------------------------------------
- void read()
- {
- cin>>n;
- for(int i=0;i<n-1;i++)
- {
- int b,e,w;
- cin>>b>>e>>w;
- v[b].push_back(make_pair(e,w));
- v[e].push_back(make_pair(b,w));
- st.push(road(b,e));
- }
- }
- ///----------------------------------------------
- void dfs(int k,int t,int p)
- {
- first_encounter[k]=t;
- e[cnt++]=euler(k,t);
- for(int i=0;i<v[k].size();i++)
- {
- int nb=v[k][i].first;
- int c=v[k][i].second;
- if(nb!=p)
- {
- cost[nb]=cost[k]+c;
- dfs(nb,t+1,k);
- e[cnt++]=euler(k,t);
- }
- }
- }
- ///----------------------------------------------
- int precompute()
- {
- int root=0;
- for(int i=1;i<n;i++)
- {
- if(v[root].size()<v[i].size())
- {
- root=i;
- }
- }
- return root;
- }
- ///----------------------------------------------
- void build_sparse()
- {
- for(int i=0;i<cnt;i++)
- {
- sparse[0][i]=e[i];
- }
- for(int i=1;(1<<i)<cnt;i++)
- {
- for(int j=0;j+(1<<i)-1<cnt;j++)
- {
- sparse[i][j]=min(sparse[i-1][j],sparse[i-1][j+(1<<i-1)]);
- //cout<<sparse[i][j].depth<<endl;
- }
- }
- }
- ///----------------------------------------------
- int find_par(int i)
- {
- if(par[i]==i)return i;
- return par[i]=find_par(par[i]);
- }
- ///----------------------------------------------
- int LCA(int y,int w)
- {
- int f=first_encounter[y];
- int s=first_encounter[w];
- if(f>s)swap(f,s);
- int j=(int)log2(s-f+1);
- return min(sparse[j][f],sparse[j][s-(1<<j)+1]).node;
- }
- ///----------------------------------------------
- int find_sum(int y,int w)
- {
- int an=LCA(y,w);
- //cout<<y<<" "<<w<<" "<<cost[y]<<" "<<cost[w]<<" "<<cost[an]<<endl;
- return (cost[y]-cost[an])+(cost[w]-cost[an]);
- }
- ///----------------------------------------------
- void compute_ans()
- {
- int y,w;
- stack<pair<int, int> >ans;
- while(!st.empty())
- {
- road nb=st.top();
- st.pop();
- y=nb.y; w=nb.w;
- int p1=find_par(y);
- int p2=find_par(w);
- if(max_length[p1]<max_length[p2])
- ans.push(make_pair(max_length[p1],max_length[p2]));
- else
- ans.push(make_pair(max_length[p2],max_length[p1]));
- if(p1!=p2)
- {
- cout<<"/**"<<endl;
- int sum1,sum2,sum3,sum4,sum5,sum6,xy;
- sum1=find_sum(l[p1],l[p2]);
- cout<<sum1<<" "<<l[p1]<<" "<<l[p2]<<endl;
- sum2=find_sum(l[p1],r[p2]);
- cout<<sum2<<" "<<l[p1]<<" "<<r[p2]<<endl;
- sum3=find_sum(r[p1],l[p2]) ;
- cout<<sum3<<" "<<r[p1]<<" "<<l[p2]<<endl;
- sum4=find_sum(r[p1],r[p2]);
- cout<<sum4<<" "<<r[p1]<<" "<<r[p2]<<endl;
- sum5=find_sum(l[p1],r[p1]);
- cout<<sum5<<" "<<l[p1]<<" "<<r[p1]<<endl;
- sum6=find_sum(l[p2],r[p2]);
- cout<<sum6<<" "<<l[p2]<<" "<<r[p2]<<endl;
- if(sum1>max_length[p1])
- {
- max_length[p1]=sum1;
- r[p1]=l[p2];
- }
- if(sum2>max_length[p1])
- {
- max_length[p1]=sum2;
- r[p1]=r[p2];
- }
- if(sum3>max_length[p1])
- {
- max_length[p1]=sum3;
- l[p1]=r[p1];
- r[p1]=l[p2];
- }
- if(sum4>max_length[p1])
- {
- max_length[p1]=sum4;
- l[p1]=r[p1];
- r[p1]=r[p2];
- //r[p1]=r[p2];
- }
- if(sum5>max_length[p1])
- {
- max_length[p1]=sum5;
- }
- if(sum6>max_length[p1])
- {
- max_length[p1]=sum6;
- r[p1]=r[p2];
- l[p1]=l[p2];
- }
- par[p2]=p1;
- }
- }
- while(!ans.empty())
- {
- cout<<ans.top().first<<" "<<ans.top().second<<endl;
- ans.pop();
- }
- }
- ///----------------------------------------------
- void solve()
- {
- init_dsu();
- pow_pow();
- int root=precompute();
- dfs(root,0,root);
- build_sparse();
- for(int i=0;i<4;i++)
- {
- for(int j=0;j<2*n;j++)
- {
- cout<<sparse[i][j].depth<<" ";
- }
- cout<<endl;
- }
- compute_ans();
- }
- ///----------------------------------------------
- int main()
- {
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- read();
- solve();
- return 0;
- }
- /*
- 5
- 1 2 2
- 2 3 1
- 2 4 2
- 1 5 3
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement