POJ 2832 How Many Pairs?

 

Description

You are given an undirected graph G with N vertices and M edges. Each edge has a length. Below are two definitions.

  1. Define max_len(p) as the length of the edge with the maximum length of p where p is an arbitrary non-empty path in G.
  2. Define min_pair(uv) as min{max_len(p) | p is a path connecting the vertices u and v.}. If there is no paths connecting u and vmin_pair(uv) is defined as infinity.

Your task is to count the number of (unordered) pairs of vertices u and v satisfying the condition that min_pair(uv) is not greater than a given integer A.

Input

The first line of input contains three integer NM and Q (1 < N ≤ 10,000, 0 < M ≤ 50,000, 0 < Q ≤ 10,000). N is the number of vertices, M is the number of edges and Q is the number of queries. Each of the next M lines contains three integers ab, and c (1 ≤ ab ≤ N, 0 ≤ c < 108) describing an edge connecting the vertices a and b with length c. Each of the following Q lines gives a query consisting of a single integer A (0 ≤ A < 108).

Output

Output the answer to each query on a separate line.

Sample Input

4 5 4
1 2 1
2 3 2
2 3 5
3 4 3
4 1 4
0
1
3
2

Sample Output

0
1
6
3

 

题解:

将边和询问都按从小到大排序,然后对于一组询问,我们枚举所有小于当前询问的边,然后把边的两个端点对应的集合进行计算,并查集合并维护

 1 #include <algorithm>
 2 #include <iostream>
 3 #include <cstdlib>
 4 #include <cstring>
 5 #include <cstdio>
 6 #include <cmath>
 7 using namespace std;
 8 const int N=100005,M=500005,QM=2000005;
 9 typedef long long ll;
10 struct node{
11     int x,y,dis;
12     bool operator <(const node &pp)const{
13         return dis<pp.dis;
14     }
15 }e[M];
16 int gi(){
17     int str=0;char ch=getchar();
18     while(ch>'9' || ch<'0')ch=getchar();
19     while(ch>='0' && ch<='9')str=(str<<1)+(str<<3)+ch-48,ch=getchar();
20     return str;
21 }
22 int n,m,Q,size[N],fa[N];
23 int find(int x){
24     return fa[x]==x?x:fa[x]=find(fa[x]);
25 }
26 struct Question{
27     int id,x;ll sum;
28 }q[QM];
29 bool compone(const Question &pp,const Question &qq){
30     return pp.x<qq.x;
31 }
32 bool comptwo(const Question &pp,const Question &qq){
33     return pp.id<qq.id;
34 }
35 void work(){
36     int x,y,dis;
37     n=gi();m=gi();Q=gi();
38     for(int i=1;i<=m;i++){
39         e[i].x=gi();e[i].y=gi();e[i].dis=gi();
40     }
41     for(int i=1;i<=Q;i++)q[i].id=i,q[i].x=gi();
42     for(int i=1;i<=n;i++)fa[i]=i,size[i]=1;
43     sort(e+1,e+m+1);
44     sort(q+1,q+Q+1,compone);
45     int cnt=0,sum=0,p=1;
46     for(int i=1;i<=Q;i++){
47         while(e[p].dis<=q[i].x && cnt<n-1 && p<=m){
48             x=e[p].x;y=e[p].y;
49             if(find(x)==find(y)){
50                p++;continue;
51             }
52             sum+=(ll)size[find(y)]*size[find(x)];
53            size[find(x)]+=size[find(y)];
54             fa[find(y)]=find(x);
55             p++;cnt++;
56         }
57         q[i].sum=sum;
58     }
59     sort(q+1,q+Q+1,comptwo);
60     for(int i=1;i<=Q;i++)
61         printf("%lld\n",q[i].sum);
62 }
63 int main()
64 {
65     work();
66     return 0;
67 }

 

转载于:https://www.cnblogs.com/Yuzao/p/7222264.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值