PKU 1679 The Unique MST(解题报告)
2015-06-10 12:39
309 查看
思路是先找出最小生成树,并记录每条遍,要想说明是唯一的最小生成树,那么他的每条边都是必不可少的,然后我们可以枚举所有最小生成树的边 ,依次去掉,看是否还能不能组成一棵相同权值和的mst。就是因为当初的一点点儿错误导致 ,一周了将近,才拿出来,一眼看出bug!
#include<iostream> #include<string.h> #include<queue> #include<algorithm> using namespace std; const int M=103; int F[M]; int r[M]; int n,m,t,cnt; int res,ans; struct edge { int u,v,w; } per[M*M*M]; bool cmp(edge a,edge b) { return a.w<b.w; } void init() { for(int i=1; i<=n; i++) { F[i]=i; r[i]=1; } } //int Find(int x) //{ // if(x!=F[x]) // F[x]=Find(F[x]); // return F[x]; //} int Find(int x) { int r=x; while(r!=F[r]) { r=F[r]; } int k=x; while(k!=r) { int t=F[k]; F[k]=r; k=t; } return r; } bool union_set(int u,int v) { int tx=Find(u); int ty=Find(v); if(tx==ty) return false; else if(r[tx]>r[ty]) F[ty]=tx; else if(r[tx]<r[ty]) F[tx]=ty; else { F[tx]=ty; r[ty]++; } return true; } int main() { cin>>t; while(t--) { cin>>n>>m; queue<int>dict; for(int i=0; i<m; i++) cin>>per[i].u>>per[i].v>>per[i].w; sort(per,per+m,cmp); init(); cnt=0; res=0; for(int i=0; i<m; i++) { if(union_set(per[i].u,per[i].v)) { cnt++; dict.push(i); res+=per[i].w; if(cnt==n-1) break; } } int flag=0; while(!dict.empty()) { cnt=0; ans=0; init(); int k=dict.front(); dict.pop(); for(int i=0; i<m; i++) { if(i==k) continue; if(union_set(per[i].u,per[i].v)) { cnt++; ans+=per[i].w; if(cnt==n-1) { if(ans==res) { flag=1; break; } } } } if(flag) { cout<<"Not Unique!"<<endl; break; } } if(!flag) cout<<res<<endl; } return 0; }
相关文章推荐
- 关于arduino的各种网站
- [转载]UIKit Dynamics入门(OC)
- iOS中UITextField用法
- setuid权限
- FreeGLUT Tips: Resolve compile issue: error LNK1104: cannot open file 'freeglutd.lib'
- quick 关于触摸的问题
- HDU-1047-Integer Inquiry(Java大数水题 && 格式恶心)
- clCreateCommandQueue': was declared deprecated
- Universal Fighting Game Guide: Understanding Combo Systems Part 1 – The Elements
- UI控件问题和XCode编译警告和错误解决方法集锦 (持续更新ing)
- 多线程-NSOperationQueue
- ugui和ngui随手记
- UIStepper
- IOS中Key-Value Coding (KVC)的使用详解
- Unique Paths I,II
- Bluetooth--- android 蓝牙通信编程
- 全向移动平台android版手机控制器——UI界面设计
- 用好hook_views_query_alter,高度自定义view的筛选功能
- cakephp 的query方法,直接写在controller中是否 有点不合适
- 一些平台无关的整型类型,int8_t,uint8_t....