2015年长春区域网络赛 hdu 5441 Travel【并查集】

Travel

                                         Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)

                                                            Total Submission(s): 421    Accepted Submission(s): 181
                                                                                            链接:点我,传送门

Problem 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 n cities and m 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 x minutes, how many pairs of city (a,b) are there that Jack can travel from city a to b without going berserk?
Input
The first line contains one integer T,T5 , which represents the number of test case.

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

Each of the following m lines consists of three integers a,b and d where a,b{1,...,n} and d100000 . It takes Jack d minutes to travel from city a to city b and vice versa.

Then q lines follow. Each of them is a query consisting of an integer x where x is the time limit before Jack goes berserk.
Output
You should print q lines for each test case. Each of them contains one integer as the number of pair of cities (a,b) which Jack may travel from a to b within the time limit x .

Note that (a,b) and (b,a) are counted as different pairs and a and b 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
Source
2015 ACM/ICPC Asia Regional Changchun Online
 

题意:

给定N个顶点,M条边的一个无向图,Q个询问。
对于每个询问x,从a,b的路径上各边的最大权值小于x,可以记为有序对<a,b>, 求这个图里面有多少个这样的有序对。

分析:

在N和M都比较大情况下,肯定遍历是不行的。把图画出来,我们把权值小于x的边的两端视为连通,那么对于每个连通的部分,有序对的个数
就是连通部分里面顶点个数n*(n-1),对于整个图来说,我们只需要把各个连通部分的有序对个数相加就行了。
这样思路就出来了,首先边排序,然后并查集处理各顶点之间的连通关系,离线处理各个询问就行了。

实现代码:

#include <map>
#include <cmath>
#include <cstdio>
#include <string>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <algorithm>
using namespace std;
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#define FIN             freopen("input.txt","r",stdin)
#define FOUT            freopen("output.txt","w",stdout)
#define CASE(T)         for(scanf("%d",&T);T--;)

typedef __int64  LL;
const int maxn = 20000 + 5;
const int maxm = 100000 + 5;
const int maxq = 5000 + 5;
int T, N, M, Q;
struct Edge
{
    int from, to, cost;
    bool operator < (const Edge& e) const
    {
        return cost < e.cost;
    }
} edges[maxm];
int pa[maxn], num[maxn], Query[maxq], qq[maxq];
map<int, LL> Ans;

int Find(int x)
{
    return x == pa[x] ? x : (pa[x] = Find(pa[x]));
}
int main()
{
//    FIN;
    CASE(T)
    {
        scanf("%d %d %d", &N, &M, &Q);
        int u, v, c;
        for(int i = 0; i < M; i++)
        {
            scanf("%d %d %d", &u, &v, &c);
            edges[i].from = u, edges[i].to = v, edges[i].cost = c;
        }
        sort(edges, edges + M);

        Ans.clear();
        for(int i = 0; i < Q; i++)
        {
            scanf("%d", &Query[i]);
            qq[i] = Query[i];
        }
        sort(qq, qq + Q);
        LL s = 0;
        for(int i = 1; i <= N; i++) pa[i] = i, num[i] = 1;
        int j = 0;
        for(int i = 0; i < M; i++)
        {
            int fa = Find(edges[i].from), fb = Find(edges[i].to);
            while(j < Q && edges[i].cost > qq[j])
            {
                Ans[qq[j]] = s;
                j++;
            }
            if(fa != fb)
            {
                s = (LL)s - num[fa] * (num[fa] - 1) - num[fb] * (num[fb] - 1) + (num[fa] + num[fb]) * (num[fa] + num[fb] - 1);
                pa[fb] = fa;
                num[fa] += num[fb];
            }
        }
        while(j < Q) Ans[qq[j++]] = s;
        for(int i = 0; i < Q; i++)
            printf("%I64d\n", Ans[Query[i]]);
    }
    return 0;
}


你可能感兴趣的:(并查集)