传送门

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

Problem Description
You are given a complete graph with N vertices, numbered from 1 to N. 
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.
 
Input
The first line of the input contains an integer T (1<= T <=10), the number of test cases. Then T test cases follow. Each test case consists of one line containing an integer N (2<=N<=200000).
 
Output
For each test case, you must output exactly 2 lines. You must print the weight of the minimum spanning tree in the 1st line. In the 2nd line, you must print N-1 space-separated integers f2, f3, … , fN, implying there is an edge between i and fi in your tree(2<=i<=N). If there are multiple solutions you must output the lexicographically smallest one. A tree T1 is lexicographically smaller than tree T2, if and only if the sequence f obtained by T1 is lexicographically smaller than the sequence obtained by T2.
 
Sample Input
2
3
2
 
Sample Output
1
1 1
0
1

Hint

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.

 
Source
 
 
题意:

有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 (二进制)的更多相关文章

  1. 【HDU 4408】Minimum Spanning Tree(最小生成树计数)

    Problem Description XXX is very interested in algorithm. After learning the Prim algorithm and Krusk ...

  2. HDU 6614 AND Minimum Spanning

    Time limit 1000 ms Memory limit 131072 kB OS Windows 中文题意 给一张n个点的无向完全图(输入一个n就完事了),每个点标号为1~n,每条边的边权为它 ...

  3. HDU 4408 Minimum Spanning Tree 最小生成树计数

    Minimum Spanning Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  4. hdu 4408 Minimum Spanning Tree

    Problem Description XXX is very interested in algorithm. After learning the Prim algorithm and Krusk ...

  5. 数据结构与算法分析–Minimum Spanning Tree(最小生成树)

    给定一个无向图,如果他的某个子图中,任意两个顶点都能互相连通并且是一棵树,那么这棵树就叫做生成树(spanning tree). 如果边上有权值,那么使得边权和最小的生成树叫做最小生成树(MST,Mi ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. js多图上传展示和删除

    html部分 <button class="btn btn-info" for="file">请选择文件</button> <in ...

  2. python 使用自定义的类方法

  3. 【风马一族_php】NO4_php基础知识

    原文来自:http://www.cnblogs.com/sows/p/6017018.html(博客园的)风马一族 侵犯版本,后果自负 回顾 运算符:算术运算符.逻辑运算符.比较运算符.位运算符.赋值 ...

  4. 闲鱼Flutter&FaaS云端一体化架构

    讲师介绍 国有,闲鱼架构团队负责人.在7月13号落幕的2019年Archsummit峰会上就近一年来闲鱼在Flutter&FaaS一体化项目上的探索和实践进行了分享. 传统Native+Web ...

  5. 【时光回溯】【JZOJ3568】【GDKOI2014】小纪的作业题

    题目描述 输入 输出 有M行,每个询问一行,输出结果mod 1,000,000,007的值. 样例输入 10 3 3 5 1 2 3 1 3 5 2 1 7 9 3 9 2 3 样例输出 10 19 ...

  6. hdu2044 dp

    /* 每一种情况都可以由周围2个点得出 */ #include<stdio.h> int main() { __int64 dp[]; int i,t,l,r; dp[]=; dp[]=; ...

  7. oracle Sql语句分类

    dml语句:数据操作语句[insert,update,delete] ddl语句:数据定义语言[create table,drop table] dql语句:数据查询语句[select] dtl语句: ...

  8. Ubuntu修改root密码,ssh 允许root用户登录

    1,切换为root用户 2,passwd root(or others) 3,输两次密码 4,重启. ssh允许root用户登录: 1,vim /etc/ssh/sshd_config 2,修改Per ...

  9. 14 个你可能不知道的 JavaScript 调试技巧

    了解你的工具可以极大的帮助你完成任务.尽管 JavaScript 的调试非常麻烦,但在掌握了技巧 (tricks) 的情况下,你依然可以用尽量少的的时间解决这些错误 (errors) 和问题 (bug ...

  10. Codeforces Round #591 (Div. 2, based on Technocup 2020 Elimination Round 1) 题解

    A..B略 C 对当前的值排序,再二分答案,然后对于(i%x==0 && i%y==0)放入大的,再放其他的贪心解决即可. #include<iostream> #incl ...