POJ1679 The Unique MST(Kruskal)(最小生成树的唯一性)

The Unique MST

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 27141   Accepted: 9712

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!【分析】最小生成树的唯一性,思路是先判断每条边是否有重边,有的话eq=1,否则0.然后第一次求出最小生成树,将结果记录下来, 然后依次去掉第一次使用过的且含有重边的边,再求一次最小生成树,若结果与第一次结果一样,则不唯一。

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#include<functional>
#define mod 1000000007
#define inf 0x3f3f3f3f
#define pi acos(-1.0)
using namespace std;
typedef long long ll;
const int N=11000;
const int M=15005;
int n,m,cnt;
int parent[N];
bool flag;
struct man {
    int u,v,w;
    int eq,used,del;
} edg[N];
bool cmp(man g,man h) {
return g.w<h.w;
}
void init() {
    for(int i=0; i<=10005; i++) {
        parent[i]=i;
    }
}
int Find(int x) {
    if(parent[x] != x) parent[x] = Find(parent[x]);
    return parent[x];
}//查找并返回节点x所属集合的根节点
void Union(int x,int y) {
    x = Find(x);
    y = Find(y);
    if(x == y) return;
    parent[y] = x;
}//将两个不同集合的元素进行合并
int Kruskal() {
    init();
   int sum=0;
   int num=0;
   for(int i=0;i<m;i++){
    if(edg[i].del==1)continue;
    int u=edg[i].u;int v=edg[i].v;int w=edg[i].w;

    if(Find(u)!=Find(v)){
        sum+=w;
        if(!flag)edg[i].used=1;
        num++;
        Union(u,v);
    }
    if(num>=n-1)break;
   }
   return sum;
}
int main() {
    int t,d;
    cin>>t;
    while(t--) {
        cnt=0;
        cin>>n>>m;
        for(int i=0; i<m; i++) {
            cin>>edg[i].u>>edg[i].v>>edg[i].w;
            edg[i].del=0;
            edg[i].used=0;
            edg[i].eq=0;//一开始这个地方eq没有初始化,WA了好几发,操
        }
        for(int i=0;i<m;i++){
            for(int j=0;j<m;j++){
                if(i==j)continue;
                if(edg[i].w==edg[j].w)edg[i].eq=1;
            }
        }
        sort(edg,edg+m,cmp);
        flag=false;
        cnt=Kruskal();
        flag=true;
        bool gg=false;
        for(int i=0;i<m;i++){
            if(edg[i].used==1&&edg[i].eq==1){
                edg[i].del=1;
                int s=Kruskal();//printf("%d %d\n",i,s);
                if(s==cnt){
                    gg=true;
                    printf("Not Unique!\n");
                    break;
                }
                edg[i].del=0;
            }
        }
        if(!gg)cout<<cnt<<endl;
    }
    return 0;
}

时间: 2024-10-08 23:04:51

POJ1679 The Unique MST(Kruskal)(最小生成树的唯一性)的相关文章

POJ-1679 The Unique MST (判断最小生成树的唯一性)

<题目链接> 题目大意: 给定一张无向图,判断其最小生成树是否唯一. 解题分析: 对图中每条边,扫描其它边,如果存在相同权值的边,则标记该边:用kruskal求出MST. 如果MST中无标记的边,则该MST唯一:否则,在MST中依次去掉标记的边,再求MST,若求得MST权值和原来的MST 权值相同,则MST不唯一. #include <cstdio> #include <iostream> #include <cstring> #include <al

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 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): Con

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

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 (判定最小生成树是否唯一)

题目链接:http://poj.org/problem?id=1679 The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 29408   Accepted: 10520 Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definition 1 (Spannin

POJ 1679 The Unique MST 【最小生成树/次小生成树】

The Unique MST Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 22668   Accepted: 8038 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: 22715   Accepted: 8055 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(次小生成树,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