标签:
id=1679
题意:告诉你有n个点,m条边,以及m条边的信息(起点、终点、权值)。推断最小生成树是否唯一
之前是用另外一种方法做的。复杂度最高可达O(n^3),后来用次小生成树又做了一次。复杂度O(n^2+m)。
先说次小生成树的方法。
次小生成树:求出最小生成树,把用到的边做标记,此时加入额外的边进去必定形成环,删除环中第二大的边(即这个环里在生成树上的最大边),加上额外边的权值,枚举每一个额外边,取最小值,就是次小生成树的值。用同样的方法能够求第K小生成树以及推断第K小生成树是否唯一。
#include<cstring> #include<string> #include<fstream> #include<iostream> #include<iomanip> #include<cstdio> #include<cctype> #include<algorithm> #include<queue> #include<map> #include<set> #include<vector> #include<stack> #include<ctime> #include<cstdlib> #include<functional> #include<cmath> using namespace std; #define PI acos(-1.0) #define MAXN 50100 #define eps 1e-7 #define INF 0x7FFFFFFF #define LLINF 0x7FFFFFFFFFFFFFFF #define seed 131 #define MOD 1000000007 #define ll long long #define ull unsigned ll #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 struct node{ int u,v,w; int used; }edge1[10010]; struct Edge{ int u,v,w,next; }edge[20010]; struct NODE{ int u,maxm; }; int head[110],vis[110],father[110]; int maxm[110][110]; int n,m,cnt; void add_edge(int a,int b,int c){ edge[cnt].u = a; edge[cnt].v = b; edge[cnt].w = c; edge[cnt].next = head[a]; head[a] = cnt++; } int Find(int x){ int t = x; while(t != father[t]) t = father[t]; int k = x; while(k != t){ int temp = father[k]; father[k] = t; k = temp; } return t; } int kruskal(){ int i,j; int num = 0, sum = 0; for(i = 0; i <= n; i++) father[i] = i; for(i = 0; i < m; i++){ int xx = Find(edge1[i].u); int yy = Find(edge1[i].v); if(xx != yy){ add_edge(edge1[i].u, edge1[i].v, edge1[i].w); add_edge(edge1[i].v, edge1[i].u, edge1[i].w); father[xx] = yy; sum += edge1[i].w; edge1[i].used = 1; num++; if(num >= n - 1) break; } } return sum; } void bfs(int src){ int i,j; memset(vis, 0, sizeof(vis)); queue<NODE>q; NODE t1,t2; t1.u = src; t1.maxm = 0; q.push(t1); vis[src] = 1; while(!q.empty()){ t1 = q.front(); q.pop(); for(i = head[t1.u]; i != -1; i = edge[i].next){ t2.u = edge[i].v; t2.maxm = edge[i].w; if(!vis[t2.u]){ vis[t2.u] = 1; if(t1.maxm > t2.maxm) t2.maxm = t1.maxm; maxm[src][t2.u] = t2.maxm; q.push(t2); } } } } int main(){ int t,i,j; int a,b,c; scanf("%d", &t); while(t--){ scanf("%d%d", &n, &m); for(i = 0; i < m; i++){ scanf("%d%d%d", &edge1[i].u, &edge1[i].v, &edge1[i].w); edge1[i].used = 0; } memset(head,-1,sizeof(head)); cnt = 0; int ans1 = kruskal(); for(i = 1; i <= n; i++) bfs(i); int ans2 = INF; for(i = 0; i < m; i++){ if(!edge1[i].used){ int temp = ans1 - maxm[edge1[i].u][edge1[i].v] + edge1[i].w; if(temp < ans2) ans2 = temp; } } if(ans1!=ans2) printf("%d\n", ans1); else puts("Not Unique!"); } return 0; }
删边枚举法:对于每条边假设有和他相等权值的边。则做一个标记,然后进行一遍kruskal或prim找出最小生成树权值。然后对于每一个使用过而且有相等边标记的边,把它从图中删去,再进行一遍kruskal或prim,假设此时最小生成树权值和第一次一样,则说明最小生成树不唯一。否则最小生成树唯一。
#include<cstring> #include<string> #include<fstream> #include<iostream> #include<iomanip> #include<cstdio> #include<cctype> #include<algorithm> #include<queue> #include<map> #include<set> #include<vector> #include<stack> #include<ctime> #include<cstdlib> #include<functional> #include<cmath> using namespace std; #define PI acos(-1.0) #define MAXN 110000 #define eps 1e-7 #define INF 0x7FFFFFFF #define seed 131 #define ll long long #define ull unsigned ll #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 struct node{ int u,v,dis; int used,equa,del; }edge[MAXN]; int father[105],vis[105]; int n,m,flag,ans; bool cmp(node x,node y){ return x.dis<y.dis; } int find(int x){ int t = x; while(father[t]!=t) t = father[t]; int k = x; while(k!=t){ int temp = father[k]; father[k] = t; k = temp; } return t; } int kruskal(){ int i,j=0; int sum = 0; for(i=1;i<=n;i++) father[i] = i; for(i=0;i<m;i++){ if(edge[i].del) continue; int a = find(edge[i].u); int b = find(edge[i].v); if(a!=b){ father[a] = b; sum += edge[i].dis; j++; if(flag) edge[i].used = 1; if(j>=n-1) break; } } return sum; } int main(){ int t,i,j; scanf("%d",&t); while(t--){ flag = 1; scanf("%d%d",&n,&m); for(i=0;i<m;i++){ scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].dis); edge[i].used = edge[i].equa = edge[i].del = 0; } for(i=0;i<m;i++){ for(j=0;j<m;j++){ if(i==j) continue; if(edge[i].dis == edge[j].dis) edge[i].equa = 1; } } sort(edge,edge+m,cmp); int ans1 = kruskal(); flag = 0; for(i=0;i<m;i++){ if(edge[i].used&&edge[i].equa){ edge[i].del = 1; int ans2 = kruskal(); if(ans2==ans1){ printf("Not Unique!\n"); break; } } } if(i>=m) printf("%d\n",ans1); } return 0; }
POJ--1679--The Unique MST【推断MST是否唯一】
标签:
原文地址:http://www.cnblogs.com/mengfanrong/p/5070765.html