HDU - 5441(并查集)


Travel

Jack likes to travel around the world, but he doesn’t like to wait. Now, he is traveling in the Undirected Kingdom. There are nn cities and mm 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 xx minutes, how many pairs of city (a,b)(a,b) are there that Jack can travel from city aa to bb without going berserk?

Input

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

For each test case, the first line consists of three integers n,mn,m and qq where n≤20000,m≤100000,q≤5000n≤20000,m≤100000,q≤5000. The Undirected Kingdom has nn cities and mm bidirectional roads, and there are qq queries.

Each of the following mm lines consists of three integers a,ba,b and dd where a,b∈{1,…,n}a,b∈{1,…,n} and d≤100000d≤100000. It takes Jack dd minutes to travel from city aa to city bb and vice versa.

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

Output

You should print qq lines for each test case. Each of them contains one integer as the number of pair of cities (a,b)(a,b) which Jack may travel from aa to bb within the time limit xx.

Note that (a,b)(a,b) and (b,a)(b,a) are counted as different pairs and aa and bb 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

题意:n的点m条边q次询问,每一次询问给出一个距离x,求有多少对点相连不大于x。注意a->b和b->a算两对。

思路:因为边数和Q次数多,所以要离线处理。边w和询问x都从小到大排序。先把每一个点看做一个集合,按照从小到大查询顺序进行查询,当他们之间满足当前询问就计算两个集合之间对答案的贡献(集合A数量X集合B数量X2),再合并两个集合和他们数量。因此每次查询都能利用到之前的查询得到的贡献。

#include <iostream>
#include <fstream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <cmath>
#include <algorithm>
#include <functional>
#define inf 0X3f3f3f3f
using namespace std;
typedef long long ll;
const int MAXN=1e5+10;
const int MAX=20000+10;
const double eps=1e-6;

int n,m,q;
int f[MAX],cnt[MAX],ans[MAX];
struct EDGE{
    int u,v,w;
    bool operator<(const EDGE& a)const{
        return w<a.w;
    }
}edge[MAXN];
struct NODE{
    int d,id;
    bool operator<(const NODE &a)const{
        return d<a.d;
    }
}Q[MAX];

void init(){
    memset(ans,0,sizeof(ans));
    for(int i=1;i<=n;i++){
        f[i]=i;
        cnt[i]=1;
    }
}

int find(int x){
    return (f[x]==x)?f[x]:f[x]=find(f[x]);
}

void solve(){
    int i,j=0,sum=0;
    for(int i=0;i<q;i++){
        while(j<m&&edge[j].w<=Q[i].d){

            int x=find(edge[j].u);
            int y=find(edge[j].v);
            if(x!=y){               
                sum+=2*cnt[x]*cnt[y];
                f[x]=y;
                cnt[y]+=cnt[x];
                cnt[x]=0;
            }

            j++;
        }
        ans[Q[i].id]=sum;
    }
}

int main(){
    #ifdef ONLINE_JUDGE
    #else
    freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    #endif
    int T;
    cin>>T;
    while(T--){
        cin>>n>>m>>q;
        init();
        for(int i=0;i<m;i++)
            scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].w);
        sort(edge,edge+m);
        for(int i=0;i<q;i++){
            scanf("%d",&Q[i].d);
            Q[i].id=i+1;
        }
        sort(Q,Q+q);
        solve();
        for(int i=1;i<=q;i++){
            printf("%d\n",ans[i]);
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值