当前位置:网站首页>POJ-The Unique MST
POJ-The Unique MST
2022-04-23 05:51:00 【Round moon】
The Unique MST
题目描述
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’.
输入
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!’.
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!
题目大意
询问这个图的最小生成树是否唯一。
判断是否唯一只需要比较其最小生成树和非严格次小生成树就可以了。
本题就是针对与非严格次小生成树所做出的。
我们在链接的时候,记录一下,两个集合每个元素到彼此每个元素的权值。因为要拿一个边去替换的话,我们必须知道替换下来的边的权值是多少。
好核心部分说明了,那么继续
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://blog.csdn.net/qq_35339563/article/details/121173685
边栏推荐
- 类的继承与派生
- realsense 选型大对比D455 D435i D415 T265 3D硬件对比
- Call procedure of function
- C语言数组处理批量数据
- 【学习一下】HF-Net 训练
- PM2 deploy nuxt project
- Protection of shared data
- 基于TensorFlow的线性回归实例
- 【UDS统一诊断服务】四、诊断典型服务(5)— 功能/元件测试功能单元(例行程序功能单元0x31)
- [UDS unified diagnosis service] IV. typical diagnosis service (3) - read fault information function unit (storage data transmission function unit)
猜你喜欢
【UDS统一诊断服务】三、应用层协议(2)
【UDS统一诊断服务】一、诊断概述(3)— ISO 15765体系结构
cuda工程更换环境(电脑)后遇到的一系列编译问题
[UDS unified diagnostic service] III. application layer protocol (1)
[UDS unified diagnosis service] IV. typical diagnosis service (3) - read fault information function unit (storage data transmission function unit)
[UDS unified diagnosis service] i. diagnosis overview (2) - main diagnosis protocols (K-line and can)
[UDS unified diagnosis service] i. diagnosis overview (3) - ISO 15765 architecture
[untitled]
【UDS统一诊断服务】一、诊断概述(2)— 主要诊断协议(K线和CAN)
卷积神经网络实现CIFAR100数据集分类
随机推荐
【UDS统一诊断服务】四、诊断典型服务(1)— 诊断和通信管理功能单元
grub boot. S code analysis
vs中的多字节与unicode
逻辑回归原理及代码实现
Protection of shared data
ARM常用汇编指令
Quaternion multiplication
[untitled]
C [document operation] PDF files and pictures are converted to each other
对象的动态建立和释放,赋值和复制
Eigen 库常用基本用法 备忘
【无标题】
共用数据的保护
Latex configuration and use
Object array and object pointer
Tabbar implementation of dynamic bottom navigation bar in uniapp, authority management
C语言结构体指定初始化
声明为全局变量
PM2 deploy nuxt related commands
ArcGIS license错误-15解决方法