Query set
The edge is sorted from the largest to the smallest. Each time an edge is added, it is determined which side of the capital should be built .....
Conquer a new region
Time Limit: 8000/4000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 1169 accepted submission (s): 373
Problem descriptionthe 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.
Inputthere 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)
Outputfor 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
Source2012 Asia Changchun Regional Contest
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn=200200;typedef long long int LL;struct Edge{LL u,v,w;}edge[maxn];bool cmp(Edge x,Edge y){return x.w>y.w;}LL n;LL fa[maxn];LL value[maxn];LL sz[maxn];LL find(LL x){if(x==fa[x]) return x;return fa[x]=find(fa[x]);}int main(){while(scanf("%I64d",&n)!=EOF){for(LL i=0;i<n-1;i++){LL a,b,c;scanf("%I64d%I64d%I64d",&a,&b,&c);edge[i].u=a; edge[i].v=b; edge[i].w=c;fa[i]=i;sz[i]=1;value[i]=0;}fa[n-1]=n-1; sz[n-1]=1; value[n-1]=0;fa[n]=n;sz[n]=1;value[n]=0;sort(edge,edge+n-1,cmp);for(LL i=0;i<n-1;i++){LL u=edge[i].u,v=edge[i].v; LL w=edge[i].w;LL U=find(u),V=find(v);if(U==V) continue;LL VVV=max(value[U]+w*sz[V],value[V]+w*sz[u]);fa[U]=V;value[V]=VVV;sz[V]=sz[V]+sz[U];}cout<<value[find(1)]<<endl;}return 0;}
Hdoj 4424 conquer a new region