AND Minimum Spanning Tree(2019杭电多校训练赛第四场-签到题)

AND Minimum Spanning Tree(2019杭电多校训练赛第四场-签到题)

judge:hduoj 6614
source:2019 Multi-University Training Contest 4
Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 131072/131072 K (Java/Others)

Problem Description

You are given a complete graph with N N N vertices, numbered from 1 1 1 to N N N.
The weight of the edge between vertex x x x and vertex y ( 1 &lt; = x , y &lt; = N , x   ! = y ) y (1&lt;=x, y&lt;=N, x\ !=y) y(1<=x,y<=N,x !=y) is simply the bitwise AND of x x x and y y y. Now you are to find minimum spanning tree of this graph.

Input

The first line of the input contains an integer T ( 1 &lt; = T &lt; = 10 ) T (1&lt;= T &lt;=10) T(1<=T<=10), the number of test cases. Then T T T test cases follow. Each test case consists of one line containing an integer N ( 2 &lt; = N &lt; = 200000 ) N (2&lt;=N&lt;=200000) N(2<=N<=200000).

Output

For each test case, you must output exactly 2 2 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 N-1 N1 space-separated integers f 2 , f 3 , … , f N f2, f3, … , fN f2,f3,,fN, implying there is an edge between i i i and f i f_i fi in your tree ( 2 &lt; = i &lt; = N ) (2&lt;=i&lt;=N) (2<=i<=N). If there are multiple solutions you must output the lexicographically smallest one. A tree T 1 T_1 T1 is lexicographically smaller than tree T 2 T_2 T2, if and only if the sequence f f f obtained by T 1 T_1 T1 is lexicographically smaller than the sequence obtained by T 2 T_2 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.

题意

现在有n个点(n <= 200000),每个点之间都有一条边,x 点和 y 点的权值为x & y。让求一颗最小生成树的总权值。

看到边的权值时想到了位运算,然后又想到了可以让两个&之后为0的点连起来,这样权值为0.又想到打表一个数组存下2的幂指数,因为这些数的二进制位上只有一个1,再拿它们去和剩下的点求&,绝大多数的点(除了n范围内二进制位全为1的那个数)必定能与它们之中的一个或多个&之后为0,因为只要二进制位中有一个0,那么就能找出那一位且只有那一位为1的数去和他求&。而且这些2的幂指数互相求&结果都为0,这样把他们连起来,再以这颗小数为重心连接其他的边,求&不为0的数就和1连起来,这样得到的树肯定是总权值最小!

代码
#include <bits/stdc++.h>
#define maxn 200005
typedef long long ll;
#define _for(i, a) for(ll i = 0; i < (a); i++)
#define _rep(i, a, b) for(ll i = (a); i <=(b); i++)
using namespace std;
ll l = 0;
ll n;
ll number[50];
ll mat[maxn];
int main() {
	//freopen("in.txt", "r", stdin);
	number[0] = 1;
	_rep(i, 1, 31) {
		number[i] = number[i - 1] * 2;
		mat[i] = 1;
	}
	ll T;
	cin >> T;
	while (T--) {
		ll num = 0;
		memset(mat, 0, sizeof(mat));
		scanf("%d", &n);
		_rep(k, 1, n) {
			bool f = 1;
			for (int i = 0; i < 32 && number[i] <= n; i++) {
				if ((k & number[i]) == 0) {
					f = 0;
					mat[k] = number[i];
					break;
				}
			}
			if (f) num++;
		}
		printf("%d\n", num);
		_rep(i, 2, n - 1) printf("%d ", mat[i] ? mat[i] : 1);
		printf("%d\n", mat[n] ? mat[n] : 1);
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值