HDU 5627 Clarke and MST
2016-02-18 12:24
316 查看
问题描述
输入描述
输出描述
输入样例
输出样例
克拉克是一名人格分裂患者。某一天克拉克变成了一名图论研究者。 他学习了最小生成树的几个算法,于是突发奇想,想做一个位运算and的最大生成树。 一棵生成树是由n-1n−1条边组成的,且nn个点两两可达。一棵生成树的大小等于所有在生成树上的边的权值经过位运算and后得到的数。 现在他想找出最大的生成树。
输入描述
第一行是一个整数T(1 \le T \le 5)T(1≤T≤5),表示数据组数。 每组数据第一行是两个整数n, m(1 \le n, m \le 300000)n,m(1≤n,m≤300000),分别表示点个数和边个数。其中n, m > 100000n,m>100000的数据最多一组。 接下来mm行,每行33个整数x, y, w(1 \le x, y \le n, 0 \le w \le 10^9)x,y,w(1≤x,y≤n,0≤w≤109),表示x, yx,y之间有一条大小为ww的边。
输出描述
每组数据输出一行一个数,表示答案。若不存在生成树,输出00。
输入样例
1 4 5 1 2 5 1 3 3 1 4 2 2 3 1 3 4 7
输出样例
1
根据2进制一位一位往下做,找到符合的生成树#include<cstdio> #include<cstring> #include<cmath> #include<queue> #include<vector> #include<iostream> #include<algorithm> #include<bitset> #include<functional> using namespace std; typedef unsigned long long ull; typedef long long LL; const int maxn = 3e5 + 10; const int mod = 1e9 + 7; int T, n, m, fa[maxn], ans; struct point { int x, y, w; void read(){ scanf("%d%d%d", &x, &y, &w); } bool operator <(const point&a)const { return w > a.w; } }a[maxn]; int get(int x) { return fa[x] == x ? x : fa[x] = get(fa[x]); } int main() { scanf("%d", &T); while (T--) { cin >> n >> m; for (int i = 0; i < m; i++) a[i].read(); sort(a, a + m); ans = 0; for (int i = 29; i >= 0; i--) { int cnt = n - 1; for (int j = 1; j <= n; j++) fa[j] = j; for (int j = 0; j < m&&cnt; j++) { if (a[j].w < (1 << i)) break; if (a[j].w & (1 << i)) { int fx = get(a[j].x), fy = get(a[j].y); if (fx == fy) continue; fa[fx] = fy; cnt--; } } if (!cnt) { ans |= (1 << i); for (int j = 0; j < m; j++) { if (a[j].w < (1 << i)) break; if (a[j].w & (1 << i)) a[cnt++] = a[j]; } m = cnt; } } printf("%d\n", ans); } return 0; }
相关文章推荐
- Pod在墙内
- 关于C#中的抽象类、抽象方法和虚方法的探究
- 现在的问题
- [置顶] 让Linux系统启动过程自动运行自己编写的脚本
- centos 下 69 column window is too narrow
- Mysql表使用全文索引
- work_note 2016年2月18日, PM 12:21:29
- SVN常用命令
- poj1014 dfs/背包/类筛法求素数
- 剑指Offer:包含min函数的栈(JAVA)
- 【SPOJ-BTTNS】Buttons【高斯消元】【异或方程组】
- [iOS]The app icon set named "AppIcon" did not have any applicable content
- __stdcall,__cdecl,__fastcall的区别
- 初窥Linux 之 我最常用的20条命令
- 人物动画的控制
- HDU 5626 Clarke and points
- C#的闭包
- MySQL LEFT JOIN
- 安卓 ImageView ACTION_UP&DOWN监听
- HUST 1358 Uiwurerirexb jeqvad