标签:生成 字典 ble main class ++ ref += 大于
题目链接:Click here
题目大意:两个点之间的边权为编号按位与的值,求最小生成树,方案要字典序最小
一道不难的构造题,每个点连向他取反后的lowbit值,这样边权为0,若lowbit值大于n,则连1
这样自构造出来的必然是最小生成树,且满足字典序最小
#include<bits/stdc++.h>
using namespace std;
const int N=2e5+1;
int n,ans=0,a[N];
int lowbit(int x){return x&(-x);}
int read(){
int x=0,f=1;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')f=-f;ch=getchar();}
while(isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;
}
void solve(){
int n=read(),ans=0;
for(int i=2;i<=n;i++){
int x=lowbit(~i);
a[i]=x<=n?x:1;
ans+=(a[i]&i);
}printf("%d\n",ans);
for(int i=2;i<n;i++) printf("%d ",a[i]);
printf("%d\n",a[n]);
}
int main(){
int t=read();
while(t--) solve();
return 0;
}
2019hdu多校 AND Minimum Spanning Tree
标签:生成 字典 ble main class ++ ref += 大于
原文地址:https://www.cnblogs.com/NLDQY/p/11291692.html