码迷,mamicode.com
首页 > 其他好文 > 详细

修复公路

时间:2017-10-23 23:04:20      阅读:225      评论:0      收藏:0      [点我收藏+]

标签:记录   最大值   开始   using   cstring   ons   最大   ios   pre   

今天就写并查集了。

原题链接:https://www.luogu.org/problemnew/show/1111#sub

题号好评(删

一开始没看出来,以为只需要把所有边排个序然后用并查集维护最后输出一个时间最大值就好。

后来一想,这特么不是kruskal吗?只不过是记录一下边权的最大值就好。

(为什么记录边权最大值?因为修复是同时进行的)

余大惊,遂随手A之。

参考代码:

 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdio>
 4 #include <algorithm>
 5 #define maxn 10000005
 6 using namespace std;
 7 struct Edge{
 8     int from,to,dis;
 9     bool operator<(const Edge &rhs)const{
10         return dis < rhs.dis;
11     }
12 };
13 Edge edge[maxn];
14 int n,m,ans;
15 inline int read(){
16     int num = 0;
17     char c;
18     while ((c = getchar()) ==   || c == \n || c == \r);
19     num = c - 0;
20     while (isdigit(c = getchar()))
21         num = num * 10 + c - 0;
22     return num;
23 }
24 int father[maxn];
25 void init(){
26     for (int i=1;i<=n;i++)
27         father[i] = i;
28 }
29 int find(int x){
30     if (father[x] == x)
31         return father[x];
32     return father[x] = find(father[x]);
33 }
34 void merge(int x,int y){
35     father[find(x)] = find(y);
36 }
37 
38 
39 int main(){
40     n = read();m = read();
41     for (register int i=1;i<=m;i++){
42         edge[i].from = read();
43         edge[i].to = read();
44         edge[i].dis = read();
45     }
46     int totedge = 0;
47     sort(edge+1,edge+m+1);
48     init();
49     for (register int i=1;i<=m;i++){
50         if (find(edge[i].from)!=find(edge[i].to)){
51             ans=max(ans,edge[i].dis);
52             merge(edge[i].from,edge[i].to);
53             totedge++;
54         }
55     }
56     if (totedge < n-1)
57         printf("-1");
58     else
59         printf("%d",ans);
60     return 0;
61 }

 

修复公路

标签:记录   最大值   开始   using   cstring   ons   最大   ios   pre   

原文地址:http://www.cnblogs.com/OIerShawnZhou/p/7719554.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!