标签:标记 tput lse namespace use lag style bin str
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!
这题直接暴力枚举,找到一颗最小生成树,标记一下,依次删边
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <stack> 5 #include <string> 6 #include <math.h> 7 using namespace std; 8 const int maxn = 1010 ; 9 const int INF = 0x7fffffff; 10 struct node { 11 int u, v, w; 12 int used, num, flag; 13 } qu[10 * maxn]; 14 int fa[maxn], n, m; 15 int cmp(node a, node b) { 16 return a.w < b.w; 17 } 18 void init() { 19 for (int i = 0 ; i <= n ; i++) fa[i] = i; 20 } 21 int Find(int x) { 22 return fa[x] == x ? x : fa[x] = Find(fa[x]); 23 } 24 int combine(int x, int y) { 25 int nx = Find(x); 26 int ny = Find(y); 27 if (nx != ny) { 28 fa[nx] = ny; 29 return 1; 30 } 31 return 0; 32 } 33 int kruskal(int flag) { 34 init(); 35 int sum = 0, cnt = 0; 36 for (int i = 0 ; i < m ; i++) { 37 if (qu[i].flag) continue; 38 if (combine(qu[i].v, qu[i].u)) { 39 if (!flag) qu[i].used = 1; 40 sum += qu[i].w; 41 cnt++; 42 if (cnt == n - 1) break; 43 } 44 } 45 if (cnt != n - 1) return -1; 46 return sum; 47 } 48 int main() { 49 int t; 50 scanf("%d", &t); 51 while(t--) { 52 scanf("%d%d", &n, &m); 53 for (int i = 0 ; i < m ; i++) { 54 scanf("%d%d%d", &qu[i].u, &qu[i].v, &qu[i].w); 55 qu[i].flag = 0, qu[i].used = 0; 56 } 57 sort(qu, qu + m, cmp); 58 int sum = kruskal(0); 59 int flag = 0; 60 for (int i = 0 ; i < m ; i++) { 61 if (qu[i].used ) { 62 qu[i].flag = 1; 63 int temp = kruskal(1); 64 qu[i].flag = 0; 65 if (temp == sum) { 66 flag = 1; 67 break; 68 } 69 } 70 } 71 if (flag) printf("Not Unique!\n"); 72 else printf("%d\n", sum); 73 } 74 return 0; 75 }
标签:标记 tput lse namespace use lag style bin str
原文地址:https://www.cnblogs.com/qldabiaoge/p/9080058.html