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

[SCOI2005]繁忙的都市

时间:2017-06-09 14:08:13      阅读:182      评论:0      收藏:0      [点我收藏+]

标签:union   return   i++   nbsp   set   algorithm   make   cond   oid   

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\n",ans,max);
45     return 0;
46 }

 

[SCOI2005]繁忙的都市

标签:union   return   i++   nbsp   set   algorithm   make   cond   oid   

原文地址:http://www.cnblogs.com/skylee03/p/6971549.html

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