多校 HDU - 6614 AND Minimum Spanning Tree (二进制)
传送门
AND Minimum Spanning Tree
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 378 Accepted Submission(s): 190
The weight of the edge between vertex x and vertex y (1<=x, y<=N, x!=y) is simply the bitwise AND of x and y. Now you are to find minimum spanning tree of this graph.
3
2
1 1
0
1
In the 1st test case, w(1, 2) = w(2, 1) = 0, w(1, 3) = w(3, 1) = 1, w(2, 3) = w(3, 2) = 2. There is only one minimum spanning tree in this graph, i.e. {(1, 2), (1, 3)}.
For the 2nd test case, there is also unique minimum spanning tree.
有N个结点,两两之间的权值为两个点取与
比如说结点2和结点3之间的权值 W(2,3) = 10&11 = 10
问整个图的最小生成树
第一行输出最小生成树的权值和
第二行分别输出2到N节点连接的点(如果一个节点连接了2个点的话,需要输出小的那个)
举个例子:
10011(二进制)最好和哪个结点连接呢
无疑是 00100(二进制)
感受一下?
其实本质就是找到第一个非1的位置,其他的所有位置都置为0就OK。
这里有个特殊情况
如果是111?(二进制)
那么最优的连接结点就是1000。
但是如果N恰好就是7,那么就没有1000(十进制是8)来和111(十进制是7)组队了。
111只能去和1连接(被迫)
所以第一行答案要么是1要么是0
判断一下N是不是二的幂次减1就行
第二行的话可以按照上面的规律去找,即找到第一个非1的位置为1,其他的都为0
#include"bits/stdc++.h"
using namespace std;
typedef long long LL;
int a[];
bool judge(int x){
int n = x&(x-);
return n == ;
}
/*
找到二进制中第一个不是0的
比如说 1001
返回的是 0010
1111返回的是10000
*/
int getMin(int x){
int ans = ;
while(true){
int k = x&;
x >>= ;
if(k == ){
break;
}
ans <<= ;
}
return ans;
}
int main()
{
int _;
for(scanf("%d",&_);_--;){
int n;
scanf("%d",&n);
memset(a,,sizeof(int)*(n+));
judge(n+)?puts(""):puts("");
for(int i = ; i <= n ; i++){
int Min = getMin(i);
Min > n? printf(""):printf("%d",Min);//判断一下找到的数是不是大于n
i == n ? printf("\n"):printf(" ");
}
}
}
/*
2
3
2
*/
多校 HDU - 6614 AND Minimum Spanning Tree (二进制)的更多相关文章
- 【HDU 4408】Minimum Spanning Tree(最小生成树计数)
Problem Description XXX is very interested in algorithm. After learning the Prim algorithm and Krusk ...
- HDU 6614 AND Minimum Spanning
Time limit 1000 ms Memory limit 131072 kB OS Windows 中文题意 给一张n个点的无向完全图(输入一个n就完事了),每个点标号为1~n,每条边的边权为它 ...
- HDU 4408 Minimum Spanning Tree 最小生成树计数
Minimum Spanning Tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- hdu 4408 Minimum Spanning Tree
Problem Description XXX is very interested in algorithm. After learning the Prim algorithm and Krusk ...
- 数据结构与算法分析–Minimum Spanning Tree(最小生成树)
给定一个无向图,如果他的某个子图中,任意两个顶点都能互相连通并且是一棵树,那么这棵树就叫做生成树(spanning tree). 如果边上有权值,那么使得边权和最小的生成树叫做最小生成树(MST,Mi ...
- Educational Codeforces Round 3 E. Minimum spanning tree for each edge LCA/(树链剖分+数据结构) + MST
E. Minimum spanning tree for each edge Connected undirected weighted graph without self-loops and ...
- CF# Educational Codeforces Round 3 E. Minimum spanning tree for each edge
E. Minimum spanning tree for each edge time limit per test 2 seconds memory limit per test 256 megab ...
- Codeforces Educational Codeforces Round 3 E. Minimum spanning tree for each edge LCA链上最大值
E. Minimum spanning tree for each edge 题目连接: http://www.codeforces.com/contest/609/problem/E Descrip ...
- MST(Kruskal’s Minimum Spanning Tree Algorithm)
You may refer to the main idea of MST in graph theory. http://en.wikipedia.org/wiki/Minimum_spanning ...
随机推荐
- 2018-11-21-WPF-解决-ViewBox--不显示线的问题
title author date CreateTime categories WPF 解决 ViewBox 不显示线的问题 lindexi 2018-11-21 09:37:53 +0800 201 ...
- Hdu 1867 KMP
题目链接 题目意思: 给出两个字符串a, b, 求最长的公共字串c, c是a的后缀,也是b的前缀. 本题没有具体说明哪个字符串是文本串和匹配串, 所以都要考虑 思路: 查找的时候, 当文本串结束的时候 ...
- 【JZOJ4846】【NOIP2016提高A组集训第5场11.2】行走
题目描述 数据范围 对于70%的数据保证 n <= 1000 对于100%的数据保证 n,q <= 10^5,c_i,v_i <= 10^{18} 保证每次修改后的边权小于等于原来的 ...
- MaxCompute 费用暴涨之新增SQL分区裁剪失败
现象:因业务需求新增了SQL任务,这SQL扫描的表为分区表,且SQL条件里表只指定了一个分区,按指定的分区来看数据量并不大,但是SQL的费用非常高.费用比预想的结果相差几倍甚至10倍以上. 若只知道总 ...
- XML之DOM解析文档 Day24
TestDom.java package com.sxt.dom; import java.io.File; import java.io.IOException; import javax.xml. ...
- 2018-9-19-Roslyn-通过-Nuget-管理公司配置
title author date CreateTime categories Roslyn 通过 Nuget 管理公司配置 lindexi 2018-9-19 10:57:5 +0800 2018- ...
- 1176. Two Ends
题目链接地址:http://soj.me/1176 题目大意:两头取数.第一个人随机取,第二个人用贪婪算法(每次都取大的),求两人取数在第一个人赢的情况下的最大分差.使用贪婪算法时,如果左右两边相等, ...
- Mysql查询某个月的每一天的数据
需求:查询最近三个月的每一天的业绩总和 因为最近三个月每个月的天数是不一样,所以不能用这篇文章:Mysql查询最近30天的数据(每天的业绩总和数据) 介绍的用固定多少天去查数据.需要一个新方法. 一. ...
- 1、Ubuntu 16.04 安装.net core
Register the Microsoft key register the product repository Install required dependencies 参考网址:https: ...
- Java 内存模型及GC原理 (转)
来源:http://blog.csdn.net/ithomer/article/details/6252552 一个优秀Java程序员,必须了解Java内存模型.GC工作原理,以及如何优化GC的性 ...