标签:memset follow algorithm iostream return string lan span can
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 35999 | Accepted: 13145 |
Description
Input
Output
Sample Input
2 3 3 1 2 1 2 3 2 3 1 3 4 4 1 2 2 2 3 2 3 4 2 4 1 2
Sample Output
3 Not Unique!
C/C++:
1 #include <map> 2 #include <queue> 3 #include <cmath> 4 #include <vector> 5 #include <string> 6 #include <cstdio> 7 #include <cstring> 8 #include <climits> 9 #include <iostream> 10 #include <algorithm> 11 #define INF 0x3f3f3f3f 12 using namespace std; 13 const int my_max_edge = 10010, my_max_node = 110; 14 15 int t, n, m, my_book_edge[my_max_edge], my_pre[my_max_node], my_first; 16 17 struct edge 18 { 19 int a, b, val; 20 }P[my_max_edge]; 21 22 bool cmp(edge a, edge b) 23 { 24 return a.val < b.val; 25 } 26 27 int my_find(int x) 28 { 29 int n = x; 30 while (n != my_pre[n]) 31 n = my_pre[n]; 32 int i = x, j; 33 while (n != my_pre[i]) 34 { 35 j = my_pre[i]; 36 my_pre[i] = n; 37 i = j; 38 } 39 return n; 40 } 41 42 int my_kruskal(int my_flag) 43 { 44 int my_ans = 0; 45 for (int i = 1; i <= n; ++ i) 46 my_pre[i] = i; 47 48 for (int i = 0; i < m; ++ i) 49 { 50 int n1 = my_find(P[i].a), n2 = my_find(P[i].b); 51 if (n1 == n2 || my_flag == i) continue; 52 my_pre[n1] = n2; 53 if (my_first)my_book_edge[i] = 1; 54 my_ans += P[i].val; 55 } 56 57 int temp = my_find(1); 58 for (int i = 2; i <= n; ++ i) 59 if (temp != my_find(i)) 60 return -1; 61 return my_ans; 62 } 63 64 int main() 65 { 66 scanf("%d", &t); 67 while (t --) 68 { 69 scanf("%d%d", &n, &m); 70 for (int i = 0; i < m; ++ i) 71 scanf("%d%d%d", &P[i].a, &P[i].b, &P[i].val); 72 sort(P, P + m, cmp); 73 memset(my_book_edge, 0, sizeof(my_book_edge)); 74 75 my_first = 1; 76 int mst = my_kruskal(-1), flag = 1; 77 if (mst == -1) 78 { 79 printf("0\n"); 80 continue; 81 } 82 my_first = 0; 83 for (int i = 0; i < m; ++ i) 84 { 85 if (my_book_edge[i]) 86 {; 87 if (mst == my_kruskal(i)) 88 { 89 printf("Not Unique!\n"); 90 flag = 0; 91 break; 92 } 93 } 94 } 95 if (flag) printf("%d\n", mst); 96 } 97 return 0; 98 }
poj 1679 The Unique MST (次小生成树(sec_mst)【kruskal】)
标签:memset follow algorithm iostream return string lan span can
原文地址:https://www.cnblogs.com/GetcharZp/p/9497554.html