思路:显然的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




 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 



Input


, 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


 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 


Sample Input


15 5 32 3 63341 5 15724 3 5 5705 4 3 12382 1 3 21726 6000 10000 13000


Sample Output


2612