• [SCOI2005]繁忙的都市


    OJ题号:BZOJ1083、洛谷2330

    思路:Kruskal。

     1 #include<cstdio>
     2 #include<utility>
     3 #include<algorithm>
     4 #define w first
     5 #define u second.first
     6 #define v second.second
     7 typedef std::pair<int,std::pair<int,int> > Edge;
     8 const int N=301;
     9 class UnionFindSet {
    10     private:
    11         int anc[N];
    12         int Find(int x) {
    13             return (x==anc[x])?x:(anc[x]=Find(anc[x]));
    14         }
    15     public:
    16         UnionFindSet(int n) {
    17             for(int i=1;i<=n;i++) anc[i]=i;
    18         }
    19         void Union(int x,int y) {
    20             anc[Find(y)]=Find(x);
    21         }
    22         bool isConnected(int x,int y) {
    23             return Find(x)==Find(y);
    24         }
    25 };
    26 int main() {
    27     int n,m;
    28     scanf("%d%d",&n,&m);
    29     Edge e[m];
    30     for(int i=0;i<m;i++) {
    31         int a,b,c;
    32         scanf("%d%d%d",&a,&b,&c);
    33         e[i]=std::make_pair(c,std::make_pair(a,b));
    34     }
    35     std::sort(&e[0],&e[m]);
    36     UnionFindSet s(n);
    37     int ans=0,max=0;
    38     for(int i=0;i<m;i++) {
    39         if(s.isConnected(e[i].u,e[i].v)) continue;
    40         s.Union(e[i].u,e[i].v);
    41         max=std::max(max,e[i].w);
    42         ans++;
    43     }
    44     printf("%d %d
    ",ans,max);
    45     return 0;
    46 }
  • 相关阅读:
    自定义指令
    freemarker 数据类型
    hibernate简单的增删改查
    hibernate增删改查
    Hibernate HQL查询语句总结
    Hibernate配置详细解释
    Filter过滤器
    日志统计
    分页-模糊查询
    分页
  • 原文地址:https://www.cnblogs.com/skylee03/p/6971549.html
Copyright © 2020-2023  润新知