POJ1679------The Unique MST
来源:互联网 发布:枕头高度 知乎 编辑:程序博客网 时间:2024/05/17 00:06
The Unique MST
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 24371 Accepted: 8674
判断最小生成树是否唯一;
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'.
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
23 31 2 12 3 23 1 34 41 2 22 3 23 4 24 1 2
Sample Output
3Not Unique!
#include <iostream>#include<cstdio>#include<string>#include<vector>#include<iomanip>#include<cmath>#include<cctype>#include<climits>#include<cstring>#include<algorithm>#include<set>#include<map>#include<cstdlib>#define LL long long#define uint unsigned int#define uLL unsigned LL#pragma comment(linker,"/STACK:102400000,102400000")using namespace std;struct Edge{ int u,v,w; int equa,used,del; bool operator < (const Edge& x)const { return w<x.w; }}que[10010];int fa[110];bool first;int n,m;int fint(int x){ return fa[x]!=x?fa[x]=fint(fa[x]):x;}void Uset(){ for(int i=0;i<=n;i++) { fa[i]=i; }}int Kruscal(){ Uset(); int g,h; int sum=0,cnt=0; for(int i=0;i<m;i++) { if(que[i].del)continue; g=fint(que[i].u);h=fint(que[i].v); if(g!=h) { fa[g]=h; cnt++; sum+=que[i].w; if(first) que[i].used=1; if(cnt==n-1) break; } } return sum;}int main(){ ios::sync_with_stdio(0); int t; while(cin>>t) { while(t--) { cin>>n>>m; for(int i=0;i<m;i++) { scanf("%d%d%d",&que[i].u,&que[i].v,&que[i].w); que[i].del=que[i].equa=que[i].used=0; for(int j=0;j<i;j++) { if(que[i].w==que[j].w) que[i].equa=que[j].equa=1; } } sort(que,que+m); first =1 ; int weight1=Kruscal(),weight2; first=0; bool flag=1; for(int i=0;i<m;i++) { if(que[i].used&&que[i].equa==1) { que[i].del=1; weight2=Kruscal(); if(weight1==weight2) { flag=0; break; } que[i].del=0; } } if(flag) printf("%d\n",weight1); else printf("Not Unique!\n"); } } return 0;}
0 0
- POJ1679:The Unique MST
- POJ1679 The Unique MST
- poj1679 - The Unique MST
- POJ1679 The Unique MST
- poj1679 The Unique MST
- poj1679 The Unique MST
- POJ1679------The Unique MST
- POJ1679-The Unique MST
- poj1679 The Unique MST
- POJ1679 The Unique MST
- POJ1679 The Unique MST
- poj1679 The Unique MST
- POJ1679-The Unique MST
- POJ1679 The Unique MST
- poj1679 The Unique MST
- poj1679(The Unique MST)Kruskal
- POJ1679 The Unique MST(kru)
- poj1679——The Unique MST
- 操作系统中的proc系统文件
- iPhone第一节:视图控制器和基本视图
- C语言copy文件
- C语言(6)--国际象棋棋盘、螺旋矩阵、指定年份日历、打印ASCII码表
- Ajax提高篇(3)在请求和响应中使用 XML(1)
- POJ1679------The Unique MST
- 博客的起点
- 软工视频——结尾篇
- eclipse 看*.vm 文件里面的coding都是黑色怎么设置
- poj-3253
- 第5章 标准I/O库
- 学习JAVA过程中遇到的一些低级问题备忘记录
- 在浏览器上使用服务器字体文件的字体
- 03+在一个有序的二维数组中查找一个数