COJ 0979 WZJ data structure (negative 21)

Source: Internet
Author: User

WZJ data structure (minus 21)
Difficulty level: C; operating time limit: 5000ms; operating space limit: 262144KB; code length limit: 2000000B
Question Description

Ask you to implement a data structure that accomplishes such a function:

Give you a graph of n points, the initial state is boundless.

Add a two-way edge (U,V,W) each time, if the addition does not constitute a spanning tree, output "not yet", otherwise output the current minimum spanning tree weights.

Input
The first row of two positive integers n,m. Indicates that there are n points m operations.
Next m lines are three positive integers per line u,v,w.
Output
Add a two-way edge (U,V,W) each time, if the addition does not constitute a spanning tree, output "not yet", otherwise output the current minimum spanning tree weights.
Input example
4 6
1 2 10
2 3 10
3 4 10
2 2 1
1 3 2
2 4 3
Output example
Not yet
Not yet
30
30
22
15
Other Notes
1<=n<=100000
1<=m<=500000
1<=ui,vi<=n
1<=wi<=1000

The truth: This is the real dynamic MST.

1#include <iostream>2#include <cstdio>3#include <cmath>4#include <algorithm>5#include <queue>6#include <cstring>7 #definePAU Putchar (")8 #defineENT Putchar (' \ n ')9 #defineCH for (int d=0;d<=1;d++) if (Ch[d])Ten using namespacestd; One Const intmaxn=300000+Ten, maxm=1500000+Ten, inf=-1u>>1; A structnode{ -node*fa,*ch[2],*MX;intXBOOLRev; -Node () {fa=ch[0]=ch[1]=null;mx= This; x=0; rev=false;} the     voidRevt () {Swap (ch[0],ch[1]); rev^=1;return;} -     voidUpdate () {mx= This; ch{if(mx->x<ch[d]->mx->x) mx=ch[d]->mx;}return;} -     voidDown () {if(rev) {ch{ch[d]->revt ();} rev=false;}return;} -}lct[maxn+maxm],*nodecnt; + intParent (Node*x,node*&y) {return(Y=X-&GT;FA)? y->ch[0]==x?0: y->ch[1]==x?1:-1:-1;} - voidRotate (node*x) { +Node*y,*z;intD1=parent (x, y), d2=parent (y,z); A     if(y->ch[d1]=x->ch[d1^1]) y->ch[d1]->fa=y; aty->fa=x;x->fa=z;x->ch[d1^1]=y; -     if(d2!=-1) z->ch[d2]=x; -Y->update ();return; - } - voidPushdown (node*x) { -     StaticNODE*S[MAXN];inttop=0; in      for(Node*y;; x=y) { -S[top++]=x;y=x->FA; to         if(!y| | (y->ch[0]!=x&&y->ch[1]!=X)) Break; +} while(top--) S[top]->down ();return; - } theNode*splay (node*x) { *Pushdown (x); node*y,*z;intd1,d2; $      while(true){Panax Notoginseng         if(D1=parent (x, y)) <0) Break; -         if((D2=parent (y,z)) <0) {rotate (x); Break;} the         if(d1==D2) rotate (y), rotate (x); +         Elserotate (x), rotate (x); A} x->update ();returnx; the } +Node*access (node*x) { -node*ret=NULL; $      for(; x;x=x->fa) splay (x)->ch[1]=ret, (ret=x)update (); $     returnret; - } - voidMakeroot (intx) {access (X+LCT)->revt ();return;} the voidLinkintXintY) {makeroot (x); splay (X+LCT)->fa=lct+y;return;} - voidCutintXinty) {WuyiMakeroot (x); node*p= (Access (Y+LCT), splay (y+LCT)); thep->ch[0]=p->ch[0]->fa=null;p->update ();return; - } WuNode*findtop (intx) { -node*t= (Access (X+LCT), splay (X+LCT)); while(t->ch[0]) T->down (), t=t->ch[0];returnT; About } $Node*query (intXinty) { -Makeroot (x);returnAccess (Y+LCT),MX; - } - intn,m; AInlineintRead () { +     intx=0, sig=1;CharCh=GetChar (); the      while(!isdigit (CH)) {if(ch=='-') sig=-1; ch=GetChar ();} -      while(IsDigit (CH)) x=Ten*x+ch-'0', ch=GetChar (); $     returnx*=Sig; the } theInlinevoidWriteintx) { the     if(x==0) {Putchar ('0');return;}if(x<0) Putchar ('-'), x=-x; the     intlen=0, buf[ the]; while(x) buf[len++]=x%Ten, x/=Ten; -      for(inti=len-1; i>=0; i--) Putchar (buf[i]+'0');return; in } the intS[MAXM],T[MAXM]; the voidinit () { AboutN=read (); M=read ();intans=0, cnt=n;nodecnt=lct+n+1; the      for(intI=1; i<=m;i++){ theS[i]=read (); t[i]=read (); theNode*q=nodecnt++;q->x=read ();intk=q-Lct;makeroot (k); +         if(s[i]!=T[i]) { -         if(Findtop (s[i])! =findtop (T[i])) { theLink (s[i],k); link (t[i],k); ans+=q->x;cnt--;Bayi}Else{ theNode*p=query (S[i],t[i]);if(p->x>q->x) { theans+=q->x-p->x;intid=p-lct-N; -Cut (S[ID],P-LCT); Cut (t[id],p-LCT); - link (s[i],k); link (t[i],k);} the         } the         } the         if(cnt==1) write (ans), ENT; the         ElsePuts"Not yet"); -     }  the     return; the } the voidWork () {94     return; the } the voidprint () { the     return;98 } About intMain () {init (); work ();p rint ();return 0;}

COJ 0979 WZJ data structure (negative 21)

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.