HDU5441 Travel 离线并查集
Travel
Problem 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≤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≤20000,m≤100000,q≤5000. The Undirected Kingdom has n cities and mbidirectional 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≤100000. It takes Jack d minutes to travel from city a to city band 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≤20000,m≤100000,q≤5000. The Undirected Kingdom has n cities and mbidirectional 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≤100000. It takes Jack d minutes to travel from city a to city band 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
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
///1085422276 #include<bits/stdc++.h> using namespace std ; typedef long long ll; #define mem(a) memset(a,0,sizeof(a)) #define meminf(a) memset(a,127,sizeof(a)); #define TS printf("111111\n"); #define FOR(i,a,b) for( int i=a;i<=b;i++) #define FORJ(i,a,b) for(int i=a;i>=b;i--) #define READ(a,b,c) scanf("%d%d%d",&a,&b,&c) #define inf 100000 inline ll read() { ll x=0,f=1; char ch=getchar(); while(ch<'0'||ch>'9') { if(ch=='-')f=-1; ch=getchar(); } while(ch>='0'&&ch<='9') { x=x*10+ch-'0'; ch=getchar(); } return x*f; } //**************************************** #define maxn 1000000+50 struct ss { int u,v,w; bool operator <(const ss &x)const{ return w<x.w; } }a[maxn]; ll ans[maxn]; int num[maxn],parent[maxn],n,m,q; struct sss { int v,id; bool operator <(const sss &x)const{ return v<x.v; } }Ans[maxn]; int finds(int x){ if(x!=parent[x])parent[x]=finds(parent[x]); return parent[x]; } void init() { FOR(i,0,n){parent[i]=i;num[i]=1;} mem(ans); } int main() { int T=read(); while(T--) { scanf("%d%d%d",&n,&m,&q); FOR(i,1,m) { scanf("%d%d%d",&a[i].u,&a[i].v,&a[i].w); } sort(a+1,a+m+1); // FOR(i,1,m){cout<<a[i].u<<" "<<a[i].v<<" "<<a[i].w<<endl;} FOR(i,1,q) { scanf("%d",&Ans[i].v); Ans[i].id=i; } sort(Ans+1,Ans+q+1); init(); //FOR(i,1,q)cout<<Ans[i].v<<" "<<Ans[i].id<<endl; ll aa=0; int j=1; FOR(i,1,q) { while(j<=m&&Ans[i].v>=a[j].w) { int fx=finds(a[j].u); int fy=finds(a[j].v); if(fx!=fy) { parent[fy]=fx; aa+=num[fx]*num[fy]; num[fx]+=num[fy]; } j++; } ans[Ans[i].id]=aa*2; } for(int i=1;i<=q;i++) printf("%I64d\n", ans[i]); } return 0; }
给你一个图,n个点,m条边,并有边权值,q个询问,每个询问是一个限制值,问你多少对不同的(S,T)路径上的最小权值不超过这个限制值,(S,T)与(T,S)是不同的。
题解:离线并查集做,讲边按照权值排序,然后用并查集构造集合中最大权值逐渐增大的联通快,统计两个不同集合时,方案数就是节点数num[A]*num[B]
更新就好了