HDU 5441 Travel(并查集)

本文介绍了一种解决旅行者Jack在有限时间内从一个城市到另一个城市的旅行问题的方法。通过将问题转化为图论中的连通性问题,并采用并查集进行高效处理,实现了对不同时间限制下可行路径数量的有效计算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

思路:显然的mq这样的做法就T成狗...那么可以给边排个序,询问也排个序,然后扫过去,每次两个连通块合并的时候的答案就是(cnt[u]+cnt[v])*(cnt[u]+cnt[v]-1) - (cnt[u]*(cnt[u]-1))-(cnt[v]*(cnt[v]-1))嘛,然后就做完了


#include<bits/stdc++.h>
using namespace std;
const int maxn = 20000+6;
const int maxm = 100000+7;
int fa[maxn],cnt[maxn],ans[maxn];
int Find(int x){return x==fa[x]?x:fa[x]=Find(fa[x]);}
int n,m,qq;
struct Node
{
	int u,v,w;
}e[maxm];
struct node
{
	int w,id;
}q[maxn];
bool cmp1(node a,node b){return a.w<b.w;}
bool cmp(Node a,Node b){return a.w<b.w;}
void merge(int u,int v)
{
	fa[u]=v;
	cnt[v]+=cnt[u];
}
void init()
{
	for(int i = 0;i<=n;i++)
	{
		fa[i]=i;
		cnt[i]=1;
	}
}
int main()
{
    int T;
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d%d%d",&n,&m,&qq);
		init();
		for(int i= 1;i<=m;i++)
			scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].w);
		sort(e+1,e+1+m,cmp);
		for(int i = 1;i<=qq;i++)
			scanf("%d",&q[i].w),q[i].id=i;
		sort(q+1,q+1+qq,cmp1);
		int j = 1;
		int res = 0;
        for(int i = 1;i<=qq;i++)
		{
            while(j<=m && e[j].w<=q[i].w)
			{
                int u = Find(e[j].u);
				int v = Find(e[j].v);
				if(u!=v)
				{
					int tmp = cnt[u]+cnt[v];
					res += (tmp*(tmp-1))-(cnt[u]*(cnt[u]-1))-(cnt[v]*(cnt[v]-1));
					merge(u,v);
				}
				j++;
			}
			ans[q[i].id]=res;
		}
		for(int i = 1;i<=qq;i++)
			printf("%d\n",ans[i]);
	}
}


Description

Jack likes to travel around the world, but he doesn’t like to wait. Now, he is traveling in the Undirected Kingdom. There are  cities and  bidirectional roads connecting the cities. Jack hates waiting too long on the bus, but he can rest at every city. Jack can only stand staying on the bus for a limited time and will go berserk after that. Assuming you know the time it takes to go from one city to another and that the time Jack can stand staying on a bus is  minutes, how many pairs of city  are there that Jack can travel from city  to  without going berserk?

Input

The first line contains one integer , which represents the number of test case. 

For each test case, the first line consists of three integers  and  where . The Undirected Kingdom has  cities and  bidirectional roads, and there are  queries. 

Each of the following  lines consists of three integers  and  where  and . It takes Jack  minutes to travel from city  to city  and vice versa. 

Then  lines follow. Each of them is a query consisting of an integer  where  is the time limit before Jack goes berserk. 

Output

You should print  lines for each test case. Each of them contains one integer as the number of pair of cities  which Jack may travel from  to  within the time limit 

Note that  and  are counted as different pairs and  and  must be different cities.

Sample Input

1
5 5 3
2 3 6334
1 5 15724
3 5 5705
4 3 12382
1 3 21726
6000
10000
13000

Sample Output

2
6
12


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值