Portal
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2063 Accepted Submission(s): 1008
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
Source
2011 Multi-University Training Contest 10 - Host by HRBEU
Recommend
We have carefully selected several similar problems for you: 3935 3937 3931 3932 3933
#pragma comment(linker, "/STACK:102400000,102400000")
#include<bits/stdc++.h>
using namespace std;
#define debug puts("YES");
#define rep(x,y,z) for(int (x)=(y);(x)<(z);(x)++)
#define lrt int l,int r,int rt
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define ll long long
const int maxn =1e5+5;
const int mod=1e9+7;
/*
题目大意:就是说两个点之间如果要建路的话,
权重是多个路径中的最长边的最小值。
要注意到生成树可以保证所有点连通,
那么最小生成树可以维护这样的集合,
边一定选取的都是最小的,
就是说用最小的代价边来维护连通区域。
这道题目明显应该离线查询,
对查询的权值从小到大排序,
然后对每个查询值不断的通过Krustal并查集方法扩展,
两个连通块的合并对答案的贡献是两个连通块大小的乘积。
至此,答案可以迭代出来。
*/
int n,m,q;
int ans;
///并查集结构
int pre[maxn],sum[maxn];
int Find(int x){return pre[x]==x?x:pre[x]=Find(pre[x]);}
void Union(int x,int y)
{
int fx=Find(x),fy=Find(y);
if(fx==fy) return ;///同一节点的就不比了
ans+=sum[fx]*sum[fy];
if(sum[fx]>sum[fy])
{
pre[fy]=fx;
sum[fx]+=sum[fy];
}
else
{
pre[fx]=fy;
sum[fy]+=sum[fx];
}
return ;
}
///边节点和查询节点
struct node{int x,y,z;};///公用
node edge[maxn],query[maxn];
bool cmp1(node p,node q)
{
return p.z<q.z;
}
bool cmp2(node p, node q)
{
return p.x<q.x;///询问的权重
}
int ret[maxn];
int main()
{
scanf("%d%d%d",&n,&m,&q);
for(int i=0;i<m;i++) scanf("%d%d%d",&edge[i].x,&edge[i].y,&edge[i].z);
for(int i=0;i<q;i++) scanf("%d",&query[i].x),query[i].y=i;
for(int i=1;i<=n;i++)
{
sum[i]=1;
pre[i]=i;
}///预处理
sort(edge,edge+m,cmp1);
sort(query,query+q,cmp2);
int i=0,j=0; ans=0;
for(j=0;j<q;j++)
{
while(i<m && edge[i].z<=query[j].x)
{
Union( edge[i].x , edge[i].y );
i++;
}
ret[query[j].y]=ans;
}
for(int i=0;i<q;i++) printf("%d\n",ret[i]);
return 0;
}