2019HDU多校第四场 AND Minimum Spanning Tree HDU - 6614

本文探讨了一个关于完全图的算法问题,其中边的权重由顶点间的按位与操作决定。通过分析,找到了构建最小生成树的有效策略,即从最低位开始检查二进制表示,遇到第一个0则尝试与之前的1进行配对,若不可则向上查找。文章提供了完整的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一个思路题 画了好多 才看出来 有点菜 orz

AND Minimum Spanning Tree

题面

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.
给了你1 到 n 边权是 2点 & 的值
很快知道 只有 1 和 0 但是 一开始没有思考找最小 只是构造出来 wa了一发 然后脑子开始不转了 花了点时间 想到从低开始 遇到第一个二进制 0 与之前 的 100… 数据 看看能不能对其
如果没有0不然就向上找 + 1… 能不能再n以下 就过了

#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e6 + 5;

int main() {
    int t, n;
    scanf("%d", &t);
    while(t -- ) {
        scanf("%d", &n);
        
        int ejws = 0;
        int m = n;
        while(m) {
            ejws ++;
            m/=2;
        } 
        
        int q1s = 0;
        q1s = (1 << ejws) - 1;
        
        int ans = 0;
        
        if(n == q1s) ans = 1;
        printf("%d\n", ans);
        
        for(int i = 2; i <= n; i ++) {
            int ejws = 0;
            int m = i;
            int f = 0;
            while(m) {
                if(m % 2 == 0) {
                    printf("%d", 1 << ejws);
                    f = 1;
                    break;
                }
                m /= 2;
                ejws ++;
            }
            if(!f) {
                ;if(i + 1 <= n) printf("%d", i + 1); 
                else printf("%d", 1); 
            }
            if(i == n) puts("");
            else printf(" ");
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值