最小生成树
图
一个二元组 G=(V(G)(点集),E(G)(边集))。V(G)(点集)中各元素被称为顶点(节点)。
生成树
连通无向图所有点的生成子图。
最小生成树
边权和最小的生成树。
如何判断最小生成树是否唯一
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'.
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 | Output |
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 | 3 Not Unique! |
题解:开一个数组记录上次某个点合并时的边权。
// C++ includes used for precompiling -*- C++ -*-
// Copyright (C) 2003-2018 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library. This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// Under Section 7 of GPL version 3, you are granted additional
// permissions described in the GCC Runtime Library Exception, version
// 3.1, as published by the Free Software Foundation.
// You should have received a copy of the GNU General Public License and
// a copy of the GCC Runtime Library Exception along with this program;
// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
// <http://www.gnu.org/licenses/>.
/** @file stdc++.h
* This is an implementation file for a precompiled header.
*/
// 17.4.1.2 Headers
// C
#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdalign>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cuchar>
#include <cwchar>
#include <cwctype>
#endif
// C++
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <codecvt>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif
#if __cplusplus >= 201402L
#include <shared_mutex>
#endif
#if __cplusplus >= 201703L
#include <charconv>
#include <filesystem>
#endif
using namespace std;
int fa[20230];
int n,m;
int sum;
bool flag;
void init()
{
flag=false;
sum=0;
cin>>n>>m;
for(int i=1;i<=n;i++)fa[i]=i;
}
struct edge
{
int x,y,w;
}trip[20230];
bool cmp(edge i1,edge i2)
{
if(i1.w!=i2.w)return i1.w<i2.w;
else return i1.x<i2.x;
}
int find(int x)
{
return x==fa[x]?x:fa[x]=find(fa[x]);
}
void solve()
{
init();
int last[20230]={0};
for(int i=1;i<=m;i++)cin>>trip[i].x>>trip[i].y>>trip[i].w;
sort(trip+1,trip+1+m,cmp);
int fx,fy;
for(int i=1;i<=m;i++)
{
fx=find(trip[i].x),fy=find(trip[i].y);
if(fx!=fy)
{
fa[fy]=fx;
sum+=trip[i].w;
last[trip[i].x]=last[trip[i].y]=trip[i].w;
}
else
{
if(last[trip[i].x]==trip[i].w||last[trip[i].y]==trip[i].w)
{
flag=true;
break;
}
}
}
if(flag)cout<<"Not Unique!";
else cout<<sum;
}
int main()
{
int t;
cin>>t;
while(t--)
{
solve();
cout<<'\n';
}
}
Kruskal实现方法
将所有边按权值大小排序,合并(指先判断在不在同一个集合再合并)直至边数==点数-1。
并查集
基础模板
void init() //一定不要忘记写上初始化函数!!!
{
for(int i=1;i<=n;i++)fa[i]=i/*,pointsize[i]=1,edgesize[i]=0*/;
}
int find(int x)
{
return x==fa[x]?x:find(fa[x]); //注意路径压缩会破坏树的结构:它要求直接连到根上。
}
void merge(int x,int y)
{
int fx=find(x),fy=find(y);
if(fx!=fy)
{
//edgesize[fx]++; (点数)
fa[fy]=fx;
//pointsize[fx]+=pointsize[fy]; (边数)
}
}
寻找连通块的方法
寻找祖先->有多少个元素x满足 fa[x]==x ,就有多少个连通块。(O(n)复杂度)