With the 60th anniversary celebration of Nanjing University of Science and Technology coming soon, the university sets n tourist spots to welcome guests. Of course, Redwood forests in our university and its Orychophragmus violaceus must be recommended as top ten tourist spots, probably the best of all. Some undirected roads are made to connect pairs of tourist spots. For example, from Redwood forests (suppose it’s a) to fountain plaza (suppose it’s b), there may exist an undirected road with its length c. By the way, there is m roads totally here. Accidently, these roads’ length is an integer, and all of them are different. Some of these spots can reach directly or indirectly to some other spots. For guests, they are travelling from tourist spot s to tourist spot t, they can achieve some value f. According to the statistics calculated and recorded by us in last years, We found a strange way to calculate the value f:
From s to t, there may exist lots of different paths, guests will try every one of them. One particular path is consisted of some undirected roads. When they are travelling in this path, they will try to remember the value of longest road in this path. In the end, guests will remember too many longest roads’ value, so he cannot catch them all. But, one thing which guests will keep it in mind is that the minimal number of all these longest values. And value f is exactly the same with the minimal number.
Tom200 will recommend pairs (s, t) (start spot, end spot points pair) to guests. P guests will come to visit our university, and every one of them has a requirement for value f, satisfying f>=t. Tom200 needs your help. For each requirement, how many pairs (s, t) you can offer?
Input
Multiple cases, end with EOF.
First line:n m
n tourist spots ( 1
using namespace std;
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#define N 100010
#define r(x) scanf("%d",&x)
#define F(i,a,b) for(int i=a;i<=b;i++)
struct edge{
int u,v;
int C;
}xx[500001],yy[500001];
bool cmp(edge x,edge y)
{
return x.C<y.C;
}
int fa[N];
int getf(int x)
{
return fa[x]==x?x:fa[x]=getf(fa[x]);
}
int n,m;
int val[N];
int S[N];
int num[N];
int x,y,z;
void init()
{
F(i,1,n+1) fa[i]=i;
for(int i=1;i<=n+1;i++) S[i]=1;
memset(num,0,sizeof(num));
}
int cnt;
int p;
int main()
{
while(scanf("%d%d",&n,&m)==2)
{
F(i,1,m)
{
r(xx[i].u);r(xx[i].v);r(xx[i].C);xx[i].u++;xx[i].v++;
}
sort(xx+1,xx+m+1,cmp);
cnt=0;
init();
F(i,1,m)
{
if(getf(xx[i].u)!=getf(xx[i].v))
{
if(val[cnt]!=xx[i].C)cnt++;val[cnt]=xx[i].C;
int it=S[fa[xx[i].u]]*S[fa[xx[i].v]]*2;num[cnt]+=it;
S[fa[xx[i].v]]+=S[fa[xx[i].u]];
S[fa[xx[i].u]]=0;
fa[fa[xx[i].u]]=getf(xx[i].v);
}
}
F(i,1,cnt+1) num[i]+=num[i-1];
// F(i,1,cnt) cout<<val[i]<<" "<<num[i]<<endl;
r(p);
F(i,1,p)
{
r(x);
int it=lower_bound(val+1,val+cnt+1,x)-val-1;
printf("%d\n",num[cnt+1]-num[it]);
}
}
}