zoukankan      html  css  js  c++  java
  • BZOJ 1083 [SCOI2005]繁忙的都市

    最小生成树模板。

    然后写并查集写了个fa[x]==find(fa[x]),RE。。。

    十分难受

    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #include<cmath>
    #include<queue>
    #include<vector>
    typedef long long LL; 
    const int maxn=40000+299;
    using namespace std;
    int n,m,u,v,w,fa[maxn],tot,ans;
    struct edge {
        int u,v,w;
       friend bool operator <(const edge&a,const edge &b) {
            return a.w<b.w;
        }
    }e[maxn];
    int find(int x) {return x==fa[x]?x:fa[x]=find(fa[x]); }
    int main()
    {
        scanf("%d%d",&n,&m);
        for(int i=1;i<=m;i++) 
        scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].w);
        sort(e+1,e+m+1);
        for(int i=1;i<=n;i++) fa[i]=i;
        for(int i=1;i<=m;i++) {
            u=e[i].u; v=e[i].v;
            int fau=find(u),fav=find(v);
            if(fau!=fav) {
                tot++;
                fa[fau]=fav;
                if(tot==n-1) {ans=e[i].w;break;}
            }
        }
        printf("%d %d
    ",tot,ans);
        return 0;
    }
    View Code
  • 相关阅读:
    OSX中zsh新增环境变量
    新的开始 春光明媚
    tmux
    继承
    6
    Object类
    网页收藏
    画王八
    ES6 语法之import export
    ES6 语法 之 destructuring
  • 原文地址:https://www.cnblogs.com/Achenchen/p/7541328.html
Copyright © 2011-2022 走看看