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


Contest: The 2012 ACM-ICPC Asia Changchun Regional Contest

题意:给你一个树,每条路径都有一个权值,让你找到一个点X,使得任意点与X点这条路上的最小的一个权值之和最大,问你这个最大权值是多少

题解:先排序,然后对个加进来的边的两端a,b子树看作集合A,B,即要么以A集合一端点a为X点,要么B集合一端点b为X点,并查集维护下去,并保存集合的元素个数与答案

到最后对这个整体的集合进行找祖先就好了。

//1085422276
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
#include<cmath>
#include<map>
#include<bitset>
#include<set>
#include<vector>
using namespace std ;
typedef long long ll;
#define mem(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,127,sizeof(a));
#define memfy(a) memset(a,-1,sizeof(a))
#define TS printf("111111\n");
#define FOR(i,a,b) for( int i=a;i<=b;i++)
#define FORJ(i,a,b) for(int i=a;i>=b;i--)
#define READ(a,b) scanf("%d%d",&a,&b)
#define mod 1000000007
#define maxn 200000+10
inline ll read()
{
    ll x=0,f=1;
    char ch=getchar();
    while(ch<‘0‘||ch>‘9‘)
    {
        if(ch==‘-‘)f=-1;
        ch=getchar();
    }
    while(ch>=‘0‘&&ch<=‘9‘)
    {
        x=x*10+ch-‘0‘;
        ch=getchar();
    }
    return x*f;
}
//****************************************
int parent[maxn],n,m,num[maxn];
ll sum[maxn];
struct ss
{
    int u,v,w;
} edge[maxn];
void init()
{
    FOR(i,0,n)parent[i]=i;
    mem(sum);
    FOR(i,0,n)num[i]=1;
}
int finds(int x)
{
    if(x!=parent[x])parent[x]=finds(parent[x]);
    else return x;
}
bool cmp(ss s1,ss s2)
{
    return s1.w>s2.w;
}
int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        init();
        FOR(i,1,n-1)
        {
            scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].w);
        }
        sort(edge+1,edge+n,cmp);
        FOR(i,1,n-1)
        {
            int A=finds(edge[i].u);
            int B=finds(edge[i].v);
            if(sum[A]+(ll)edge[i].w*num[B]>=sum[B]+(ll)edge[i].w*num[A])
            {
                parent[B]=A;
                num[A]+=num[B];
                sum[A]+=(ll)edge[i].w*num[B];

            }
            else
            {
                parent[A]=B;
                num[B]+=num[A];
                sum[B]+=(ll)edge[i].w*num[A];

            }
        }
        cout<<sum[finds(1)]<<endl;
    }
    return 0;
}

代码君

时间: 2024-10-21 10:04:03

ZOJ3659 Conquer a New Region 并查集的相关文章

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

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];

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

并查集+贪心 HDU4424

先对边进行从大到小的排序 并查集保存节点数和长度 Conquer a New Region Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1289    Accepted Submission(s): 429 Problem Description The wheel of the history rolling forward, o

并查集&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 最

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

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 rollin

zoj 3659 并查集

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4882 现在在牡丹江,明天regional现场赛,不出一个月就要退役了,求保佑 今天热身赛做题很紧张,老是打错字,所以晚上写写代码练练手 脑子还是不好使,没想到可以并查集 思路:题目中的操作导致的一个结果是,一条边会成为一个集合的w,----  如果能想到这里可能就能想到并查集吧 WA了因为如果father[x]==x并不能表示x中只有一个数啊... #include <cst

POJ 1838 Banana (并查集)

Description Consider a tropical forrest, represented as a matrix. The cell from the right top corner of the matrix has the coordinates (1,1), and the coordinates of the other cells are determinated by the row and the column on which the cell is. In s