Problem Description
ZLGG found a magic theory that the bigger banana the bigger banana peel .This important theory can help him make a portal in our universal. Unfortunately, making a pair of portals will cost min{T} energies. T in a path between point V and point U is the length of the longest edge in the path. There may be lots of paths between two points. Now ZLGG owned L energies and he want to know how many kind of path he could make.
Input
There are multiple test cases. The first line of input contains three integer N, M and Q (1 < N ≤ 10,000, 0 < M ≤ 50,000, 0 < Q ≤ 10,000). N is the number of points, M is the number of edges and Q is the number of queries. Each of the next M lines contains three integers a, b, and c (1 ≤ a, b ≤ N, 0 ≤ c ≤ 10^8) describing an edge connecting the point a and b with cost c. Each of the following Q lines contain a single integer L (0 ≤ L ≤ 10^8).
Output
Output the answer to each query on a separate line.
Sample Input
10 10 10
7 2 1
6 8 3
4 5 8
5 8 2
2 8 9
6 4 5
2 1 5
8 10 5
7 3 7
7 8 8
10
6
1
5
9
1
8
2
7
6
Sample Output
36
13
1
13
36
1
36
2
16
13
通过sort以后
使用并查集+离线即可
#include <iostream>
#include <cstdio>
#include <string.h>
#include <algorithm>
using namespace std;
typedef long long ll;
int n,m,qq;
struct node{
int s,e;
int v;
}edge[50005];
struct node2{
int id;
int ans,ques;
}q[10005];
int root[10005];
int sz[10005];
int nowans=0;
bool cmp_v(node a,node b)
{
return a.v<b.v;
}
bool cmp_l(node2 a,node2 b)
{
return a.ques<b.ques;
}
bool cmp_id(node2 a,node2 b)
{
return a.id<b.id;
}
void init()
{
for(int i=1;i<=n;i++){
root[i]=i;
sz[i]=1;
}
}
int f(int x)
{
return x==root[x]?x:(root[x]=f(root[x]));
}
bool unit(int a,int b)
{
a=f(a);
b=f(b);
if(a==b){
return false;
}
nowans=sz[a]*sz[b];
if(sz[a]>=sz[b]){
root[b]=a;
sz[a]+=sz[b];
}
else{
root[a]=b;
sz[b]+=sz[a];
}
return true;
}
int main()
{
while(scanf("%d%d%d",&n,&m,&qq)!=EOF)
{
init();
for(int i=1;i<=m;i++){
scanf("%d%d%d",&edge[i].s,&edge[i].e,&edge[i].v);
}
for(int i=1;i<=qq;i++){
scanf("%d",&q[i].ques);
q[i].id=i;
q[i].ans=0;
}
sort(edge+1,edge+1+m,cmp_v);
sort(q+1,q+1+qq,cmp_l);
int cur=1;
for(int i=1;i<=qq;i++){
q[i].ans=q[i-1].ans;
while(edge[cur].v<=q[i].ques&&cur<=m){
if(unit(edge[cur].s,edge[cur].e)){
q[i].ans+=nowans;
}
cur++;
}
}
sort(q+1,q+1+qq,cmp_id);
for(int i=1;i<=qq;i++){
printf("%d\n",q[i].ans);
}
}
return 0;
}