总结:生成树的知识点真多,不过博主觉得图论的题目终究是建图难,第一步就是如何建图,将其转换成已知的问题。
另外,关于生成树的两个注意点,也是为了防止碰到毒瘤题。一就是自环,二就是重边。
A - The Unique MST
次小生成树裸题。
prim模板
#include<iostream>
#include<cstring>
#include<algorithm>
#include<stdio.h>
using namespace std;
const int maxn = 105;
const int inf = 0x3f3f3f3f;
int map[maxn][maxn];
int lowcost[maxn];
int closest[maxn];
bool used[maxn][maxn];
bool vis[maxn];
int Max[maxn][maxn];
int n, m;
int prim()
{
memset(lowcost, inf, sizeof(lowcost));
memset(closest, 0, sizeof(closest));
memset(used, 0, sizeof(used));
memset(vis, 0, sizeof(vis));
memset(Max, 0, sizeof(Max));
lowcost[1] = 0;
vis[1] = 1;
closest[1] = 1;
int num = 0, ans = 0;
int nownode = 1;
while (num<n - 1)
{
int Mincost = inf, theidx;
for (int i = 1;i <= n;i++)
{
if (vis[i])
{
if (i == nownode)continue;
Max[i][nownode] = Max[nownode][i] = max(Max[i][closest[nownode]], lowcost[nownode]);
}
else
{
if (lowcost[i] > map[i][nownode])
{
lowcost[i] = map[i][nownode];
closest[i] = nownode;
}
if (Mincost > lowcost[i])
{
Mincost = lowcost[i];
theidx = i;
}
}
}
ans += Mincost;
num++;
nownode = theidx;
vis[nownode] = 1;
used[theidx][closest[theidx]] = used[closest[theidx]][theidx] = 1;
}
for (int i = 1;i <= n;i++)
Max[i][nownode] = Max[nownode][i] = max(Max[i][closest[nownode]], lowcost[nownode]);
return ans;
}
void solve(int num)
{
for (int i = 1;i <= n;i++)
for (int j = i + 1;j <= n;j++)if (!used[i][j])
{
int ans = num - Max[i][j] + map[i][j];
if (ans == num)
{
puts("Not Unique!");
return;
}
}
printf("%d\n", num);
}
int main()
{
int T;
scanf("%d", &T);
while (T--)
{
scanf("%d %d", &n, &m);
int u, v, dis;
memset(map, inf, sizeof(map));
for (int i = 1;i <= m;i++)
{
scanf("%d %d %d", &u, &v, &dis);
map[u][v] = map[v][u] = dis;
}
int ans = prim();
solve(ans);
}
}
D - Is There A Second Way Left?
这题相对于上题来说,有重边。所以用kruskal(个人感觉也能用prim)。。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#include<vector>
#include<stack>
#include<set>
#include<cmath>
#include<climits>
using namespace std;
#define ll long long
const int maxn = 105;
const int maxm = 205;
const int inf = 0x3f3f3f3f;
struct Edge
{
int u, v, dis;
bool vis;
Edge(int _u, int _v, int _d) :u(_u), v(_v), dis(_d) { vis = 0; }
Edge(){}
bool operator<(const Edge &b)const
{
return dis < b.dis;
}
}edges[maxm];
int tot;
int n, m;
vector<int>node[maxn];
void addedge(int u, int v, int dis)
{
edges[tot++] = Edge(u, v, dis);
}
int f[maxn];
int find(int a)
{
return a == f[a] ? a : f[a] = find(f[a]);
}
int Max[maxn][maxn];
int Kruskal()
{
sort(edges + 1, edges + tot);
for (int i = 1;i <= n;i++)
f[i] = i, node[i].clear(), node[i].push_back(i);
memset(Max, 0, sizeof(Max));
int ans = 0, tot1 = 0;
for (int i = 1;i < tot;i++)
{
if (tot1 == n - 1)break;
int u = edges[i].u, v = edges[i].v, dis = edges[i].dis;
int fu = find(u), fv = find(v);
if (fu != fv)
{
edges[i].vis = 1;
tot1++;
ans += dis;
f[fv] = fu;
//cout << find(fu) << endl;
int Sizefu = node[fu].size();
int Sizefv = node[fv].size();
for (int j = 0;j < Sizefu;j++)
for (int k = 0;k < Sizefv;k++)
Max[node[fu][j]][node[fv][k]] = Max[node[fv][k]][node[fu][j]] = dis;
int tmp[105];
for (int j = 0;j < Sizefu;j++)
tmp[j] = node[fu][j];
for (int j = 0;j < Sizefv;j++)
node[fu].push_back(node[fv][j]);
for (int j = 0;j < Sizefu;j++)
node[fv].push_back(tmp[j]);
}
}
if (tot1 != n - 1)return -1;
return ans;
}
int solve(int num)
{
int ans = inf;
for (int i = 1</