Bzoj 3784: path on the tree

Source: Internet
Author: User

Description

Ask a tree forward \ (k\) The large path of the Benquan.

Sol

Divide and conquer the side.

Thank you very much for the data no Daisy diagram.

In order to write the side of the division to try and then opened the problem.

Edge Division is very good to think, choose a heavy edge, divided into two parts, and then to find the maximum value, each heavy edge of a heap maintenance, the global heap saved the answer.

Rebuild seems to write a problem ah qwq ... Crazy re more than ... In the end, no tree can be rebuilt directly.

Rebuild I just want to make him into a two-fork tree, each point of the degree of not more than 3 or 4 is almost enough.

Code
/************************************************************** problem:3784 User:beiyu language:c++ Result : Accepted time:4540 ms memory:26660 kb****************************************************************/#include &L T;bits/stdc++.h>using namespace Std; #define DEBUG (a) cout<< #a << "=" <<a<< "" #define MPR make_pairtypedef pair< int,int > PR; typedef pair< INT,PR > Prr;const int N = 100500; int N,k,rt,rtt;int D[n],sz[n],ud[n];int vis[n<<1]; int cnte;struct Edge {int to,w;} edge[n<<2];vector< int > G[n];p riority_queue< PR > qq;priority_queue< PR > q[n];vector< int &G T nxt[n];vector< int > Tmp[n][2];    inline int in (int x=0,char Ch=getchar ()) {while (ch> ' 9 ' | | ch< ' 0 ') Ch=getchar (); while (ch>= ' 0 ' && ch<= ' 9 ') x=x*10+ch-' 0 ', Ch=getchar (); return x; } void Addedge (int fr,int to,int W) {g[fr].push_back (cnte); edge[cnte++]= (Edge) {to,w};} void Print () {cout<< " StaRT "<<endl;        for (int i=1;i<=n;i++) {cout<<i<< "--"; for (int j=0;j< (int) g[i].size (); j + +) cout<< "ID:" <<g[i][j]<< "" <<edge[g[i][j]].to<        < "";    cout<<endl; } cout<< "-------------------------" &LT;&LT;ENDL;}    void ReBuild (int u,int fa) {int cnt=0,id=-1,lst=0,fst=0; if (G[u].size () >3) for (int i=g[u].size () -1,v;i>=1;i--) if ((v=edge[g[u][i]].to)!=FA) {if (cnt&1            ) {G[n].push_back (g[u][i]);                }else {g[++n].push_back (g[u][i]);                    if (LST) {Addedge (lst,n,0);                Addedge (n,lst,0);                }else {fst=n;            }lst=n;            } cnt^=1;            Edge[g[u][i]^1].to=n;        G[u].pop_back ();    }else Id=g[u][i],g[u].pop_back ();    if (id!=-1) g[u].push_back (ID);    if (FST) Addedge (u,fst,0), Addedge (fst,u,0); for (int i=0;i< (int) g[U].size (); i++) if (EDGE[G[U][I]].TO!=FA) ReBuild (edge[g[u][i]].to,u);    } void GetSize (int u,int fa) {d[u]=d[fa]+1,sz[u]=1; for (int i=g[u].size () -1,v;~i;i--) if ((v=edge[g[u][i]].to)!=fa &&!vis[g[u][i]]) {getsize (v,u), Sz[u]+=sz[v]    ; }}void getroot (int u,int nn) {rt=-1,rtt=n; GetSize (u,u);//for (int i=1;i<=n;i++) cout<<sz[i]<< "", cout<<endl;//for (int i=1;i<=n;i++)    cout<<d[i]<< "";cout<<endl;    queue< PR > Q;q.push (MPR (u,u)); for (PR x;! Q.empty ();)        {X=q.front (), Q.pop ();        int u=x.first,tmp; for (int i=0,v;i< (int) g[u].size (); i++) if ((v=edge[g[u][i]].to)!=x.second &&!vis[g[u][i]]) {if (D[U]&G            T;D[V]) Tmp=max (Sz[u],nn-sz[u]);            else Tmp=max (Sz[v],nn-sz[v]);//debug (U), Debug (v), Debug (TMP), Debug (g[u][i)) <<endl;            if (Tmp<rtt) rtt=tmp,rt=g[u][i];        Q.push (MPR (v,u)); }}//Debug (U), debug (RT) <<endl;} void Getdep (inT u,int fa,int w,vector< int > &vv) {vv.push_back (w); for (int i=0,v;i< (int) g[u].size (); i++) if ((v=edge[g[u][i]].to)!=fa &&!vis[g[u][i]) {GETDEP (v,u,w+edge[    G[U][I]].W,VV);    }} void Getans (int x,int nn) {vis[x]=vis[x^1]=1;    int u=edge[x].to,v=edge[x^1].to;         GETDEP (U,u,0,tmp[x][0]), GETDEP (v,v,0,tmp[x][1]);    Sort (Tmp[x][0].begin (), tmp[x][0].end (),greater< int > ());     Sort (Tmp[x][1].begin (), tmp[x][1].end (),greater< int > ()); cout<< "---------------------" <<endl;//debug (x), debug (NN), Debug (U), Debug (v) <<endl;//for (int i=0;i< (int) tmp[x][0].size (); i++) cout<<tmp[x][0][i]<< ""; cout<<endl;//for (int i=0;i< (int)          Tmp[x][1].size (); i++) cout<<tmp[x][1][i]<< "";cout<<endl;    Nxt[x].resize (Tmp[x][0].size ());         for (int i=0;i< (int) tmp[x][0].size (); i++) nxt[x][i]=0; for (int i=0;i< (int) tmp[x][0].size (); i++) Q[x].push (MPR (tmp[x][0][i]+tmp[x][1][0]+edGe[x].w,i));    Debug (TMP[X][0][I]+TMP[X][1][0]+EDGE[X].W)//debug (Q[x].top (). First) <<endl;         Qq.push (MPR (Q[x].top (). first,x));    if (D[u]<d[v]) swap (U,V);    int Ss=sz[u];    Getroot (U,SS);    if (rt!=-1) Getans (RT,SS);    Getroot (V,NN-SS); if (rt!=-1) Getans (RT,NN-SS);}    void Init () {n=in (), K=in ();     for (int i=1,u,v,w;i<n;i++) u=in (), V=in (), W=in (), Addedge (u,v,w), Addedge (V,U,W);     ReBuild (a);     Print ();         cout<< "Qwq" <<endl;     Getroot (1,n);         Debug (RT) <<endl;     Getans (Rt,n);    }void Del (int x) {PR r=q[x].top ();    Q[x].pop ();    int U=r.second; if (nxt[x][u]+1< (int) tmp[x][1].size ()) Nxt[x][u]++,q[x].push (MPR (TMP[X][0][U]+TMP[X][1][NXT[X][U]]+EDGE[X].W,    u)); Qq.push (MPR (Q[x].top (). first,x));}    int Query () {PR x=qq.top (); Qq.pop ();    int R=x.first,u=x.second;         Del (U); return r;}    int main () {//Freopen ("In.in", "R", stdin);//Ios::sync_with_stdio (FALSE);         Init (); FoR (; k--;)    {printf ("%d\n", Query ()); } return 0;}

  

Bzoj 3784: path on the tree

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.