poj1679+次小生成树

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!

题目大意就是求给出的树是否存在唯一的最小生成树。我们可以求出它的次小生成树,如果次小生成树的权等于最小生成树的权,则不唯一。否则唯一。
 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 using namespace std;
 5 const int mmax=1<<30;
 6 int g[110][110],maxval[110][110],intree[110],eintree[110][110];
 7 int prim(int n)
 8 {
 9     intree[1]=1;
10     int sum=0,i,j,num=0;
11     while(num<n)//朴素prim算法
12     {
13         int mine=mmax,v1,v2;
14         for(i=1;i<=n;i++)
15         {
16             if(intree[i]==1)
17             {
18                 for(j=1;j<=n;j++)
19                 {
20                     if(i!=j&&intree[j]==0)
21                     {
22                         if(g[i][j]<mine)
23                         {
24                             mine=g[i][j];
25                             v1=i;
26                             v2=j;
27                         }
28                     }
29                 }
30             }
31         }
32         if(mine==mmax) break;
33         sum+=mine;
34         num++;
35         intree[v2]=1;
36         eintree[v1][v2]=1;
37         //在prim的同时,用一个矩阵max_val[u][v] 记录在最小生成树(T)中连结任意两点u,v的唯一的路中权值最大的那条边的权值。
38         maxval[v1][v2]=maxval[v2][v1]=g[v1][v2];//这是T中与新加入点直接相连的点
39         for(i=1;i<=n;i++)//T中所有与新加入点间接相连的点
40         {
41             if(intree[i]&&i!=v2&&i!=v1)
42                     maxval[i][v2]=maxval[v2][i]=max(maxval[i][v1],maxval[v1][v2]);
43         }
44     }
45     if(num<n-1) return -1;
46     else return sum;
47 }
48 int main()
49 {
50     int cas;
51     scanf("%d",&cas);
52     while(cas--)
53     {
54         int n,m,i,j;
55         scanf("%d%d",&n,&m);
56         for(i=1;i<=n;i++)
57         {
58             for(j=1;j<=n;j++)
59             {
60                 if(i==j) g[i][j]=0;
61                 else g[i][j]=mmax;
62             }
63         }
64         memset(intree,0,sizeof(intree));//标记点是否在树中
65         memset(eintree,0,sizeof(eintree));//标记边是否在树中
66         memset(maxval,0,sizeof(maxval));
67         for(i=0;i<m;i++)
68         {
69             int a,b,c;
70             scanf("%d%d%d",&a,&b,&c);
71             if(c<g[a][b])
72                 g[a][b]=g[b][a]=c;
73         }
74         int ans=prim(n);
75         int ci=mmax;//ci表示次小生成树的值
76        //枚举所有不在T中的边uv,加入边uv则必然替换权为max_val[u][v]的边,枚举一次就得到一棵新的生成树,如果在这些生成树中有权值和与原最小生成树相等的,则最小生成树不唯一
77         for(i=1;i<=n;i++)
78         {
79             for(j=i+1;j<=n;j++)
80             {
81                 if(!eintree[i][j]&&g[i][j]!=mmax)
82                 {
83                     //printf("%d,%d\n",g[i][j],maxval[i][j]);
84                     int aans=ans+g[i][j]-maxval[i][j];
85                     if(aans<ci) ci=aans;
86                 }
87             }
88         }
89         if(ci==ans) printf("Not Unique!\n");
90         else printf("%d\n",ans);
91     }
92     return 0;
93 }

				
时间: 2024-12-28 12:14:50

poj1679+次小生成树的相关文章

poj1679次小生成树入门题

次小生成树求法:例如求最小生成树用到了 1.2.4这三条边,总共5条边,那循环3次的时候,每次分别不用1.2.4求得最小生成树的MST,最小的MST即为次小生成树 如下代码maxx即每次删去1,2,4边之后求得的最大边 #include<map> #include<set> #include<cmath> #include<queue> #include<stack> #include<vector> #include<cstd

POJ1679(次小生成树)

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

POJ1679 The Unique MST 【次小生成树】

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

次小生成树(POJ1679/CDOJ1959)

POJ1679 首先求出最小生成树,记录权值之和为MinST.然后枚举添加边(u,v),加上后必形成一个环,找到环上非(u,v)边的权值最大的边,把它删除,计算当前生成树的权值之和,取所有枚举加边后生成树权值之和的最小值 思路: 最小生成树唯一性判断,求次小生成树的val再与最小生成树比较.1.先用prim算法求出最小生成树.并在其过程中保存加入到MST中的Max[i][j]( i 到 j 路径中的最大边 ) 2.对未加入MST的边进行遍历:从MST中去掉i j路径中的最大边,加入未访问边G[i

[POJ1679]The Unique MST 次小生成树

题目链接:http://poj.org/problem?id=1679 给你一个图的连通情况,询问你此图的最小生成树是否唯一. 假如最小生成树唯一,即生成树连通所有节点的权值和唯一.假如不唯一,那么存在另一条最小生成树使得权值等于之前最小生成树的权值. 换个思路考虑,也就是次小生成树的权值与最小生成树的权值相同,那么问题就变成了求次小生成树的权值. 我选择的是先求出最小生成树,将树上用到的边都保存下来.接着分别将每一条用到的边摘下来,再求一次最小生成树.假如不包含当前删掉的边生成的生成树的所选边

POJ1679 The Unique MST【Kruskal】【次小生成树】

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

poj1679——The Unique MST(次小生成树,Kruskal)

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

次小生成树

次小生成树:所谓的次小生成树,是指在边集与某一最小生成树的边集不完全相同的其它生成树中值最小的那个.因此在数值上,最小生成树可能会等于次小生成树,这种情况也可以看做最小生成数. 思路:最直观的解法是,首先求出最小生成树,并且记录最小生成树中的边,然后再枚举删除最小生成树的边并同时求最小生成树. 以POJ1679为例,Kruskal求次小生成树代码如下: 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm>

次小生成树的模版

求次小生成树的步骤是: 1.求出最小生成树MST,用一个矩阵maxe[u][v]记录在MST中连接u-v的路径中权值最大的边. 2.枚举所有不在T中的边u-v,加入边u-v,删除权值为max[u][v]的边,不断枚举找到次小生成树. 由于邻接矩阵建图无法储存平行边(重边),我们在建图的时候都是取当前最小值.所以用prim做的话,很可能出错.但是prim也是有市场的,当没有平行边,并且定点数比较少的时候,用prim就有优势.如果有平行边,就必须用kruskal.用链式前向星建图. 下面是两个模版,