Count The Pairs
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 277 Accepted Submission(s): 150
Problem Description
![](https://i-blog.csdnimg.cn/blog_migrate/07b99ef46d34e48140136c011b7b293f.jpeg)
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<n<=10000), spots’ index starts from 0.
m undirected roads ( 1<m<=500000).
Next m lines, 3 integers, a b c
From tourist spot a to tourist spot b, its length is c. 0<a, b<n, c(0<c<1000000000), all c are different.
Next one line, 1 integer, p (0<p<=100000)
It means p guests coming.
Next p line, each line one integer, t(0<=t)
The value t you need to consider to satisfy f>=t.
First line:n m
n tourist spots ( 1<n<=10000), spots’ index starts from 0.
m undirected roads ( 1<m<=500000).
Next m lines, 3 integers, a b c
From tourist spot a to tourist spot b, its length is c. 0<a, b<n, c(0<c<1000000000), all c are different.
Next one line, 1 integer, p (0<p<=100000)
It means p guests coming.
Next p line, each line one integer, t(0<=t)
The value t you need to consider to satisfy f>=t.
Output
For each guest's requirement value t, output the number of pairs satisfying f>=t.
Notice, (1,2), (2,1) are different pairs.
Notice, (1,2), (2,1) are different pairs.
Sample Input
2 1 0 1 2 3 1 2 3 3 3 0 1 2 0 2 4 1 2 5 5 0 2 3 4 5
Sample Output
2 2 0 6 6 4 4 0
Source
Recommend
liuyiding
使用并查集维护点的个数,边从小到大加就可以了。
查询的时候二分查找
1 /* *********************************************** 2 Author :kuangbin 3 Created Time :2013/9/21 星期六 12:43:28 4 File Name :2013南京网络赛\1003.cpp 5 ************************************************ */ 6 7 #pragma comment(linker, "/STACK:1024000000,1024000000") 8 #include <stdio.h> 9 #include <string.h> 10 #include <iostream> 11 #include <algorithm> 12 #include <vector> 13 #include <queue> 14 #include <set> 15 #include <map> 16 #include <string> 17 #include <math.h> 18 #include <stdlib.h> 19 #include <time.h> 20 using namespace std; 21 22 const int MAXN = 10010; 23 int F[MAXN]; 24 int num[MAXN]; 25 int find(int x) 26 { 27 if(F[x] == -1)return x; 28 else return F[x] = find(F[x]); 29 } 30 void bing(int x,int y) 31 { 32 int t1 = find(x); 33 int t2 = find(y); 34 if(t1 != t2) 35 { 36 F[t1] = t2; 37 num[t2] += num[t1]; 38 } 39 } 40 struct Edge 41 { 42 int u,v,w; 43 }edge[500010]; 44 bool cmp(Edge a,Edge b) 45 { 46 return a.w < b.w; 47 } 48 49 int a[500010]; 50 int b[500010]; 51 long long sum[500010]; 52 int main() 53 { 54 //freopen("in.txt","r",stdin); 55 //freopen("out.txt","w",stdout); 56 int n,m; 57 while(scanf("%d%d",&n,&m) == 2) 58 { 59 for(int i = 0;i < n;i++) 60 { 61 F[i] = -1; 62 num[i] = 1; 63 } 64 for(int i = 0;i < m;i++) 65 { 66 scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].w); 67 } 68 sort(edge,edge+m,cmp); 69 for(int i = 0;i < m;i++) 70 b[i] = edge[i].w; 71 for(int i = 0;i < m;i++) 72 { 73 int u = edge[i].u; 74 int v = edge[i].v; 75 if(find(u) != find(v)) 76 { 77 a[i] = 2*num[find(u)]*num[find(v)]; 78 bing(u,v); 79 } 80 else a[i] = 0; 81 } 82 sum[m] = 0; 83 for(int i = m-1;i >= 0;i--) 84 sum[i] = sum[i+1] + a[i]; 85 int p; 86 int t; 87 scanf("%d",&p); 88 while(p--) 89 { 90 scanf("%d",&t); 91 int id = lower_bound(b,b+m,t) - b; 92 if(id >= m)printf("0\n"); 93 else 94 { 95 printf("%I64d\n",sum[id]); 96 } 97 } 98 99 } 100 return 0; 101 }