The Unique MST
2015-08-20 13:09
645 查看
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:
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
Sample Output
题解:次小生成树,未优化的普利姆做法。每次把当前的最小生成树的任意两点之间的最大值保存,然后遍历没有用到的边,遍历i与j之间,如果i,j的权等于最小生成树上i->j的最大值,肯定存在第二个生成树,因为我可以删除原来那一条,用现在这一条顶替。
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int INF = 0x3fffffff;
int map[203][203];
bool visited[203];
int d[203];
int w[203][203]; //生成树上两点之间的最大值
bool s[203][203]; //记录生成树的边
int pre[203];
int ans;
int max(int a,int b)
{
return a > b ? a : b;
}
int min(int a,int b)
{
return a > b ? b : a;
}
bool prim(int n)
{
memset(visited,false,sizeof(visited));
memset(w,0,sizeof(w));
memset(s,false,sizeof(s));
for(int i = 1;i <= n;i++)
{
d[i] = map[1][i];
w[1][i] = w[i][1] = map[1][i];
pre[i] = 1;
}
visited[1] = true;
ans = 0;
for(int i = 1;i < n;i++)
{
int min = INF;
int k;
for(int j = 1;j <= n;j++)
{
if(!visited[j] && min > d[j])
{
min = d[j];
k = j;
}
}
s[pre[k]][k] = s[k][pre[k]] = true;
if(min == -1)
{
return false;
}
ans += min;
visited[k] = true;
for(int j = 1;j <= n;j++)
{
if(visited[j]) //更新j到其他点的最大值,其他访问了的点和该点的生成树路径上的边已经得到
{
w[j][k] = w[k][j] = max(w[j][pre[k]],map[pre[k]][k]);
}
if(!visited[j] && d[j] > map[k][j])
{
d[j] = map[k][j];
pre[j] = k;
}
}
}
return true;
}
int main()
{
int n,m;
int ncase;
cin>>ncase;
while(ncase--)
{
scanf("%d%d",&n,&m);
for(int i = 1;i <= n;i++)
{
for(int j = 1;j <= n;j++)
{
if(i == j)
{
map[i][j] = 0;
}
else
{
map[i][j] = INF;
}
}
}
int u,v,c;
for(int i = 0;i < m;i++)
{
scanf("%d%d%d",&u,&v,&c);
map[u][v] = map[v][u] = min(map[u][v],c);
}
p
4000
rim(n);
bool flag = true;
for(int i = 1;i <= n;i++)
{
for(int j = 1;j <= n;j++)
{
if(map[i][j] != INF && !s[i][j] && i != j)
{
if(w[i][j] == map[i][j]) //i-j的边(没有在生成树上)等于生成树上i->j路径上的最大值
{
flag = false;
break;
}
}
}
}
if(flag)
{
printf("%d\n",ans);
}
else
{
printf("Not Unique!\n");
}
}
return 0;
}
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
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!
题解:次小生成树,未优化的普利姆做法。每次把当前的最小生成树的任意两点之间的最大值保存,然后遍历没有用到的边,遍历i与j之间,如果i,j的权等于最小生成树上i->j的最大值,肯定存在第二个生成树,因为我可以删除原来那一条,用现在这一条顶替。
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int INF = 0x3fffffff;
int map[203][203];
bool visited[203];
int d[203];
int w[203][203]; //生成树上两点之间的最大值
bool s[203][203]; //记录生成树的边
int pre[203];
int ans;
int max(int a,int b)
{
return a > b ? a : b;
}
int min(int a,int b)
{
return a > b ? b : a;
}
bool prim(int n)
{
memset(visited,false,sizeof(visited));
memset(w,0,sizeof(w));
memset(s,false,sizeof(s));
for(int i = 1;i <= n;i++)
{
d[i] = map[1][i];
w[1][i] = w[i][1] = map[1][i];
pre[i] = 1;
}
visited[1] = true;
ans = 0;
for(int i = 1;i < n;i++)
{
int min = INF;
int k;
for(int j = 1;j <= n;j++)
{
if(!visited[j] && min > d[j])
{
min = d[j];
k = j;
}
}
s[pre[k]][k] = s[k][pre[k]] = true;
if(min == -1)
{
return false;
}
ans += min;
visited[k] = true;
for(int j = 1;j <= n;j++)
{
if(visited[j]) //更新j到其他点的最大值,其他访问了的点和该点的生成树路径上的边已经得到
{
w[j][k] = w[k][j] = max(w[j][pre[k]],map[pre[k]][k]);
}
if(!visited[j] && d[j] > map[k][j])
{
d[j] = map[k][j];
pre[j] = k;
}
}
}
return true;
}
int main()
{
int n,m;
int ncase;
cin>>ncase;
while(ncase--)
{
scanf("%d%d",&n,&m);
for(int i = 1;i <= n;i++)
{
for(int j = 1;j <= n;j++)
{
if(i == j)
{
map[i][j] = 0;
}
else
{
map[i][j] = INF;
}
}
}
int u,v,c;
for(int i = 0;i < m;i++)
{
scanf("%d%d%d",&u,&v,&c);
map[u][v] = map[v][u] = min(map[u][v],c);
}
p
4000
rim(n);
bool flag = true;
for(int i = 1;i <= n;i++)
{
for(int j = 1;j <= n;j++)
{
if(map[i][j] != INF && !s[i][j] && i != j)
{
if(w[i][j] == map[i][j]) //i-j的边(没有在生成树上)等于生成树上i->j路径上的最大值
{
flag = false;
break;
}
}
}
}
if(flag)
{
printf("%d\n",ans);
}
else
{
printf("Not Unique!\n");
}
}
return 0;
}
相关文章推荐
- requireJS简单的学习门户网站
- 开源作品-PHP写的在线文件管理工具(单文件绿色版)-SuExplorer_PHP_2_0
- QSqlQueryModel读取数据库,提示 database locked
- 搞定百度编辑器UEditor
- 在UINavigation上添加UISearchBar
- 虚拟化--004 your account is no authorized to perform the requested action
- UITableViewCell的重用问题
- IOS开发 - UIApplication的常见使用
- UE4 GPU Profiling
- UE4 CPU Profiling
- UE4 Profiler
- UE4 Performance Guidelines
- UE4 Performance and Profiling
- UE4 Stat Commands
- 获得多行UILabel的每一行内容
- UE4 System Settings
- 80 css ie8下ui-box-head出现阴影效果
- UIAlertView的自动取消的使用
- Unreal4 Build System
- UE4 配置文件