POJ 1679 The unique MST [次小生成树]
2016-08-09 13:29
295 查看
The Unique MST
Time Limit: 1000MS Memory Limit: 10000KB 64bit IO Format: %lld & %llu
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
一般的次小生成树。。
这道题数据范围不需要用倍增,那么就暴力替换就可以了。
Time Limit: 1000MS Memory Limit: 10000KB 64bit IO Format: %lld & %llu
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<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<cmath> #include<vector> #include<queue> #include<stack> #include<map> #include<set> #include<string> #include<iomanip> #include<ctime> #include<climits> #include<cctype> #include<algorithm> #ifdef WIN32 #define AUTO "%I64d" #else #define AUTO "%lld" #endif using namespace std; const int INF=0x3f3f3f3f; const int maxn=105; struct Edge { int from,to; int val; bool operator < (const Edge &t)const { return val<t.val; } }edge[maxn*maxn]; struct MST { int to,next; int val; }mst[maxn<<1]; int head[maxn]; int maxedge; int n,m,fa[maxn]; bool inMST[maxn*maxn],vis[maxn]; int maxcost[maxn][maxn]; inline void addmst(int u,int v,int c) { mst[++maxedge]=(MST){v,head[u],c}; head[u]=maxedge; mst[++maxedge]=(MST){u,head[v],c}; head[v]=maxedge; } inline void init() { memset(head,-1,sizeof(head)); memset(inMST,0,sizeof(inMST)); memset(maxcost,0,sizeof(maxcost)); maxedge=-1; scanf("%d%d",&n,&m); for(int i=1;i<=m;i++) scanf("%d%d%d",&edge[i].from,&edge[i].to,&edge[i].val); for(int i=1;i<=n;i++) fa[i]=i; } inline int find(int x) { if(fa[x]!=x) fa[x]=find(fa[x]); return fa[x]; } inline bool union_find(int x,int y) { int t1=find(x),t2=find(y); if(t1==t2) return false; fa[t2]=t1; return true; } inline int kruskal() { int ret=0; sort(edge+1,edge+m+1); int tot=0,pos=1; while(tot!=n-1) { if(union_find(edge[pos].from,edge[pos].to)) { tot++,ret+=edge[pos].val; addmst(edge[pos].from,edge[pos].to,edge[pos].val); inMST[pos]=true; } if(++pos>m) break;//do not return herte in case the last edge to be NO.m } if(tot^(n-1)) return -1; else return ret; } void dfs(int s,int t,int val) { vis[t]=true; maxcost[s][t]=val; for(int i=head[t];~i;i=mst[i].next) { int v=mst[i].to; if(vis[v]) continue; dfs(s,v,max(val,mst[i].val)); } } inline int get_maxcost(int x,int y) { if(!maxcost[x][y]) return maxcost[y][x]; 4000 return maxcost[x][y]; } int main() { #ifdef Local freopen("unique.in","r",stdin); freopen("unique.out","w",stdout); #endif int cas; scanf("%d",&cas); while(cas--) { init(); int minMST=kruskal(),subMST=INF; for(int i=1;i<=n;i++) { memset(vis,0,sizeof(vis)); dfs(i,i,0); } for(int i=1;i<=m;i++) if(!inMST[i]) subMST=min(subMST,minMST-get_maxcost(edge[i].from,edge[i].to)+edge[i].val); if(minMST^subMST) printf("%d\n",minMST); else printf("Not Unique!\n"); } return 0; }
相关文章推荐
- poj 1679 The Unique MST(判断最小生成树是否唯一)
- poj1679 The Unique MST ——判断最小生成树是否唯一_kruscal算法
- 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 【次小生成树】【模板】
- The Unique MST - POJ 1679 次小生成树
- poj-1679-The Unique MST-最小生成树是否唯一
- poj 1679 The Unique MST(判断最小生成树是否唯一)
- POJ1679 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,次小生成树