Bzoj 3669 [Noi2014] Magical forest LCT

Source: Internet
Author: User

Test Instructions:Link Method:Lct parsing:Water problem. Two-dimensional words are sorted according to the first dimension of experience. The second dimension then maintains the minimum spanning tree directly. It's too spicy to know what to say. Because when I write a sort of CMP, the hand shake is written if (a.a==b.b) return A.B lost 1 A chance my heart is broken! Code:
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#define N 50010#define M 100010#define K 150010#define INF 0x3f3f3f3fusing namespace Std;intNm, ans;intVal[k],Ma[k],fa[k],ch[k][2],RT[K],REV[K];intFffa[n];struct node{int x,y, A,b,tag;} EDGE[M];intCMP (node A,node b) {if(A.A==B.A)returna.b<b.b;returnA.A&LT;B.A;} void Pushup (int x){if(!x)return;Ma[x]=x;if(ch[x][0]!=0&&val[Ma[ch[x][0]]]>val[Ma[x]])Ma[x]=Ma[ch[x][0]];if(ch[x][1]!=0&&val[Ma[ch[x][1]]]>val[Ma[x]])Ma[x]=Ma[ch[x][1]];} voidReverse(int x){if(!x)return; Swap (ch[x][0],ch[x][1]); rev[x]^=1;} void Pushdown (int x){if(!x)return;if(rev[x])    {Reverse(ch[x][0]);Reverse(ch[x][1]); rev[x]^=1; }}void Down (int x){if(!rt[x]) Down (fa[x]); Pushdown (x);} void Rotate (int x){int y=fa[x],kind=ch[y][1]==x; ch[y][kind]=ch[x][!kind]; fa[ch[y][kind]]=y; ch[x][!kind]=y; fa[x]=fa[y]; fa[y]=x;if(rt[y]) rt[x]=1, rt[y]=0;Elsech[fa[x]][ch[fa[x]][1]==y]=x; Pushup (y);} void Splay (int x) {Down (x); while(!rt[x])    {int y=fa[x],z=fa[y];if(rt[y]) Rotate (x);Else if((ch[y][1]==x) = = (ch[z][1]==y)) Rotate (y), Rotate (x);ElseRotatex), Rotate (x); } pushup (x);} void Access (int x){int y=0; while(x) {splay (x); rt[ch[x][1]]=1, rt[y]=0; ch[x][1]=y; Pushup (x);y=x,x=fa[x]; }}void MoveToRoot (int x) {Access (x); Splay (x);Reverse(x);} voidLink(int x,int y) {MoveToRoot (x); fa[x]=y;} void Cut (int x,int y) {MoveToRoot (x); Accessy); Splay (y); ch[y][0]=0; fa[x]=0; rt[x]=1; Pushup (y);}intFind_root (int x) {Access (x), Splay (x); while(ch[x][0])x=ch[x][0];return x;}intQueryint x,int y){if(Find_root (x)!=find_root (y))return-1; MoveToRoot (x); Accessy); Splay (y);return Ma[y];} void Init () { for(intI=1; i<=n+m; i++) rt[i]=1,Ma[I]=i;}intMain () {ans=inf; scanf"%d%d",&n,&m); Init (); for(intI=1; i<=m; i++) {scanf ("%d %d%d%d", &edge[i].x, &edge[i].y, &edge[i].a,&edge[i].b);if(Edge[i].x==edge[i].y) edge[i].tag=1; }Sort(edge+1, edge+1+m, CMP);inttot=0; for(intI=1; i<=m; i++) {if(edge[i].tag==1)Continue;int x=edge[i].x,y=edge[i].y;if(Find_root (x)!=find_root (y)) {val[i+n]=edge[i].b;Link(x, I+n),Link(I+n,y); }Else{intT=query (x,y);if(Edge[i].b<val[t])                {val[i+n]=edge[i].b; Cut (Edge[t-n].x, T), Cut (T,edge[t-n].y);Link(x, I+n),Link(I+n,y); }        }intTmp=query (1, n);if(tmp!=-1) {ans=min (ans,edge[i].a+val[tmp]); }    }if(Ans==inf)printf(" -1\n");Else printf("%d\ n", ans);}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Bzoj 3669 [Noi2014] Magical forest LCT

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.