Zoj3659 conquer a new region

Source: Internet
Author: User

Conquer a new region

Time Limit: 5 seconds
Memory limit: 32768 KB

The wheel of the History rolling forward, our King conquered a new region in a distant continent.

There are n towns (numbered from 1 to n) in this region connected by several roads. it's confirmed that there is exact one route between any two towns. traffic is important while controlled colonies are far away from the local country. we define the capacity
C (I, j) of a road indicating it is allowed to transport at most C (I, j) goods between town I and town J if there is a road between them. and for a route between I and j, we define a value S (I, j) indicating the maximum traffic capacity between I and j which
Is equal to the minimum capacity of the roads on the route.

Our King wants to select a center town to restore his war-resources in which the total traffic capacities from the center to the other n-1 towns is maximized. now, you, the best programmer in the Kingdom, shocould help our king to select this center.

Input

There are multiple test cases.

The first line of each case contains an integer n. (1 ≤ n ≤ 200,000)

The next n-1 lines each contains three integers a, B, c indicating there is a road between town a and town B whose capacity is C. (1 ≤ a, B ≤ n, 1 ≤ C ≤ 100,000)

Output

For each test case, output an integer indicating the total traffic capacity of the chosen center town.

Sample Input
41 2 22 4 12 3 141 2 12 4 12 3 1
Sample output
43
It is indeed very good to use and check the set for this question. At that time, there was a kind of network stream feeling, which had been stuck there. In fact, we can find that, this is a tree, so there is only one path between the two points, that is, this important nature. We can use and query the set. We sort the order from big to small, for two sets of A and B, we have two merge methods. If we compare A to B or B to A, we only need to merge them in the largest way, you can get the maximum value! Because there is only one edge between two points!
#include <iostream>#include <stdio.h>#include <algorithm>#include <string.h>using namespace std;#define MAXN 200050struct node {    int s,e,c;    bool operator <(node a)const{return c>a.c;}}edge[MAXN];long long sum[MAXN];int no[MAXN],father[MAXN];bool cmp(node a,node b){    return a<b;}int find(int x){    if(father[x]!=x)        father[x]=find(father[x]);    return father[x];}int main(){   int n,i;   long long  tempa,tempb;   while(scanf("%d",&n)!=EOF)   {       for(i=0;i<=n;i++)       {           no[i]=1,sum[i]=0,father[i]=i;       }       for(i=1;i<=n-1;i++)       {          scanf("%d%d%d",&edge[i].s,&edge[i].e,&edge[i].c);       }       sort(edge+1,edge+n,cmp);       for(i=1;i<=n-1;i++)       {           int a=find(edge[i].s);           int b=find(edge[i].e);           if(a!=b)           {               tempa=(long long )edge[i].c*no[b]+sum[a];               tempb=(long long )edge[i].c*no[a]+sum[b];               if(tempa>tempb)               {                   father[b]=a,sum[a]=tempa,no[a]+=no[b];               }               else               {                   father[a]=b,sum[b]=tempb,no[b]+=no[a];               }           }        }        int a=find(1);        printf("%lld\n",sum[a]);   }    return 0;}

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.