zoukankan      html  css  js  c++  java
  • 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
    ");
    #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;
    }
    代码君
  • 相关阅读:
    如何将 Web 框架迁移到 Serverless
    Serverless + CVM 实战
    Serverless Registry 设计解读与实战
    使用 ServerLess 实现云原生
    腾讯云 Serverless 技术在「老司机汽车 app」的落地实践
    LeetCode 树 103. 二叉树的锯齿形层次遍历(双端队列 DFS的空间复杂度)
    LeetCode 树 116. 填充每个节点的下一个右侧节点指针(层序遍历 分层)
    LeetCode 树 230. 二叉搜索树中第K小的元素(二叉搜索树 中序遍历 剪枝)
    LeetCode 树 236. 二叉树的最近公共祖先(递归 深度优先搜索)
    LeetCode 树 102. 二叉树的层序遍历(广度优先搜索 深度优先搜索 队列)
  • 原文地址:https://www.cnblogs.com/zxhl/p/4814794.html
Copyright © 2011-2022 走看看