POJ 1679 prime次小生成树判定生成树是否唯一

The Unique MST

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 21931   Accepted: 7784

Description

Given a connected undirected graph, tell if its minimum spanning tree is unique.

Definition 1 (Spanning Tree): Consider a connected, undirected graph G = (V, E). A spanning tree of G is a subgraph of G, say T = (V‘, E‘), with the following properties: 
1. V‘ = V. 
2. T is connected and acyclic.

Definition 2 (Minimum Spanning Tree): Consider an edge-weighted, connected, undirected graph G = (V, E). The minimum spanning tree T = (V, E‘) of G is the spanning tree that has the smallest total cost. The total cost of T means the sum of the weights on all the edges in E‘.

Input

The first line contains a single integer t (1 <= t <= 20), the number of test cases. Each case represents a graph. It begins with a line containing two integers n and m (1 <= n <= 100), the number of nodes and edges. Each of the following m lines contains a triple (xi, yi, wi), indicating that xi and yi are connected by an edge with weight = wi. For any two nodes, there is at most one edge connecting them.

Output

For each input, if the MST is unique, print the total cost of it, or otherwise print the string ‘Not Unique!‘.

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!

题目意思:

给一个图,判定最小生成树是否唯一。prime次小生成树模板判定。

代码:

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <iostream>
 5 #include <vector>
 6 #include <queue>
 7 #include <cmath>
 8 using namespace std;
 9
10 #define N 105
11 #define inf 999999999
12
13 int max(int x,int y){return x>y?x:y;}
14 int min(int x,int y){return x<y?x:y;}
15 int abs(int x,int y){return x<0?-x:x;}
16
17 int maxh[N][N];
18 int map[N][N];
19 int fa[N];
20 bool in[N];
21 int stack[N];
22 int n, m;
23 int dis[N];
24
25 int prime(){
26     int sum=0;
27     int top=0;
28     int i, j, k;
29     for(i=2;i<=n;i++){
30         dis[i]=map[1][i];fa[i]=1;
31     }
32     memset(in,false,sizeof(in));
33     dis[1]=0;
34
35     in[1]=true;
36     stack[top++]=1;
37     for(i=1;i<=n;i++){
38         int minh=-1;
39         for(j=1;j<=n;j++){
40             if(!in[j]&&(minh==-1||minh>dis[j])){
41                 k=j;minh=dis[j];
42             }
43
44         }//printf("%d %d\n",minh,sum);
45         if(minh==-1) return sum;
46
47         in[k]=true;
48         sum+=minh;
49
50         for(j=0;j<top;j++) maxh[stack[j]][k]=maxh[k][stack[j]]=max(maxh[stack[j]][fa[k]],minh);
51         stack[top++]=k;
52         for(j=1;j<=n;j++){
53             if(!in[j]&&dis[j]>map[k][j]){
54                 dis[j]=map[k][j];
55                 fa[j]=k;
56             }
57         }
58     }
59 }
60
61 main()
62 {
63     int t, i, j, k;
64     cin>>t;
65     while(t--){
66         scanf("%d %d",&n,&m);
67         for(i=1;i<=n;i++){
68             for(j=1;j<=n;j++){
69                 map[i][j]=inf;
70                 maxh[i][j]=0;
71             }
72         }
73         int x, y, z;
74         while(m--){
75             scanf("%d %d %d",&x,&y,&z);
76             map[x][y]=map[y][x]=z;
77         }
78         int ans=prime();
79         //printf("%d\n",ans);
80         int minh=inf;
81         for(i=1;i<=n;i++){
82             for(j=1;j<=n;j++){
83                 if(i!=j&&fa[i]!=j&&fa[j]!=i)
84                     minh=min(minh,map[i][j]-maxh[i][j]);
85             }
86         }
87         if(minh==0) printf("Not Unique!\n");
88         else printf("%d\n",ans);
89     }
90 }
时间: 2024-12-21 11:37:52

POJ 1679 prime次小生成树判定生成树是否唯一的相关文章

poj 1679 The Unique MST (判断最小生成树是否唯一)

The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20679   Accepted: 7255 Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spanning Tree): Consider a connected, undire

POJ 1679 The Unique MST(求最小生成树是否唯一)

The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20430   Accepted: 7186 Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spanning Tree): Consider a connected, undire

POJ 1679 The Unique MST(判断最小生成树是否唯一)

题目链接: http://poj.org/problem?id=1679 Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spanning Tree): Consider a connected, undirected graph G = (V, E). A spanning tree of G is a subgraph of G

POJ 1679 The Unique MST (Kruskal 判最小生成树是否唯一)

The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 21646 Accepted: 7661 Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spanning Tree): Consider a connected, undirected

poj 1679 The Unique MST (次小生成树)

The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20293   Accepted: 7124 Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spanning Tree): Consider a connected, undire

POJ - 1679 The Unique MST (次小生成树)

Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spanning Tree): Consider a connected, undirected graph G = (V, E). A spanning tree of G is a subgraph of G, say T = (V', E'), with the followin

POJ 1679:The Unique MST(次小生成树&amp;&amp;Kruskal)

The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 19941   Accepted: 6999 Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spanning Tree): Consider a connected, undire

poj 1679 The Unique MST 【次小生成树+100的小数据量】

题目地址:http://poj.org/problem?id=1679 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! 分析:T组数据,每组n个节点m条边.计算一下,最小生成树是不是独一无二的,如果是就输出最小生成树的权值和,否则输出Not Unique!(不是独一无二的).先计算最小生成树,在计算次小生成树,判断两者的值是否相等!输入数据保证不存在重边. #include <stdi

poj 1679 The Unique MST 【次小生成树】【模板】

题目:poj 1679 The Unique MST 题意:给你一颗树,让你求最小生成树和次小生成树值是否相等. 分析:这个题目关键在于求解次小生成树. 方法是,依次枚举不在最小生成树上的边,然后添加到最小生成树上,然后把原树上添加了之后形成环的最长的边删去,知道一个最小的.就是次小生成树. 这些需要的都可以在求解最小生成树的时候处理出来. AC代码: #include <cstdio> #include <cstring> #include <iostream> #i