bzoj3322 Maximum Spanning Tree +lca

Source: Internet
Author: User

The main idea: to give a graph, each edge has a limit, each point can buy and sell a certain amount of gold; and then walk through n points in order to ask for the maximum amount of gold to be sold at each point of gold sold

At first I was a little confused, thinking about how to do this problem on the map, and then know to build a maximum spanning tree first

Then just do it, do it all in gold, do not have to consider the first condition, because even if it is not enough to buy a little bit of gold in the previous place

And then there's not a single question. Two-point LCA to find the minimum edge limit on the path, so you can ask how much gold to betray

Finally to condemn the very brain remnants of data, there is a data point is two chain, DFS will explode stack = =,wa I more than 10 times two days

It's a waste of time and energy to say that you shouldn't tease people like that.

Be sure to write the stack!!

1#include <stdio.h>2#include <string.h>3#include <algorithm>4 #defineLL Long Long5 #defineINF 214748364700006 using namespacestd;7 Const intMAXN =100010;8 structnode{9     int  from, To,next;Ten LL cost; One}e[maxn*2],e[maxn*2]; A intn,m,q,trade[maxn],fa[maxn][ +],fa[maxn],dep[maxn],head[maxn],tot,logn,order[maxn],scc[maxn],bel,st[maxn*Ten]; -LL pre[maxn][ +]; -  the voidInsertintUintV, LL c) { -E[++tot].to=v; E[tot].next=head[u]; E[tot].cost=c; head[u]=tot; - } -  + BOOLCMP (Node A, Node B) { -     returnA.cost>B.cost; + } A  at intFindintx) { -     returnfa[x]==x?x:fa[x]=find (Fa[x]); - } -  - voidDfsintUintf) { -     inttop=0; inst[++top]=u; -      while(top) { tou=st[top--]; f=fa[u][0]; +dep[u]=dep[f]+1; -          for(intI=1; i<=logn; i++) fa[u][i]=fa[fa[u][i-1]][i-1]; the          for(intI=1; i<=logn; i++) Pre[u][i]=min (pre[u][i-1],pre[fa[u][i-1]][i-1]); *          for(intI=head[u]; I I=e[i].next) $             if(e[i].to!=f) {Panax Notoginsengfa[e[i].to][0]=u; -pre[e[i].to][0]=E[i].cost; thest[++top]=e[i].to; +             } A     } the } +  -LL LCA (intUintv) { $LL ret=INF; $     if(dep[u]<Dep[v]) swap (U,V); -      while(dep[u]>Dep[v]) { -          for(intI=logn; i>=0; i--) the             if(dep[fa[u][i]]>Dep[v]) { -ret=min (ret,pre[u][i]);Wuyiu=Fa[u][i]; the             } -Ret=min (ret,pre[u][0]); Wuu=fa[u][0]; -     } About     if(U==V)returnret; $      for(intI=logn; i>=0; i--) -         if(fa[u][i]!=Fa[v][i]) { -ret=min (ret,pre[u][i]); -ret=min (ret,pre[v][i]); AU=fa[u][i]; v=Fa[v][i]; +         } theRet=min (Ret,min (pre[v][0],pre[u][0])); -     returnret; $ } the  the intMain () { the     //freopen ("motorcycle6.in", "R", stdin); the     //freopen ("Test.out", "w", stdout); -scanf"%d%d%d", &n, &m, &q); in      while((1&LT;&LT;LOGN) <n) logn++; tot=1; thememset (PRE, -,sizeof(pre)); the      for(intI=1; i<=n; i++) scanf ("%d", &order[i]), fa[i]=i;//Shunxu About      for(intI=1; i<=n; i++) scanf ("%d", &trade[i]);//Yaoqiu the      for(intI=1; i<=m; i++) scanf ("%d%d%lld", &e[i]. from, &e[i].to, &e[i].cost); theBel=N; the      for(intI=1, X; i<=q; i++) scanf ("%d", &x), scc[x]=1, bel=min (bel,x); +      -Sort (e+1, e+1+m,cmp); the     intNumif(!Q) num=n-1;Elsenum=n-Q;Bayi      for(intI=1, hehe=0; i<=m; i++){ the         intX=e[i]. from, y=e[i].to; the         if(Scc[x]) X=bel;if(Scc[y]) y=Bel; -         intFx=find (x), fy=find (y); -         if(fx!=y) { thefa[fx]=fy; the Insert (x,y,e[i].cost); the Insert (y,x,e[i].cost); thehehe++; -             if(Hehe==num) Break; the         } the     } thefa[1][0]=0;94Dfs1,0); theLL now=0; the     if(trade[order[1]]<0) puts ("0");Elsenow=trade[order[1]]; the      for(intI=2; i<=n; i++){98         intx=order[i-1], y=Order[i]; About         if(Scc[x]) X=bel;if(Scc[y]) y=Bel; -now=min (Now,lca (x, y));101x=order[i-1]; y=Order[i];102         if(trade[y]>0) now+=(LL) trade[y];103         Else{104             if(now+ (LL) trade[y]>0){ thenow+=(LL) trade[y];106printf"%d\n", -trade[y]);107}Else{108printf"%lld\n", now);109now=0LL; the             }111         } the         //printf ("%lld\n", now);113     } the     return 0; the}

bzoj3322 Maximum Spanning Tree +lca

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.