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 \leq 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 \leq 20000, m \leq 100000, q \leq 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 \leq 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.
For each test case, the first line consists of three integers $n, m$ and $q$ where $n \leq 20000, m \leq 100000, q \leq 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 \leq 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.
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
首先我们对于边按照边权从小到大排序,对于询问按照值从小到大排序。
枚举每次询问,从前到后扫描边,如果下一条边的边权大于当前的询问的值,那么停止,利用并查集记录每个联通块的点的个数。
每次如果某两个联通块合并,那么最终结果就是增加了(num[1]+num[2])×(num[1]+num[2]-1) - num[1] × (num[1]-1) - num[2] ×(num[2]-1)
利用
#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdio>
#include<vector>
#include<stack>
#include<map>
#include<algorithm>
#define inf 0x3f3f3f3f
#define ll long long
using namespace std;
const int MAX=20010;
struct road
{
int x,y,d;
}t[100010];
struct pq
{
int id;
int value;
ll anss;
}query[5010];
bool cmp(struct road a,struct road b)
{
return a.d<b.d;
}
bool cmpv(struct pq a,struct pq b)
{
return a.value<b.value;
}
bool cmpid(struct pq a,struct pq b)
{
return a.id<b.id;
}
int pre[MAX];
int num[MAX];
ll ans=0;
int findset(int x)
{
if(pre[x]!=x)
pre[x]=findset(pre[x]);
return pre[x];
}
void union_set(int a,int b)
{
int x=findset(a);
int y=findset(b);
if(x==y)
return ;
pre[x]=y;
ans+=(num[x]+num[y])*(num[x]+num[y]-1)-num[x]*(num[x]-1)-num[y]*(num[y]-1);
num[y]+=num[x];
}
int main()
{
int T;
cin>>T;
while(T--)
{
ans=0;
int n,m,q;
cin>>n>>m>>q;
for(int i=0;i<=m-1;i++)
scanf("%d%d%d",&t[i].x,&t[i].y,&t[i].d);
sort(t,t+m,cmp);
for(int i=1;i<=q;i++)
{
scanf("%d",&query[i].value);
query[i].id=i;
}
sort(query+1,query+q+1,cmpv);
for(int i=1;i<=n;i++)
{
pre[i]=i;
num[i]=1;
}
int index=0;
for(int j=1;j<=q;j++)
{
int x=query[j].value;
for(int i=index;i<=m-1;i++)
{
if(t[i].d>x)
{
index=i;
break;
}
union_set(t[i].x,t[i].y);
}
query[j].anss=ans;
}
sort(query+1,query+q+1,cmpid);
for(int i=1;i<=q;i++)
cout<<query[i].anss<<endl;
}
return 0;
}