[伪位运算生成树] HDU 5629 Clarke and tree
来源:互联网 发布:java获取本机端口号 编辑:程序博客网 时间:2024/05/21 07:55
求边权and和最大生成树
从高位到低位枚举 看这位为1的以及满足当前答案(答案上为1的位 则边权这一位也要为1)的边是否能构成一棵生成树
#include<cstdio>#include<cstdlib>#include<algorithm>using namespace std;inline char nc(){ static char buf[100000],*p1=buf,*p2=buf; if (p1==p2) { p2=(p1=buf)+fread(buf,1,100000,stdin); if (p1==p2) return EOF; } return *p1++;}inline void read(int &x){ char c=nc(),b=1; for (;!(c>='0' && c<='9');c=nc()) if (c=='-') b=-1; for (x=0;c>='0' && c<='9';x=x*10+c-'0',c=nc()); x*=b;}const int N=300005;int fat[N];inline void init(int n){ for (int i=1;i<=n;i++) fat[i]=i;}inline int Fat(int u){ return u==fat[u]?u:fat[u]=Fat(fat[u]);}inline bool Merge(int x,int y){ x=Fat(x); y=Fat(y); if (x==y) return 0; fat[x]=y; return 1;}int n,m,Ans;int u[N],v[N],w[N];int vst[N];int main(){ int Q; freopen("t.in","r",stdin); freopen("t.out","w",stdout); read(Q); while (Q--){ Ans=0; read(n); read(m); for (int i=1;i<=m;i++) read(u[i]),read(v[i]),read(w[i]),vst[i]=1; for (int k=31;~k;k--){ init(n); int tem=n; for (int i=1;i<=m;i++)if (vst[i] && (w[i]>>k&1) && Merge(u[i],v[i])) tem--; if (tem==1){Ans+=1<<k;for (int i=1;i<=m;i++) if (!(w[i]>>k&1)) vst[i]=0; } } printf("%d\n",Ans); } return 0;}
0 0
- [伪位运算生成树] HDU 5629 Clarke and tree
- HDOJ 5627 Clarke and MST (位运算最大生成树kruskul)
- hdoj Clarke and MST 5627(求位运算and后得到的最大生成树)(并查集&位运算)好题
- hdu 5627 Clarke and MST【最大生成树】
- bestcoder #72 DIV2 Clarke and MST 最大按位与的生成树
- HDU 5629 Clarke and tree(Purfer序列+dp+组合数学)
- Clarke and MST(最大生成树)
- HDU Clarke and points
- Clarke and MST HDU
- HDU 5627 Clarke and MST 求&的最大生成树 贪心
- hdoj 5627 Clarke and MST 【最大and生成树】
- HDU 5463 Clarke and minecraft
- HDU 5465 Clarke and puzzle
- HDU 5463 Clarke and minecraft
- HDU 5463 Clarke and minecraft
- HDU 5464:Clarke and problem
- HDU 5463Clarke and minecraft
- HDU 5465 Clarke and puzzle
- 利用RecyclerView实现的一个动画给变item的位置和左右滑动删除该RcyclervView的Item条目
- Power OJ 2543 赛场布置(网络流最小割)
- Notice: Undefined index: act
- 搭建NFS服务器
- 个人记录-LeetCode 55. Jump Game
- [伪位运算生成树] HDU 5629 Clarke and tree
- Android Webview 获取网页form 提交的数据
- Lua初学者笔记
- const修饰符的作用
- jq上传进度条
- NestedScrolling机制(三)——机制本质以及源码解析
- 灵活的指向const 的引用
- java向上转型和向下转型
- Java知识点