当前位置:网站首页>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
边栏推荐
- C语言实用小技巧合集(持续更新)
- [UDS unified diagnostic service] IV. typical diagnostic service (5) - function / component test function unit (routine function unit 0x31)
- Graduation project, curriculum link, student achievement evaluation system
- 搭建openstack平台
- Tabbar implementation of dynamic bottom navigation bar in uniapp, authority management
- Camera calibration: key point method vs direct method
- Round up a little detail of the round
- C language advanced notes 3
- [UDS unified diagnostic service] II. Network layer protocol (1) - overview and functions of network layer
- Collection of practical tips for C language (continuously updated)
猜你喜欢

cuda工程更换环境(电脑)后遇到的一系列编译问题

【UDS统一诊断服务】四、诊断典型服务(6)— 输入输出控制单元(0x2F)

Graduation project, curriculum link, student achievement evaluation system
![[UDS unified diagnosis service] IV. typical diagnosis service (1) - diagnosis and communication management function unit](/img/4f/7ca6505b545fb825b0dba36f474da7.png)
[UDS unified diagnosis service] IV. typical diagnosis service (1) - diagnosis and communication management function unit

PM2 deploy nuxt project

基于VGG卷积神经网络的图像识别代码实现

PHP junior programmers, take orders and earn extra money
![[UDS unified diagnostic service] i. overview of diagnosis (4) - basic concepts and terms](/img/fb/3d3cf54dc5b67ce42d60e0fe63baa6.png)
[UDS unified diagnostic service] i. overview of diagnosis (4) - basic concepts and terms

类的继承与派生

基于Keras的时装分类案例
随机推荐
Matching between class template with default template argument and template parameter
Object array and object pointer
Matlab calibration board corner detection principle
Collection of practical tips for C language (continuously updated)
深蓝学院激光slam 理论与实践 第三章激光雷达去畸变 作业习题
[UDS unified diagnosis service] i. diagnosis overview (2) - main diagnosis protocols (K-line and can)
Round up a little detail of the round
共用数据的保护
【UDS统一诊断服务】三、应用层协议(2)
友元函数,友元类,类模板
Introduction to nonparametric camera distortion model
[UDS unified diagnostic service] IV. typical diagnostic service (5) - function / component test function unit (routine function unit 0x31)
C语言结构体指定初始化
SSH 公钥 私钥的理解
ArcGIS license错误-15解决方法
四元数乘法
sqlite编译
[UDS unified diagnostic service] IV. typical diagnostic service (2) - data transmission function unit
TP download folder, compress folder and download
拷贝构造函数