zoukankan      html  css  js  c++  java
  • 修复公路

    今天就写并查集了。

    原题链接: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 == '
    ' || c == '
    ');
    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 }
  • 相关阅读:
    旅行
    赛道修建
    逃学的小孩
    hdu4035_概率dp
    hdu4405_概率dp
    poj2096_概率dp
    poj3420_找规律+矩阵快速幂
    poj2411_状压dp
    poj3744_矩阵快速幂
    hdu5720_贪心
  • 原文地址:https://www.cnblogs.com/OIerShawnZhou/p/7719554.html
Copyright © 2011-2022 走看看