2019hdu多校 AND Minimum Spanning Tree
题目链接:Click here
题目大意:两个点之间的边权为编号按位与的值,求最小生成树,方案要字典序最小
Solution:
一道不难的构造题,每个点连向他取反后的lowbit值,这样边权为0,若lowbit值大于n,则连1
这样自构造出来的必然是最小生成树,且满足字典序最小
Code:
#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的更多相关文章
- 多校 HDU - 6614 AND Minimum Spanning Tree (二进制)
传送门 AND Minimum Spanning Tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 ...
- 【HDU 4408】Minimum Spanning Tree(最小生成树计数)
Problem Description XXX is very interested in algorithm. After learning the Prim algorithm and Krusk ...
- 数据结构与算法分析–Minimum Spanning Tree(最小生成树)
给定一个无向图,如果他的某个子图中,任意两个顶点都能互相连通并且是一棵树,那么这棵树就叫做生成树(spanning tree). 如果边上有权值,那么使得边权和最小的生成树叫做最小生成树(MST,Mi ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- HDU 4408 Minimum Spanning Tree 最小生成树计数
Minimum Spanning Tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- [Educational Round 3][Codeforces 609E. Minimum spanning tree for each edge]
这题本来是想放在educational round 3的题解里的,但觉得很有意思就单独拿出来写了 题目链接:609E - Minimum spanning tree for each edge 题目大 ...
随机推荐
- [转帖]预警 | Linux 爆“SACK Panic”远程DoS漏洞,大量主机受影响
预警 | Linux 爆“SACK Panic”远程DoS漏洞,大量主机受影响 https://cloud.tencent.com/developer/article/1447879 所有的 版本 ...
- 【转帖】联芸Maxio展示国产PCIe SSD主控:速度可达3.5GB/s
联芸Maxio展示国产PCIe SSD主控:速度可达3.5GB/s https://www.cnbeta.com/articles/tech/855223.htm 国产主控 紫光做国产颗粒 国产器件对 ...
- 利用BFS解决拯救007问题 -- 数据结构
题目: 7-1 拯救007 (30 分) 在老电影“007之生死关头”(Live and Let Die)中有一个情节,007被毒贩抓到一个鳄鱼池中心的小岛上,他用了一种极为大胆的方法逃脱 —— 直接 ...
- Linux普通用户无法使用sudo
问题描述: jenkins执行发布脚本,因为使用的是jenkins用户,所以有些shell命令需要 sudo 来执行,导致报错. + sudo rm -rf /usr/share/nginx/html ...
- ubuntu中安装Python3.7
一. 源码安装: 1. 官网源码下载: Python官网:https://www.python.org/downloads/ setuptools官网:https://pypi.org/project ...
- HDU 1052 Tian Ji -- The Horse Racing (贪心)(转载有修改)
Tian Ji -- The Horse Racing Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- 087、日志管理之 Docker logs (2019-05-09)
参考https://www.cnblogs.com/CloudMan6/p/7749304.html 高效的监控和日志管理对保持生产系统只需稳定的运行以及排查问题至关重要. 在微服务架构中,由 ...
- OCR(光学字符识别)技术简介
OCR技术起源 OCR最早的概念是由德国人Tausheck最先提出的,1966年他们发表了第一篇关于汉字识别的文章,采用了模板匹配法识别了1000个印刷体汉字.早在60.70年代,世界各国就开始有OC ...
- ES使用text类型字段排序报错
elasticsearch text字段排序报错解决使用elasticsearch 进行排序的时候,我们一般都会排序数字.日期.但是在排序text类型的时候就会出现错误. GET xytest/sut ...
- 深入Spring Boot:那些注入不了的 Spring 占位符 ( ${} 表达式 )
Spring里的占位符 spring里的占位符通常表现的形式是: 1 2 3 <bean id="dataSource" destroy-method="close ...