POJ 1679 The Unique MST
来源:互联网 发布:page 知页还是面 编辑:程序博客网 时间:2024/06/06 09:00
这个题除了求最小生成树之外,而且要求树的形状唯一。
我在求唯一性的时候是这样做的:如果一个顶点u,他的邻接点有两个分别是v1和v2 ,如果find(v1) == find(v2) && cost(u->v1) == cost(u->v2).那个这个生成树就不唯一。当然需要枚举所有顶点。复杂度高了一些。。
/*********************************************** * Author: fisty * Created Time: 2015/2/28 14:59:19 * File Name : K.cpp *********************************************** */#include <iostream>#include <cstring>#include <deque>#include <cmath>#include <queue>#include <stack>#include <list>#include <map>#include <set>#include <string>#include <vector>#include <cstdio>#include <bitset>#include <algorithm>using namespace std;#define Debug(x) cout << #x << " " << x <<endl#define Memset(x, a) memset(x, a, sizeof(x))const int INF = 0x3f3f3f3f;typedef long long LL;typedef pair<int, int> P;#define FOR(i, a, b) for(int i = a;i < b; i++)#define MAX_N 10010int n, m;int cnt;bool ok;struct edge{ int u; int v; int cost;}G[MAX_N];struct node{ int v; int cost; node(int _v, int _cost) : v(_v), cost(_cost){}};vector<node> _G[MAX_N]; //判断是否唯一void addedge(int u, int v, int cost){ G[cnt].u = u; G[cnt].v = v; G[cnt].cost = cost; cnt++;}bool cmp(edge e1, edge e2){ return e1.cost < e2.cost;}int par[MAX_N];void init(){ for(int i = 0;i <= n; i++){ par[i] = i; }}int find(int x){ if(x == par[x]) return x; return par[x] = find(par[x]);}void unio(int x, int y){ x = find(x); y = find(y); if(x != y){ par[x] = y; }}int kurskal(){ sort(G, G + cnt, cmp); int ans = 0; init(); for(int i = 0;i < cnt; i++){ edge e = G[i]; if(find(e.u) != find(e.v)){ unio(e.u, e.v); ans += e.cost; } } return ans;}bool unique(){ for(int i = 1;i <= n; i++){ for(int j = 0;j < _G[i].size(); j++){ for(int k = 0;k < _G[i].size(); k++){ if(j != k){ node e1 = _G[i][j], e2 = _G[i][k]; if(find(e1.v) == find(e2.v) && e1.cost == e2.cost) return false; } } } } return true;}int main() { //freopen("in.cpp", "r", stdin); cin.tie(0); ios::sync_with_stdio(false); int t; cin >> t; while(t--){ cnt = 0; Memset(G, 0); cin >> n >> m; FOR(i, 1, n+1){ _G[i].clear(); } FOR(i, 0, m){ int u, v, cost; cin >> u >> v >> cost; addedge(u, v, cost); _G[u].push_back(node(v, cost)); _G[v].push_back(node(u, cost)); } int ans = kurskal(); ok = unique(); if(!ok){ cout << "Not Unique!" << endl; }else{ cout << ans << endl; } } return 0;}
0 0
- POJ 1679 The Unique MST
- poj 1679 The Unique MST
- poj 1679 The Unique MST
- POJ 1679 The Unique MST
- poj 1679 The Unique MST
- poj 1679 The Unique MST
- POJ-1679 The Unique MST
- POJ 1679 The Unique MST
- POJ 1679 The Unique MST
- POJ-1679-The Unique MST
- Poj 1679 The Unique MST
- poj 1679 The Unique MST
- POJ-1679-The Unique MST
- POJ 1679 The Unique MST
- POJ 1679 The Unique MST
- POJ-1679-The Unique MST
- poj 1679 The Unique MST
- poj 1679-The Unique MST
- POJ 3026 Borg Maze (BFS+MST)
- OC中数组套数组、字典套字典、数组套字典、字典套数组的代码示例
- EXCEL完成多条件多结果查找&多行多列变一列
- Struts2-图片验证码
- 阿J的cocos2d-x学习笔记-元素消消看(三)-菜单的创建
- POJ 1679 The Unique MST
- 蓝的成长记——追逐DBA(13):协调硬件厂商,六个故事:所见所感的“服务器、存储、交换机......”
- 欢迎使用CSDN-markdown编辑器
- java根据jar包反编译后修改再打包回jar的做法
- QlikView处理数据
- javaweb监听器
- iOS钥匙串
- javascript正则表达式修饰符之multiline(/m)的使用
- poj 2388 Who's in the Middle