HDU 6614【2019 Multi-University Training Contest 4】 AND Minimum Spanning Tree 二进制取反的操作

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.

题目大意:
N个点,每两个点之间的边权是两点之间的AND(与运算)值,问所构成的最小生成树的权值和是多少,如果存在多个和都是最小值,那么就输出字典序最小的那个;

分析:
对于1~n里面的数,咱们先随便选出一个数,假设二进制是11011011,那么能与这个值相连的最小值是不是100呢,这样and运算之后的结果是0,因此如果是最后一位0的位置是大于等于1的都可以找到对应的值相连,其他的情况就是全1的,比如111,1111,这种情况就需要特殊判断了,如果是1111,那么就看1000是不是大于n如果大于就和1号点相连,这样权值最小为1,因此知道规律后暴力就好了

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<string>
#include<cmath>
#include<cstring>
#include<set>
#include<queue>
#include<stack>
#include<map>
typedef long long ll;
using namespace std;
const int N=2e5+10;
int lowbit(int x){
    return x&(-x);
}
int bit(int x){
    int cnt=0;
    while(x)
    x/2,cnt++;
    return cnt;
}
int a[N],cnt[N];
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
    #endif // ONLINE_JUDGE
    int T;
    scanf("%d",&T);
    int n;
    for(int i=1;i<=20;i++)
        cnt[i]=(1<<i);
    while(T--){
        scanf("%d",&n);
        if(n==2){
            printf("0\n1\n");
        }else{
            int sum=0;
            int ub=0;
        for(int i=1;i<=20;i++)
        if(cnt[i+1]>n) {
            ub=i;break;
        }
            a[2]=1;
            for(int i=3;i<=n;i++){
                if(lowbit(i)>1) a[i]=1;
                else{
                    int flag=1;
                    for(int j=1;j<=ub;j++)
                    if((i&cnt[j])==0){
                        a[i]=cnt[j];
                        flag=0;
                        break;
                    }
                    if(flag)
                        a[i]=1,sum++;
                }
            }
            printf("%d\n",sum);
            for(int i=2;i<n;i++)
                printf("%d ",a[i]);
            printf("%d\n",a[n]);
        }
    }
    return 0;
}

其实上面的方法比较麻烦了,有个简单的方法,因为我不知道如何去找比如1001011这个二进制数的最低位的0的位置,这里有一个简单的方法。

#include<cstdio>
using namespace std;
int T,n,ans;
int a[200005];
int main()
{
    scanf("%d",&T);
    while(T--)
    {
        ans=0;
        scanf("%d",&n);
        for(int i=2;i<=n;i++)
        {
            int x=(1<<25)-i-1;//题目中说N<=2e5,这样就是小于2^25,这样就可以
	//得到i的反码是多少了,比如i一开始是110,假设x<<10那么x就是1111111001
            x=x&(-x);//树状数组里面lowbit
            if(x<=n)
                a[i]=x;
            else
            {
                a[i]=1;
                ans++;
            }            
        }
        printf("%d\n",ans);
        for(int i=2;i<=n;i++)
            printf("%d%c",a[i],i==n?'\n':' ');
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值