当前位置:网站首页>POJ-The Unique MST
POJ-The Unique MST
2022-04-23 06:46:00 【Round moon】
The Unique MST
Title 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:
- V’ = V.
- 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!
The main idea of the topic
Ask if the minimum spanning tree of this graph is unique .
To judge whether it is unique, we only need to compare its minimum spanning tree and non strict sub small spanning tree .
This problem is made for and non strict sub small spanning trees .
When we link , Make a note of , The weight of each element of two sets to each other . Because if you want to replace one side , We must know the weight of the replaced edge .
Well, the core part explains , Then continue to
Accepted Code
//#include<unordered_map>
#include<algorithm>
#include<iostream>
#include<string.h>
#include <iomanip>
#include<stdio.h>
#include<vector>
#include<string>
#include<math.h>
#include<cmath>
#include<queue>
#include<stack>
#include<deque>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll ll_inf = 9223372036854775807;
const int int_inf = 2147483647;
const short short_inf = 32767;
const ll less_inf = 0x3f3f3f3f;
const char char_inf = 127;
#pragma GCC optimize(2)
#define accelerate cin.tie(NULL);cout.tie(NULL);ios::sync_with_stdio(false);
#define PI 3.141592653589793
#define EPS 1.0e-8
ll gcd(ll a, ll b) {
return b ? gcd(b, a % b) : a;
}
ll lcm(ll a, ll b) {
return a / gcd(a, b) * b;
}
inline ll read() {
ll c = getchar(), Nig = 1, x = 0;
while (!isdigit(c) && c != '-')c = getchar();
if (c == '-')Nig = -1, c = getchar();
while (isdigit(c))x = ((x << 1) + (x << 3)) + (c ^ '0'), c = getchar();
return Nig * x;
}
inline void out(ll a) {
if (a < 0)putchar('-'), a = -a;
if (a > 9)out(a / 10);
putchar(a % 10 + '0');
}
ll qpow(ll x, ll n, ll mod) {
ll res = 1;
while (n > 0) {
if (n & 1)res = (res * x) % mod;
x = (x * x) % mod;
n >>= 1;
}
return res;
}
#define read read()
struct node
{
int sta, end, val;
bool friend operator<(node a, node b)
{
return a.val < b.val;
}
}save[10005];
int fa[105];
bool judge[10005];
int mp[105][105];
int find(int x)
{
return fa[x] == x ? x : fa[x] = find(fa[x]);
}
struct link {
int head, end, next;
}Link[105];
void merge(int x, int y)
{
int fx = find(x), fy = find(y);
if (fx != fy)fa[fx] = fy;
}
int main()
{
int T = read;
while (T--)
{
for (int i = 0; i < 105; i++)fa[i] = i;
memset(judge, 0, sizeof(judge));
memset(mp, 0, sizeof(mp));
for (int i = 0; i < 105; i++)
Link[i].head = Link[i].end = i, Link[i].next = -1;
int n = read, m = read;
for (int i = 0; i < m; i++)
save[i].sta = read, save[i].end = read, save[i].val = read;
sort(save, save + m);
int num = 0, cnt = 0;
int ans = 0;
for (int i = 0; i < m; i++)
{
if (find(save[i].sta) != find(save[i].end))
{
merge(save[i].sta, save[i].end);
ans += save[i].val;
judge[i] = true;
for (int u = Link[save[i].sta].head; ~u; u = Link[u].next)
for (int v = Link[save[i].end].head; ~v; v = Link[v].next)
mp[u][v] = mp[v][u] = save[i].val;
Link[Link[save[i].sta].end].next = Link[save[i].end].head;
Link[save[i].sta].end = Link[save[i].end].end;
Link[save[i].end].head = Link[save[i].sta].head;
}
}
int res = int_inf;
for (int i = 0; i < m; i++)
if (!judge[i])
res = min(res, ans - mp[save[i].sta][save[i].end] + save[i].val);
if (ans == res)puts("Not Unique!");
else
{
out(ans);
puts("");
}
}
}
By-Round Moon
版权声明
本文为[Round moon]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230549499322.html
边栏推荐
猜你喜欢
CUDA project encountered a series of compilation problems after changing the environment (computer)
基于VGG卷积神经网络的图像识别代码实现
VHDL 有限状态机(FSM) 代码示例
PN结、二极管原理详解与应用
C语言的浪漫
[UDS unified diagnostic service] IV. typical diagnostic service (5) - function / component test function unit (routine function unit 0x31)
[UDS] unified diagnostic service (UDS)
Qt 给应用程序加图标
逻辑回归原理及代码实现
cuda工程更换环境(电脑)后遇到的一系列编译问题
随机推荐
如何读文献
拷贝构造函数
相机标定:关键点法 vs 直接法
进程管理命令
Shell脚本 &&和||的使用
TensorFlow张量介绍
QT icon application
对象的动态建立和释放,赋值和复制
C语言进阶要点笔记5
C#【文件操作篇】PDF文件和图片互相转换
【UDS统一诊断服务】三、应用层协议(2)
【UDS统一诊断服务】(补充)五、ECU bootloader开发要点详解 (1)
PHP junior programmers, take orders and earn extra money
信息学一本通-小球
【UDS统一诊断服务】四、诊断典型服务(4)— 在线编程功能单元(0x34-0x38)
四元数乘法
金额输入框,用于充值提现
逻辑回归原理及代码实现
Camera calibration: key point method vs direct method
FOC SVPWM函数PWMC_SetPhaseVoltage解析