并查集hdu4424

Conquer a New Region

Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1271    Accepted Submission(s): 415


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
题意:给出一个树形图和边权值,边权值是容量,问把某个点作为首都,向其他n-1个点运输货物,货物容量不超过路径上的边权值;问可以输出的最大货物是多少?
分析:并查集,按照边权从大到小排序,在加入该边的同时,比较把两个集合合并到那个集合更优:
#include"string.h"
#include"stdio.h"
#include"iostream"
#include"algorithm"
#include"queue"
#include"stack"
#include"stdlib.h"
#include"map"
#include"string"
#include"math.h"
#define inf 10000000
#define INF 0x3f3f3f3f
const double PI=acos(-1.0);
const double r2=sqrt(2.0);
const int M=200009;
const int N=1010*502*2;
const double g=9.8;
#define eps 1e-10
using namespace std;
int f[M],h[M];
__int64 sum[M],ans;
int finde(int x)
{
    if(x!=f[x])
        f[x]=finde(f[x]);
    return f[x];
}
void make(int a,int b,__int64 w)
{
    int x=finde(a);
    int y=finde(b);
    __int64 f1,f2;
    f1=sum[x]+h[y]*w;
    f2=sum[y]+h[x]*w;
    if(f1>f2)
    {
        f[y]=x;
        sum[x]+=h[y]*w;
        h[x]+=h[y];
    }
    else
    {
        f[x]=y;
        sum[y]+=h[x]*w;
        h[y]+=h[x];
    }
}
struct node
{
    int u,v;
    __int64 w;
}e[M];
int cmp(node a,node b)
{
    return a.w>b.w;
}
int main()
{
    int n,i;
    while(scanf("%d",&n)!=-1)
    {
        for(i=1;i<=n;i++)
        {
            f[i]=i;
            sum[i]=0;
            h[i]=1;
        }
        ans=0;
        for(i=1;i<n;i++)
            scanf("%d%d%I64d",&e[i].u,&e[i].v,&e[i].w);
        sort(e+1,e+n,cmp);
        for(i=1;i<n;i++)
            make(e[i].u,e[i].v,e[i].w);
        printf("%I64d\n",sum[finde(1)]);
    }
    return 0;
}


### HDU 3342 并查集 解题思路与实现 #### 题目背景介绍 HDU 3342 是一道涉及并查集的数据结构题目。该类问题通常用于处理动态连通性查询,即判断若干元素是否属于同一集合,并支持高效的合并操作。 #### 数据描述 给定一系列的人际关系网络中的朋友关系对 (A, B),表示 A 和 B 是直接的朋友。目标是通过这些已知的关系推断出所有人之间的间接友谊连接情况。具体来说,如果存在一条路径使得两个人可以通过中间人的链条相连,则认为他们是间接朋友。 #### 思路分析 为了高效解决此类问题,可以采用带按秩压缩启发式的加权快速联合-查找算法(Weighted Quick Union with Path Compression)。这种方法不仅能够有效地管理大规模数据集下的分组信息,而且可以在几乎常数时间内完成每次查找和联合操作[^1]。 当遇到一个新的友链 `(a,b)` 时: - 如果 a 和 b 已经在同一棵树下,则无需任何动作; - 否则,执行一次 `union` 操作来把它们所在的两棵不同的树合并成一棵更大的树; 最终目的是统计有多少个独立的“朋友圈”,也就是森林里的树木数量减一即是所需新建桥梁的数量[^4]。 #### 实现细节 以下是 Python 版本的具体实现方式: ```python class DisjointSet: def __init__(self, n): self.parent = list(range(n)) self.rank = [0] * n def find(self, p): if self.parent[p] != p: self.parent[p] = self.find(self.parent[p]) # 路径压缩 return self.parent[p] def union(self, p, q): rootP = self.find(p) rootQ = self.find(q) if rootP == rootQ: return # 按秩合并 if self.rank[rootP] > self.rank[rootQ]: self.parent[rootQ] = rootP elif self.rank[rootP] < self.rank[rootQ]: self.parent[rootP] = rootQ else: self.parent[rootQ] = rootP self.rank[rootP] += 1 def solve(): N, M = map(int, input().split()) dsu = DisjointSet(N+1) # 初始化不相交集 for _ in range(M): u, v = map(int, input().split()) dsu.union(u,v) groups = set() for i in range(1,N+1): groups.add(dsu.find(i)) bridges_needed = len(groups)-1 print(f"Bridges needed to connect all components: {bridges_needed}") solve() ``` 这段代码定义了一个名为 `DisjointSet` 的类来进行并查集的操作,包括初始化、寻找根节点以及联合两个子集的功能。最后,在主函数 `solve()` 中读取输入参数并对每一对好友调用 `dsu.union()` 方法直到遍历完所有的边为止。之后计算不同组件的数量从而得出所需的桥接次数。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值