Blackops

初心易得,始终难守

0%

HDU 5441 Travel(离线+并查集)

Travel

Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 4140 Accepted Submission(s): 1392

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,T≤5, which represents the number of test case.

For each test case, the first line consists of three integers n,m and q where n≤20000,m≤100000,q≤5000. 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 d≤100000. 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

题目链接:HDU 5441
这题由于不强制在线,因此可以按照询问的权值依次加入符合要求的边,答案显然就是$\sum_{i=1}^{k} Size(i)*(Size(i)-1)$,其中$k$是当前集合个数,$Size(i)$表示第$i$个集合的大小,由于每一次只会改变合并集合的大小,因此不需要每次都遍历所有的集合来统计答案,只要在合并的时候减去合并前的贡献,加上合并后的贡献就可以了
由于一条边只会被加入一次,因此总复杂度大约是$O(MlogM+M)$
代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <bitset>
#include <string>
#include <stack>
#include <cmath>
#include <queue>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
#define fin(name) freopen(name,"r",stdin)
#define fout(name) freopen(name,"w",stdout)
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
typedef pair<int, int> pii;
typedef long long LL;
const double PI = acos(-1.0);
const int N = 20010;
const int M = 100010;
const int Q = 5010;
struct edge
{
int u, v, w;
bool operator<(const edge &rhs)const
{
return w < rhs.w;
}
} E[M];
struct Query
{
int w, id;
bool operator<(const Query &rhs)const
{
return w < rhs.w;
}
} query[Q];
int pre[N], ran[N];
int n, m, q;
int ans[Q];
int sum;

void init()
{
for (int i = 1; i <= n; ++i)
{
pre[i] = i;
ran[i] = 1;
}
}
int Find(int n)
{
return pre[n] == n ? n : pre[n] = Find(pre[n]);
}
void Merge(int a, int b)
{
a = Find(a);
b = Find(b);
if(a != b)
{
pre[b] = a;
sum -= ran[a] * (ran[a] - 1);
sum -= ran[b] * (ran[b] - 1);
ran[a] += ran[b];
ran[b] = 0;
sum += ran[a] * (ran[a] - 1);
}
}
int main(void)
{
int TC, i, j;
scanf("%d", &TC);
while (TC--)
{
scanf("%d%d%d", &n, &m, &q);
init();
sum = 0;
for (i = 0; i < m; ++i)
scanf("%d%d%d", &E[i].u, &E[i].v, &E[i].w);
sort(E, E + m);
for (i = 0; i < q; ++i)
{
scanf("%d", &query[i].w);
query[i].id = i;
}
sort(query, query + q);
int cur = 0;
for (i = 0; i < q; ++i)
{
while (cur < m && E[cur].w <= query[i].w)
{
Merge(E[cur].u, E[cur].v);
++cur;
}
ans[query[i].id] = sum;
}
for (i = 0; i < q; ++i)
printf("%d\n", ans[i]);
}
return 0;
}