POJ 1679 The Unique MST 次小生成树
来源:互联网 发布:化工项目网络计划绘制 编辑:程序博客网 时间:2024/06/06 17:31
The Unique MST
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 32291 Accepted: 11712
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
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!
Source
POJ Monthly–2004.06.27 srbga@POJ
题意:给你一张图,问这张图的最小生成树是不是唯一的。
题解:求出次小生成树,如果次小生成树与最小生成树的权值是一样的,就不唯一。
如何求最小生成树?
有个很暴力但很直接的方法。其实我目前也只知道这个 因为次小生成树一定是最小生成树换一条边得到的,所以我们可以枚举每一条不在最小生成树的边,然后在树上查找这条边的两个节点之间的最大值,然后把这条边换上去,就又成了一棵生成树。在所有替换完后的生成树中找一个最小的就是次小生成树了。
这道题就直接找每条边的两个节点与树上两个点之间的最大值是否一样就行了。
写链剖是因为刚打了一道题直接把代码粘过来了
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;inline int read(){ int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9') {if(ch=='-') f=-1;ch=getchar();} while(ch>='0'&&ch<='9') {x=x*10+ch-'0';ch=getchar();} return x*f;}const int N = 15000 + 10;const int M = 30000 + 10;int n,m,q;struct node{ int pre,v,u,w; bool vis;}edge[M<<1],e[M<<1];int num=0,head[N];void adde(int from,int to,int w){ e[++num].pre=head[from],head[from]=num; e[num].v=to,e[num].w=w;}void addedge(int from,int to,int w,int i){ edge[i].u=from,edge[i].v=to,edge[i].w=w; edge[i].vis=false;}bool operator < (node a,node b){ return a.w<b.w;}int fa[N];int find(int x){ if(x==fa[x]) return fa[x]; return fa[x]=find(fa[x]);}int siz[N],son[N],dep[N],a[N];void dfs1(int u,int f){ siz[u]=1,fa[u]=f; for(int i=head[u];i;i=e[i].pre){ int v=e[i].v; if(v==f) continue; dep[v]=dep[u]+1; a[v]=e[i].w; dfs1(v,u); siz[u]+=siz[v]; if(siz[v]>siz[son[u]]) son[u]=v; }}int in[N],indx=0,top[N],seq[N];void dfs2(int u,int tp){ top[u]=tp;in[u]=++indx;seq[indx]=u; if(!son[u]) return ; dfs2(son[u],tp); for(int i=head[u];i;i=e[i].pre){ int v=e[i].v; if(v==fa[u]||v==son[u]) continue; dfs2(v,v); }}struct TREE{ int mmax;}t[N<<2];void update(int root){ t[root].mmax=max(t[root<<1].mmax,t[root<<1|1].mmax);}void build(int root,int l,int r){ if(l==r){ t[root].mmax=a[seq[l]]; return ; } int mid=l+r>>1; build(root<<1,l,mid); build(root<<1|1,mid+1,r); update(root);}int query(int root,int l,int r,int pos,int val){ if(pos<=l&&val>=r) return t[root].mmax; int mid=l+r>>1; int ans=-1; if(pos<=mid) ans=max(ans,query(root<<1,l,mid,pos,val)); if(val>mid) ans=max(ans,query(root<<1|1,mid+1,r,pos,val)); return ans;}int query(int u,int v){ int f1=top[u],f2=top[v]; int ans=-1; while(f1!=f2){ if(dep[f1]<dep[f2]) swap(f1,f2),swap(u,v); ans=max(ans,query(1,1,n,in[f1],in[u])); u=fa[f1],f1=top[u]; } if(dep[u]>dep[v]) swap(u,v); ans=max(ans,query(1,1,n,in[son[u]],in[v])); return ans;}#define ms(x,y) memset(x,y,sizeof(x))void update(){ ms(son,0);ms(head,0); num=0,indx=0;}int main(){ int t=read(); while(t--){ update(); n=read(),m=read(); for(int i=1;i<=n;++i) fa[i]=i; for(int i=1;i<=m;++i){ int u=read(),v=read(),w=read(); addedge(u,v,w,i); } sort(edge+1,edge+m+1);int cnt=0,ans=0; for(int i=1;i<=m;++i){ int u=edge[i].u,v=edge[i].v; int x=find(u),y=find(v); if(x==y) continue; ++cnt;fa[x]=y;edge[i].vis=true; ans+=edge[i].w; adde(u,v,edge[i].w);adde(v,u,edge[i].w); if(cnt==n-1) break; } dep[1]=1;dfs1(1,0);dfs2(1,1); build(1,1,n); bool flag=false; for(int i=1;i<=m;++i){ if(edge[i].vis) continue; int u=edge[i].u,v=edge[i].v; if(query(u,v)==edge[i].w) {flag=true;break;} } if(flag) printf("Not Unique!\n"); else printf("%d\n",ans); } return 0;}
- poj 1679 - The Unique MST ( 次小生成树 )
- POJ 1679 The Unique MST 次小生成树Prim
- POJ 1679 The Unique MST 次小生成树
- POJ 1679 The Unique MST 次小生成树
- poj 1679 The Unique MST(次小生成树)
- poj 1679 The Unique MST (次小生成树)
- POJ 1679 The Unique MST 次小生成树
- 【次小生成树】POJ-1679 The Unique MST
- poj 1679 The Unique MST(次小生成树)
- POJ 1679 The Unique MST(次小生成树&Kruskal)
- poj 1679 The Unique MST 次小生成树算法
- POJ 1679 The Unique MST (次小生成树)
- poj 1679 The Unique MST 次小生成树
- The Unique MST - POJ 1679 次小生成树
- POJ 1679 The Unique MST 次小生成树
- POJ 1679 The Unique MST 次小生成树
- POJ 1679 The Unique MST 次小生成树 裸题
- POJ 1679 The Unique MST(次小生成树)
- python实现youdao终端脚本
- Linux下tomcat实时日志查看
- Spring基础-4-AOP注解
- CSAPP阅读笔记
- HDU
- POJ 1679 The Unique MST 次小生成树
- 一次请求的源码分析过程
- bootstrap table使用总结
- cordova构建app偶遇mergeDebugResources失败的问题
- HDU-5938:Four Operations(DP)
- 【c++基础】笔记(二)
- 面试记录第十九节——(MVC)
- Java水仙花数
- 使用Git管理代码