POJ1679 The Unique MST

 

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 26782 Accepted: 9598

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!

Source

 

先跑一遍最小生成树,把树上边都记录下来。

然后枚举不使用其中一条边而再跑最小生成树,若答案没变,说明最小生成树不止一条。

注意数组大小←至少有十道题死在这个问题上了

用n估算的话5000最保险,实际上3000可AC

 

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<cmath>
 6 using namespace std;
 7 const int mxn=12000;
 8 int n,m;
 9 struct edge{
10     int x,y;
11     int v;
12 }e[mxn];
13 int tot;
14 int mst[mxn],cnt;
15 int cmp(const edge a,const edge b){
16     return a.v<b.v;
17 }
18 int fa[mxn];
19 void init(int x){
20     for(int i=1;i<=x;i++)fa[i]=i;return;
21 }
22 int find(int x){
23     if(fa[x]==x)return x;
24     return fa[x]=find(fa[x]);
25 }
26 void Kruskal(){
27 
28     init(n);
29     int i,j;
30     cnt=0;
31     int ans1=0;
32     tot=0;
33     for(i=1;i<=m;i++){
34         int x=find(e[i].x);int y=find(e[i].y);
35         if(x!=y){
36             fa[x]=y;
37             ans1+=e[i].v;
38             mst[++cnt]=i;
39             tot++;
40         }
41         if(tot==n-1)break;
42     }
43     //
44     int ans2;
45     for(int k=1;k<=cnt;k++){
46         tot=0; ans2=0;
47         init(n);
48         //init
49         for(i=1;i<=m;i++){
50             if(i==mst[k])continue;
51             int x=find(e[i].x);int y=find(e[i].y);
52             if(x!=y){
53                 fa[x]=y;
54                 ans2+=e[i].v;
55             //    mst[++cnt]=i;//!!这步不能加! 偷懒从上面复制的结果就是WA记录喜+1
56                 tot++;
57             }
58             if((tot==n-1) && ans1==ans2){
59                 printf("Not Unique!\n");
60                 return;
61             }
62         }
63     }
64     printf("%d\n",ans1);
65     return;
66 }
67 int main(){
68     int T;
69     scanf("%d",&T);
70     while(T--){
71         scanf("%d%d",&n,&m);
72         for(int i=1;i<=m;i++)
73             scanf("%d%d%d",&e[i].x,&e[i].y,&e[i].v);
74         sort(e+1,e+m+1,cmp);
75         Kruscal();
76     }
77     return 0;
78 }

 

转载于:https://www.cnblogs.com/SilverNebula/p/5670744.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值