HDOJ 4424 Conquer a New Region

并查集

边从大到小排序,每加入一条边就判断应该把首都建到哪一边.....

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 Description

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, should 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

4
1 2 2
2 4 1
2 3 1
4
1 2 1
2 4 1
2 3 1

Sample Output

4
3

Source

2012 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;
}
时间: 2024-11-05 05:58:48

HDOJ 4424 Conquer a New Region的相关文章

hdu 4424 Conquer a New Region (并查集)

///题意:给出一棵树,树的边上都有边权值,求从一点出发的权值和最大,权值为从一点出去路径上边权的最小值 # include <stdio.h> # include <algorithm> # include <iostream> # include <string.h> using namespace std; # define MAX 200010 struct node { int u,v; int w; }; struct node a[MAX];

HDU 4424 Conquer a New Region 最大生成树

给你一颗树 每条边有一个权值 选择一个点为中心 定义S值为中心到其他n-1个点的路径上的最小边权 求所有点S值的和 从大到小排序 每次合并2棵树 设为A集合 B集合 设A集合的最大S值的和为sumA B集合为sumB 中心在A或者B现在加入A-B这条边使得2个集合连通 因为A-B这条边的权值小于等于AB集合里面边的权值 所以如果合并之后中心在A 那么sumA = sumA+B集合的点数*A-B这条边的权值 sumB = sumB+A集合的点数*A-B这条边的权值 2者取大 #include <c

ZOJ3659 Conquer a New Region 并查集

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 road

ZOJ 3659 Conquer a New Region

Conquer a New Region Time Limit: 5000ms Memory Limit: 32768KB This problem will be judged on ZJU. Original ID: 365964-bit integer IO format: %lld      Java class name: Main The wheel of the history rolling forward, our king conquered a new region in

zoj 3659 Conquer a New Region 并查集+贪心

点击打开链接题目链接 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 s

ZOJ Conquer a New Region(并查集)

 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 town

UVA 1664 Conquer a New Region

题意:在一颗树上要求一个到其他结点容量和最大的点,i,j之前的容量定义为i到j的路径上的最小边容量. 一开始想过由小到大的去分割边,但是很难实现,其实换个顺序就很容易做了,类似kruskal的一个贪心算法, 从大到小的连边,每次连通两个分量A和B,这样可以新边容量一定是两个分量相互到达的最小容量,其余边一定选最大,满足最优子结构 而且使新的连通分量取得最大值一定在其中一边,两者中选其中一者即可.具体实现用并查集维护即可. #include<bits/stdc++.h> using namesp

并查集&amp;MST

[HDU] 1198 Farm Irrigation 基础最小生成树★ 1598 find the most comfortable road 枚举+最小生成树★★ 1811 Rank of Tetris 并查集+拓扑排序★★ 3926 Hand in Hand 同构图★ 3938 Portal 离线+并查集★★ 2489     Minimal Ratio Tree dfs枚举组合情况+最小生成树★ 4081     Qin Shi Huang's National Road System 最

(最小生成树) hdu 4424

Conquer a New Region Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1382    Accepted Submission(s): 455 Problem Description The wheel of the history rolling forward, our king conquered a new re