UVA 1664 conquer a New region

Source: Internet
Author: User

Test instructions: In one tree requires one to the other node capacity and the largest point, the capacity before I,j is defined as the minimum edge capacity on the path of I to J.

First thought from small to large to split the edge, but it is difficult to achieve, in fact, the order is easy to do, like Kruskal of a greedy algorithm,

From large to small connecting edge, each connecting two components A and B, so that the new edge capacity must be two components each other to reach the minimum capacity, the other side must be selected maximum, to meet the optimal sub-structure

and the maximum value of the new connected component must be on one side, both of which accesses than either can be selected. Specific implementation and check set maintenance can be.

#include <bits/stdc++.h>using namespacestd;Const intMAXN = 2e5+5; typedefLong Longll;structnode{intCNT; ll sum;} TOWN[MAXN];intPA[MAXN];intFind (intx) {returnx==pa[x]?x:pa[x]=Find (pa[x]);}structedge{intU,v,cap; BOOL operator< (ConstEdge &y)Const {        returnCap >Y.cap; }}ROAD[MAXN];#defineInitnode (x) pa[x] = x; town[x].cnt = 1; town[x].sum = 0;intMain () {//freopen ("In.txt", "R", stdin);    intN;  while(~SCANF ("%d",&N)) {         for(inti =1; I < n; i++) {scanf ("%d%d%d",&road[i].u,&road[i].v,&road[i].cap);        Initnode (i);        } initnode (n); Sort (Road+1, road+N);  for(inti =1; I < n; i++){            intx = Find (road[i].u), y =Find (ROAD[I].V); ll T1= Town[x].sum + (LL) town[y].cnt*(ll) Road[i].cap; LL T2= Town[y].sum + (LL) town[x].cnt*(ll) Road[i].cap; if(t1>T2) Swap (x, y), swap (T1,T2); PA[X]=y; Town[y].sum=T2; Town[y].cnt+=town[x].cnt; } printf ("%lld\n", Town[find (1)].sum); }    return 0;}

UVA 1664 conquer a New region

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.