zoukankan      html  css  js  c++  java
  • [SCOI2005]繁忙的都市 (最小生成树)

    题目链接


    Solution

    裸的最小生成树.

    Code

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn=500008;
    struct sj{int to,fr,w;}a[maxn];
    bool cmp(sj x,sj y)
    {return x.w<y.w;}
    int n,fa[maxn],ans,m;
    int find(int x)
    {if(x==fa[x])return x;else return fa[x]=find(fa[x]);}
    void join(int x,int y)
    {x=find(x); y=find(y);if(x!=y)fa[x]=y;}
    int read()
    {
        char ch=getchar(); int f=1,w=0;
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){w=w*10+ch-'0';ch=getchar();}
        return f*w;
    }
    
    int main()
    {
        n=read(); m=read();
        for(int i=1;i<=n;i++)
        fa[i]=i;
        for(int i=1;i<=m;i++)
        {a[i].fr=read();a[i].to=read();a[i].w=read();}
        sort(a+1,a+m+1,cmp);
        for(int i=1;i<=m;i++)
        {
            if(find(a[i].fr)!=find(a[i].to))
            join(a[i].fr,a[i].to),ans=max(ans,a[i].w);
        }
        cout<<n-1<<' '<<ans<<endl;
    }
    
    
  • 相关阅读:
    如何分配资源和管理资源
    让Project把周六和周日也计算工期
    Project设置子任务
    pytest-配置文件
    pytest-fixture
    pytest-标记
    pytest-断言
    pytest-参数化
    pytest入门
    maven-插件
  • 原文地址:https://www.cnblogs.com/Kv-Stalin/p/9590843.html
Copyright © 2011-2022 走看看