标签:
题目大意:给定一个联通的无向图,求该图的一个生成树,使得边权最大的边最小。
第一问的答案显然是N-1咯~
第二问可以考虑二分答案,二分最大的边权再用并查集验证是否可行。
但是实际上并不需要这么麻烦。因为有一个定理:一颗最小生成树必定是一颗最小瓶颈生成树(最小瓶颈生成树就是边权最大的边最小的生成树)。
因此对于第二问只要裸做一遍MST就好辣~
1 #include <cstdio> 2 #include <algorithm> 3 4 using namespace std; 5 6 const size_t Max_N(305); 7 const size_t Max_C(10050); 8 9 struct edge 10 { 11 unsigned int u, v; 12 unsigned int w; 13 }; 14 15 inline 16 bool comp(const edge &a, const edge &b) 17 { 18 return a.w < b.w; 19 } 20 21 size_t N; 22 size_t C; 23 edge V[Max_C]; 24 unsigned int Father[Max_N]; 25 26 unsigned int Ans; 27 28 unsigned int Get_Father(const unsigned int &x) 29 { 30 return Father[x] == x ? x : Father[x] = Get_Father(Father[x]); 31 } 32 33 int main() 34 { 35 scanf("%u%u", &N, &C); 36 for (size_t i = 1;i <= N;++i) 37 Father[i] = i; 38 for (size_t i = 1;i <= C;++i) 39 scanf("%u%u%u", &V[i].u, &V[i].v, &V[i].w); 40 sort(V + 1, V + 1 + C, comp); 41 42 for (size_t i = 1;i <= C;++i) 43 if (Get_Father(V[i].u) != Get_Father(V[i].v)) 44 { 45 Father[Get_Father(V[i].v)] = Get_Father(V[i].u); 46 Ans = V[i].w; 47 } 48 49 printf("%u %u", N - 1, Ans); 50 51 return 0; 52 }
标签:
原文地址:http://www.cnblogs.com/Created-equal/p/5021772.html