hdu 5441 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 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.
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
Source
比赛的时候坑在这题了,一直TLE,统计个数的方法不对,导致一直在这题徘徊,否则就能去思考其它的题了,可能就不是这种破成绩了。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #pragma comment(linker, "/STACK:1024000000,1024000000") 2 #include<iostream> 3 #include<cstdio> 4 #include<cstring> 5 #include<cmath> 6 #include<math.h> 7 #include<algorithm> 8 #include<queue> 9 #include<set> 10 #include<bitset> 11 #include<map> 12 #include<vector> 13 #include<stdlib.h> 14 using namespace std; 15 #define ll long long 16 #define eps 1e-10 17 #define MOD 1000000007 18 #define N 600000 19 #define inf 1e12 20 int n,m,q; 21 struct Node{ 22 int u,v,d; 23 }edge[N]; 24 struct Node1{ 25 int num; 26 int idd; 27 int ans; 28 }query[5006]; 29 bool cmp1(Node a,Node b){ 30 return a.d<b.d; 31 } 32 bool cmp2(Node1 a,Node1 b){ 33 return a.num<b.num; 34 } 35 bool cmp3(Node1 a,Node1 b){ 36 return a.idd<b.idd; 37 } 38 ///////////////////////////////////////////// 39 int fa[20006]; 40 int cnt[20006]; 41 void init(){ 42 for(int i=0;i<20006;i++){ 43 fa[i]=i; 44 cnt[i]=1; 45 } 46 } 47 int find(int x){ 48 return fa[x]==x?x:fa[x]=find(fa[x]); 49 } 50 51 ////////////////////////////////////////////////////// 52 53 int main() 54 { 55 int t; 56 scanf("%d",&t); 57 while(t--){ 58 init(); 59 scanf("%d%d%d",&n,&m,&q); 60 for(int i=0;i<m;i++){ 61 scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].d); 62 } 63 for(int i=0;i<q;i++){ 64 scanf("%d",&query[i].num); 65 query[i].idd=i; 66 query[i].ans=0; 67 } 68 sort(edge,edge+m,cmp1); 69 sort(query,query+q,cmp2); 70 int w=0; 71 int ans=0; 72 for(int i=0;i<q;i++){ 73 //printf("+====="); 74 while(w<m && edge[w].d<=query[i].num){ 75 76 int root1=find(edge[w].u); 77 int root2=find(edge[w].v); 78 79 if(root1!=root2){ 80 ans+=cnt[root1]*cnt[root2]; 81 82 //printf("***%d\n",ans); 83 cnt[root2]+=cnt[root1]; 84 fa[root1]=root2; 85 } 86 w++; 87 } 88 //printf("%d\n",ans*2); 89 query[i].ans=ans*2; 90 } 91 sort(query,query+q,cmp3); 92 for(int i=0;i<q;i++){ 93 printf("%d\n",query[i].ans); 94 } 95 } 96 return 0; 97 }