并查集--HDU - 4313 Matrix

Description

Machines have once again attacked the kingdom of Xions. The kingdom of Xions has N cities and N-1 bidirectional roads. The road network is such that there is a 
unique path between any pair of cities. 

Morpheus has the news that K Machines are planning to destroy the whole kingdom. These Machines are initially living in K different cities of the kingdom and 
anytime from now they can plan and launch an attack. So he has asked Neo to destroy some of the roads to disrupt the connection among Machines. i.e after destroying those roads there should not be any path between any two Machines. 

Since the attack can be at any time from now, Neo has to do this task as fast as possible. Each road in the kingdom takes certain time to get destroyed and they 
can be destroyed only one at a time. 

You need to write a program that tells Neo the minimum amount of time he will require to disrupt the connection among machines. 
 

Input

The first line is an integer T represents there are T test cases. (0<T <=10) 
For each test case the first line input contains two, space-separated integers, N and K. Cities are numbered 0 to N-1. Then follow N-1 lines, each containing three, space-separated integers, x y z, which means there is a bidirectional road connecting city x and city y, and to destroy this road it takes z units of time.Then follow K lines each containing an integer. The ith integer is the id of city in which ith Machine is currently located. 
2 <= N <= 100,000 
2 <= K <= N 
1 <= time to destroy a road <= 1000,000 
 

Output

For each test case print the minimum time required to disrupt the connection among Machines.
 

Sample Input

    
    
1 5 3 2 1 8 1 0 5 2 4 5 1 3 4 2 4 0
 

Sample Output

    
    
10

分析:并查集逆向处理,n个结点n-1条边,图的生成树,假设初始每个结点都是独立的,按权值从大到小考察每条边,如果两个结点的子树中machine个数和小于1则合并这两个结点,否则删除该边;处理的过程中可以令根结点的值为-1,则1-根结点值就是子树中machine个数。


#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 100000 + 10;
struct Node { int x, y, w; };
int p[maxn];
Node node[maxn];

bool cmp(const Node& a, const Node& b)
{
    return a.w > b.w;
}

int find_set(int x)
{
	return p[x]<0?x:(p[x]=find_set(p[x]));
}

int main()
{
	int T;
	scanf("%d", &T);
	while(T--) {
		memset(p, -1, sizeof(p));
		int n, m;
		scanf("%d%d", &n, &m);
        for(int i=0; i<n-1; i++) {
			scanf("%d%d%d", &node[i].x, &node[i].y, &node[i].w);
        }
        for(int i=0; i<m; i++) {
            int mac;
            scanf("%d", &mac);
            p[mac] = -2;
        }
		sort(node, node+n-1, cmp);
		long long cnt = 0;
        for(int i=0; i<n-1; i++) {
            int x = find_set(node[i].x);
            int y = find_set(node[i].y);
            if(p[x]+p[y]>=-3) {
                p[x] += p[y]+1;
                p[y] = x;
            } else {
                cnt += node[i].w;
            }
        }
        printf("%lld\n", cnt);
	}
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值