[SCOI2005]繁忙的都市

嘟嘟嘟

 

裸的最小生成树。

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<cstring>
 5 #include<cmath>
 6 #include<cstdlib>
 7 #include<vector>
 8 #include<queue>
 9 #include<stack>
10 #include<cctype>
11 using namespace std;
12 #define enter puts("")
13 #define space putchar(' ')
14 #define Mem(a) memset(a, 0, sizeof(a))
15 typedef long long ll;
16 typedef double db;
17 const int INF = 0x3f3f3f3f;
18 const db eps = 1e-8;
19 const int maxn = 5e4 + 5;
20 inline ll read()
21 {
22     ll ans = 0;
23     char ch = getchar(), last = ' ';
24     while(!isdigit(ch)) {last = ch; ch = getchar();}
25     while(isdigit(ch)) {ans = ans * 10 + ch - '0'; ch = getchar();}
26     if(last == '-') ans = -ans;
27     return ans;
28 }
29 inline void write(ll x)
30 {
31     if(x < 0) putchar('-'), x = -x;
32     if(x >= 10) write(x / 10);
33     putchar(x % 10 + '0');
34 }
35 
36 int n, m;
37 struct Node
38 {
39     int x, y, w;
40     bool operator < (const Node& other)const
41     {
42         return w < other.w;
43     }
44 }t[maxn];
45 
46 int Max = 0, cnt = 0;
47 
48 int p[maxn];
49 void init()
50 {
51     for(int i = 1; i <= n; ++i) p[i] = i;
52 }
53 int Find(int x)
54 {
55     return x == p[x] ? x : p[x] = Find(p[x]);
56 }
57 
58 int main()
59 {
60     n = read(); m = read();
61     init();
62     for(int i = 1; i <= m; ++i) {t[i].x = read(); t[i].y = read(); t[i].w = read();}
63     sort(t + 1, t + m + 1);
64     for(int i = 1; i <= m; ++i)
65     {
66         int px = Find(t[i].x), py = Find(t[i].y);
67         if(px != py) 
68         {
69             p[px] = py;
70             cnt++; Max = t[i].w;
71             if(cnt == n - 1) break;
72         }
73     }
74     write(n - 1); space; write(Max); enter;
75     return 0;
76 }
View Code

 

posted @ 2018-08-18 10:31  mrclr  阅读(184)  评论(0编辑  收藏  举报