原文链接https://www.cnblogs.com/zhouzhendong/p/CF781C.html

题目传送门 - CF781C

题意

  给定一个 n 个点 m 条边的无向连通图,请你用 k 条长度不大于 $\lceil 2n/k \rceil$ 的路径来覆盖所有节点至少一次。每一条路径长度至少为 1 ,同一条路径可以多次经过同一个节点。

  $n,m\leq 2\times 10^5,\ \ 1\leq k\leq n$

题解

  连通图是一个很优秀的性质。

  我们只需要找出这个图的任意一个 dfs 生成树,并求出其欧拉序。由于欧拉序可以表示成一条长度为 2n-1 的连续路径,所以我们只需要把他分成 k 段就好了。

代码

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
LL read(){
LL x=0,f=1;
char ch=getchar();
while (!isdigit(ch)&&ch!='-')
ch=getchar();
if (ch=='-')
f=-1,ch=getchar();
while (isdigit(ch))
x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
return x*f;
}
const int N=400005;
int n,m,k;
vector <int> e[N];
int vis[N];
int dfn[N],b[N],t=0;
void dfs(int x){
dfn[++t]=x;
vis[x]=1;
for (auto y : e[x])
if (!vis[y]){
dfs(y);
dfn[++t]=x;
}
}
int main(){
n=read(),m=read(),k=read();
for (int i=1;i<=m;i++){
int a=read(),b=read();
e[a].push_back(b);
e[b].push_back(a);
}
memset(vis,0,sizeof vis);
dfs(1);
int s=(2*n+k-1)/k;
memset(b,0,sizeof b);
b[t]=1;
for (int i=1;i<k;i++)
b[i]=1;
for (int i=k-1,last=t;i>=1;i--){
if (last-i>s)
swap(b[i],b[last-s]),last=last-s;
else
last=i;
}
for (int last=1,i=1;i<=t;i++)
if (b[i]){
printf("%d ",i-last+1);
for (int j=last;j<=i;j++)
printf("%d ",dfn[j]);
puts("");
last=i+1;
}
return 0;
}

  

Codeforces 781C Underground Lab 构造的更多相关文章

  1. Codeforces 781C Underground Lab

    题目链接:http://codeforces.com/problemset/problem/781/C 因为有${K}$个机器人,每个又可以走${\left \lceil \frac{2n}{k} \ ...

  2. Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) E Underground Lab

    地址:http://codeforces.com/contest/782/problem/E 题目: E. Underground Lab time limit per test 1 second m ...

  3. CodeForces 780 E Underground Lab

    Underground Lab 题解: 如果遍历一棵树,我们可以发现最多需要走的步数也不会超过2 * n步. 所以我们选出一棵树,然后遍历一边这颗树. 然后把序列分成k块就好了. 代码: #inclu ...

  4. codeforces781C Underground Lab

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...

  5. Underground Lab CodeForces - 782E (欧拉序)

    大意:$n$结点,$m$条边无向图, 有$k$个人, 每个人最多走$\left\lceil\frac {2n}{k}\right\rceil$步, 求一种方案使得$k$个人走遍所有的点 $n$结点树的 ...

  6. Codeforces 1383D - Rearrange(构造)

    Codeforces 题面传送门 & 洛谷题面传送门 一道不算困难的构造,花了一节英语课把它搞出来了,题解简单写写吧( 考虑从大往小加数,显然第三个条件可以被翻译为,每次加入一个元素,如果它所 ...

  7. Codeforces 549B. Looksery Party[构造]

    B. Looksery Party time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  8. codeforces 323A. Black-and-White Cube 构造

    输入n 1 <= n <= 100 有一个n * n * n 的立方体,由n ^ 3 个1 * 1 * 1 的单位立方体构成 要用white 和 black 2种颜色来染这n ^ 3个立方 ...

  9. Codeforces Gym 100531I Instruction 构造

    Problem I. Instruction 题目连接: http://codeforces.com/gym/100531/attachments Description Ingrid is a he ...

随机推荐

  1. atoi 和 itoa的实现

    atoi 和 itoa是面试笔试经常要考到的题目,下面两份代码是用C语言实现的atoi和itoa: 1, atoi 原型: int atoi(const char *nptr); 函数说明: 参数np ...

  2. python-re模块和subprocess模块

    一.re模块 re中文为正则表达式,是字符串处理的常用工具,通常用来检索和替换符合某个模式的文本. 注:要搜索的模式和字符串都可以是unicode字符串(str)和8位字符串(bytes),但是不能将 ...

  3. Confluence 6 恢复一个站点问题解决

    如果你在导入的时候遇到了问题,检查下面的一些提示. 你的文件太大而不能上传?这个是非常常见的错误.出现的原因是备份文件不能在规定的时间内上传到服务器上.为了避免这个错误,放置你的导出文件到  < ...

  4. 移动端的dl

    https://blog.csdn.net/u013139259/article/details/52143240

  5. nodejs之crypto加密算法

    示例 const crypto = require('crypto'); const hash = crypto.createHash('sha256'); hash.update('some dat ...

  6. vue的单选框

  7. Mybait缓存机制

    MyBatis同大多数ORM框架一样,提供了一级缓存和二级缓存的支持. 一级缓存:其作用域为session范围内,当session执行flush或close方法后,一级缓存会被清空. 二级缓存:二级缓 ...

  8. Linux基础实操三

    实操一: 1) 将用户信息数据库文件和组信息数据库文件纵向合并为一个文件/1.txt(覆盖) cd /etc -->tar passwd * group * > 1.txt 2) 将用户信 ...

  9. 开始接触python

    1.什么是语言? 语言是一个事物与另一个事物交流的介质 python是人与计算机交流的介质 能够被计算机所识别的表达方式即是编程语言 2.什么是编程? 编程就是程序员将想让计算机做的事情用编程语言表达 ...

  10. SpringMVC + MyBatis + Mysql + Redis(作为二级缓存) 配置

    2016年03月03日 10:37:47 标签: mysql / redis / mybatis / spring mvc / spring 33805 项目环境: 在SpringMVC + MyBa ...