hdu 5627 Clarke and MST(最大 生成树)
4 5
1 2 5
1 3 3
1 4 2
2 3 1
3 4 7
首先贴上自己的写法,虽然不是很正宗的做法
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
#include <stack>
using namespace std;
#define PI acos(-1.0)
#define max(a,b) (a) > (b) ? (a) : (b)
#define min(a,b) (a) < (b) ? (a) : (b)
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 300006
#define M 300006
#define inf 1e12
struct Node{
int x,y;
int cost;
}edge[M];
int n,m;
int fa[N];
void init(){
for(int i=;i<N;i++){
fa[i]=i;
}
}
int find(int x){
return fa[x]==x?x:fa[x]=find(fa[x]);
}
bool cmp(Node a,Node b){
return a.cost>b.cost;
}
int main()
{
int t;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
init();
for(int i=;i<m;i++){
int a,b,c;
scanf("%d%d%d",&edge[i].x,&edge[i].y,&edge[i].cost);
}
sort(edge,edge+m,cmp);
int flag=;
int ans;
int num=n-;
for(int i=;i<m;i++){
int root1=find(edge[i].x);
int root2=find(edge[i].y);
if(root1!=root2){
if(flag){
ans=edge[i].cost;
flag=;
}else{
ans&=edge[i].cost;
}
fa[root1]=root2;
num--;
}
}
if(num!=){
printf("0\n");
}
else{
printf("%d\n",ans);
}
} return ;
}
官方题解:
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int N = + ; struct Edge{
int from,to,dis;
}a[N],b[N];
int fa[N];
int find(int x){
if(x==fa[x]) return x;
return fa[x] = find(fa[x]);
}
int tmp;
bool solve(int pos, Edge *a, int n, int m){
for(int i=;i<=n;++i)
fa[i] = i;
int cnt = ;
tmp = ;
for(int i=;i<=m;++i){
if(((a[i].dis>>pos)&)==)
continue; int fu = find(a[i].from);
int fv = find(a[i].to);
if(fu!=fv){
if(cnt==)
tmp = a[i].dis;
else
tmp &= a[i].dis; fa[fu] = fv;
cnt++;
if(cnt==n-)
return true;
}
}
return false;
}
int main() { int t,n,m;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m); for(int i=;i<=m;++i){
scanf("%d%d%d",&a[i].from,&a[i].to,&a[i].dis);
}
int ans = ;
for(int i=;i>=;--i){
if(solve(i,a,n,m)){
ans = tmp;
int mm = ;
for(int i=;i<=m;++i){
if((a[i].dis>>i)&)
b[++mm] = a[i];
}
m = mm;
for(int i=;i<=m;++i)
a[i] = b[i];
}
}
cout<<ans<<endl;
}
return ;
}
hdu 5627 Clarke and MST(最大 生成树)的更多相关文章
- HDU 5627 Clarke and MST &意义下最大生成树 贪心
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5627 题意:Bestcoder的一道题,让你求&意义下的最大生成树. 解法: 贪心,我们从高位 ...
- HDU 5628 Clarke and math——卷积,dp,组合
HDU 5628 Clarke and math 本文属于一个总结了一堆做法的玩意...... 题目 简单的一个式子:给定$n,k,f(i)$,求 然后数据范围不重要,重要的是如何优化这个做法. 这个 ...
- hdu 3367(Pseudoforest ) (最大生成树)
Pseudoforest Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tot ...
- HDU 5629 Clarke and tree dp+prufer序列
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=562 题意: 求给每个节点的度数允许的最大值,让你求k个节点能组成的不同的生成树个数. 题解: 对于n ...
- hdu 5565 Clarke and baton 二分
Clarke and baton Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php? ...
- hdu 5563 Clarke and five-pointed star 水题
Clarke and five-pointed star Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/show ...
- hdu 5465 Clarke and puzzle 二维线段树
Clarke and puzzle Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php? ...
- hdu 5464 Clarke and problem dp
Clarke and problem Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php ...
- HDU 5628 Clarke and math dp+数学
Clarke and math 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5628 Description Clarke is a patient ...
随机推荐
- hdu 5642 King's Order(数位dp)
Problem Description After the king's speech , everyone is encouraged. But the war is not over. The k ...
- poj 3180 The Cow Prom(tarjan+缩点 easy)
Description The N ( <= N <= ,) cows are so excited: it's prom night! They are dressed in their ...
- java删除文件夹下所有文件
package org.sw; import java.io.File; /** * * @author mengzw * @since 3.0 2014-2-26 */ public class D ...
- HDU4496_D-City(并查集删边/逆向)
D-City Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) Total Sub ...
- Android应用程序安装过程源代码分析
文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6766010 Android系统在启动的过程中, ...
- MBTI性格测试
INFP 哲学家型——生活在自己的理想世界 报告接收人: 才储成员4361454 日期: 2014/9/2 一.你的MBTI图形 倾向示意图表示四个维度分别的倾向程度.从中间往两侧看,绿色指示条对应下 ...
- FZU1327 优先队列
Problem 1327 Blocks of Stones II Accept: 318 Submit: 881Time Limit: 1000 mSec Memory Limit : 3 ...
- Qss样式(二)
Qss 其实就是一段文本,当然得按一定格式来写.下面请看Qss的语法格式: 选择器 { 属性1:值:属性2:值:--属性n:值:} 对应上篇文章的Qss样式: 现在我们可以来解释这段话的意思了: 对应 ...
- java中的native关键字
参照下面的链接http://blog.163.com/yueyemaitian@126/blog/static/21475796200701491621267/
- 轻量jquery框架之--组件交互基础设计
概要 组件交互基础,即考虑在JQUERY对象下($)下扩展所有组件都需要用到的通用api,如ajax入口.对表单的操作.html片段加载.通用的配合datagrid通用的curd客户端对象等. 扩展a ...