算法设计 第二次上机 The Unique MST
来源:互联网 发布:2017人工智能产业规模 编辑:程序博客网 时间:2024/05/16 11:46
- 总时间限制:
- 1000ms
- 内存限制:
- 65536kB
- 描述
- 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'. - 输入
- 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.
- 输出
- For each input, if the MST is unique, print the total cost of it, or otherwise print the string 'Not Unique!'.
- 样例输入
23 31 2 12 3 23 1 34 41 2 22 3 23 4 24 1 2
- 样例输出
3Not Unique!
- 来源
- POJ Monthly--2004.06.27 srbga@POJ
需要生成最小生成树之后,遍历去掉树上的每一条边,看生成的最小生成树是否与原本的最小生成树权重相同。
注意需要考虑当图是一棵树,并且每条边的长度都为0时的情况。
sort()默认从小到大排序。
#include <iostream>#include <stdio.h>#include <string.h>#include <stdlib.h>#include <climits>#include <vector>#include <algorithm>using namespace std;struct node{public: int u, v, w; bool operator < (node b) { return w < b.w; }}graph[10005];int father[105];bool MST[10005];int n, m;int findfather(int x){ if(father[x] != x) { father[x] = findfather(father[x]); } return father[x];}int kruskal(int e){ int cost = 0; int num = 0; for(int j = 1; j <= n; j++) { father[j] = j; } for(int j = 0; j < m; j++) { if(num >= n - 1) break; if(j == e) continue; int x = graph[j].u; int y = graph[j].v; if(findfather(x) != findfather(y)) { father[father[x]] = father[y]; cost += graph[j].w; num++; //cout << "j:" << j << "\tcost:" << cost << endl; if(e == -1) MST[j] = true; } } return cost;}int main(){ //freopen("test.txt", "r", stdin); ios::sync_with_stdio(false); int t; scanf("%d", &t); while(t--) { scanf("%d%d", &n, &m); memset(MST, false, sizeof(MST)); for(int i = 0; i < m; i++) { int xi, yi, wi; scanf("%d%d%d", &xi, &yi, &wi); graph[i].u = xi; graph[i].v = yi; graph[i].w = wi; } sort(graph, graph + m); int cost1 = kruskal(-1); int cost2; bool flag = false; if(m == n - 1) { printf("%d\n", cost1); continue; } for(int i = 0; i < m; i++) { if(MST[i]) { cost2 = kruskal(i); if(cost1 == cost2) { flag = true; break; } } } if(flag) printf("Not Unique!\n"); else printf("%d\n", cost1); } return 0;}
阅读全文
0 0
- 算法设计 第二次上机 The Unique MST
- 算法设计 第二次上机 Subway
- The Unique MST
- pku1679 The Unique MST
- PKU1679 the unique mst
- POJ1679:The Unique MST
- The Unique MST
- The Unique MST(P1679)
- poj_1679 The Unique MST
- POJ1679 The Unique MST
- 【hdu】 The Unique MST
- poj1679 - The Unique MST
- POJ1679 The Unique MST
- poj The Unique MST
- poj1679 The Unique MST
- The Unique MST
- poj1679 The Unique MST
- The Unique MST
- 编程作业50页4题 编写应用程序求1000 之内的所有完数
- 欲写vba代码 必先创Sub
- git简介及常用命令
- 一些javaweb开发常用工具类
- python: join函数
- 算法设计 第二次上机 The Unique MST
- 10-12-C++外包面试题
- spring中freemarker生成静态页面
- Hibernate 入门
- 设计模式_责任链模式(20)
- 如何正确地比较两个double类型的数据
- Firefox 恢复关闭的标签页 用什么插件?
- java se--8.多线程-1.基本概念
- 二叉树