Portal 并查集(去重)

Problem Description
ZLGG found a magic theory that the bigger banana the bigger banana peel .This important theory can help him make a portal in our universal. Unfortunately, making a pair of portals will cost min{T} energies. T in a path between point V and point U is the length of the longest edge in the path. There may be lots of paths between two points. Now ZLGG owned L energies and he want to know how many kind of path he could make.
 

 

Input
There are multiple test cases. The first line of input contains three integer N, M and Q (1 < N ≤ 10,000, 0 < M ≤ 50,000, 0 < Q ≤ 10,000). N is the number of points, M is the number of edges and Q is the number of queries. Each of the next M lines contains three integers a, b, and c (1 ≤ a, b ≤ N, 0 ≤ c ≤ 10^8) describing an edge connecting the point a and b with cost c. Each of the following Q lines contain a single integer L (0 ≤ L ≤ 10^8).
 

 

Output
Output the answer to each query on a separate line.
 

 

Sample Input
10 10 10
7 2 1
6 8 3
4 5 8
5 8 2
2 8 9
6 4 5
2 1 5
8 10 5
7 3 7
7 8 8
10
6
1
5
9
1
8
2
7
6
 

 

Sample Output
36
13
1
13
36
1
36
2
16
13
***************************************************************************************************************************
并查集&&离线算法
***************************************************************************************************************************
 1 /*
 2 求最大边的最小值,用贪心排序
 3 */
 4 #include<iostream>
 5 #include<string>
 6 #include<cstring>
 7 #include<cmath>
 8 #include<cstdio>
 9 #include<algorithm>
10 using namespace std;
11 int fa[50001],num[50001];
12 int ans[50001];
13 int n,m,q;
14 int i,j,k;
15 void init()
16 {
17     int it;
18     for(int it=1;it<=n;it++)
19     {
20         fa[it]=it;
21         num[it]=1;
22     }
23 }
24 struct node
25 {
26     int st,en,len;
27 }e[50001];
28 struct out
29 {
30     int id;
31     int value;
32 }A[50001];
33 bool cmp1(node a,node b)
34 {
35     return a.len<b.len;
36 }
37 bool cmp2(out a,out b)
38 {
39     return a.value<b.value;
40 }
41 int find(int x)
42 {
43     int temp=x;
44     int root;
45     while(x!=fa[x])
46      x=fa[x];
47     root=x;
48     x=temp;
49     while(x!=fa[x])
50     {
51         temp=x;
52         fa[x]=root;
53         x=fa[temp];
54     }
55     return root;
56 }
57 int Unon(int a,int b)
58 {
59     int x=find(a);
60     int y=find(b);
61     if(x==y)return  0;
62     int temp1=num[x]*num[y];
63     num[x]+=num[y];
64     num[y]=0;//此处很重要,主要为了去重
65     fa[y]=x;
66     return temp1;
67 }
68 int main()
69 {
70    while(scanf("%d %d %d",&n,&m,&q)!=EOF)
71    {
72        init();
73        for(i=0;i<m;i++)
74         scanf("%d %d %d",&e[i].st,&e[i].en,&e[i].len);
75        sort(e,e+m,cmp1);
76        for(i=0;i<q;i++)
77        {
78            scanf("%d",&A[i].value);
79            A[i].id=i;
80 
81        }
82        sort(A,A+q,cmp2);
83        int temp=0;
84        k=0;
85        for(i=0;i<q;i++)
86        {
87            while(k<m&&e[k].len<=A[i].value)
88            {
89                temp+=Unon(e[k].st,e[k].en);
90                k++;
91            }
92            ans[A[i].id]=temp;
93        }
94        for(i=0;i<q;i++)
95        {
96            printf("%d\n",ans[i]);
97        }
98    }
99 }
View Code

 

转载于:https://www.cnblogs.com/sdau--codeants/p/3401460.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值