Codeforces 781C Underground Lab 构造
原文链接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 构造的更多相关文章
- Codeforces 781C Underground Lab
题目链接:http://codeforces.com/problemset/problem/781/C 因为有${K}$个机器人,每个又可以走${\left \lceil \frac{2n}{k} \ ...
- 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 ...
- CodeForces 780 E Underground Lab
Underground Lab 题解: 如果遍历一棵树,我们可以发现最多需要走的步数也不会超过2 * n步. 所以我们选出一棵树,然后遍历一边这颗树. 然后把序列分成k块就好了. 代码: #inclu ...
- codeforces781C Underground Lab
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...
- Underground Lab CodeForces - 782E (欧拉序)
大意:$n$结点,$m$条边无向图, 有$k$个人, 每个人最多走$\left\lceil\frac {2n}{k}\right\rceil$步, 求一种方案使得$k$个人走遍所有的点 $n$结点树的 ...
- Codeforces 1383D - Rearrange(构造)
Codeforces 题面传送门 & 洛谷题面传送门 一道不算困难的构造,花了一节英语课把它搞出来了,题解简单写写吧( 考虑从大往小加数,显然第三个条件可以被翻译为,每次加入一个元素,如果它所 ...
- Codeforces 549B. Looksery Party[构造]
B. Looksery Party time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- codeforces 323A. Black-and-White Cube 构造
输入n 1 <= n <= 100 有一个n * n * n 的立方体,由n ^ 3 个1 * 1 * 1 的单位立方体构成 要用white 和 black 2种颜色来染这n ^ 3个立方 ...
- Codeforces Gym 100531I Instruction 构造
Problem I. Instruction 题目连接: http://codeforces.com/gym/100531/attachments Description Ingrid is a he ...
随机推荐
- Light OJ 1007
求区间欧拉函数平方和... 最后因为longlong 范围爆了WA 了, 0.0 #include<bits/stdc++.h> using namespace std; const in ...
- ZOJ 2110 DFS
狗要出门,且正好在T秒 就是DFS + 剪枝, 联系一下剪枝技巧 #include<iostream> #include<cstdio> #include<cstring ...
- [转]MingGW64 多个版本区别(silj, seh)
1 引言 在https://sourceforge.net/projects/mingw-w64上会看到MinGW有各种版本如下,但区别在于哪儿呢 MinGW-W64 GCC-8.1.0 x86_64 ...
- [PHP]session回收机制及php.ini session生命期
由于PHP的工作机制,它并没有一个daemon线程,来定时地扫描session信息并判断其是否失效.当一个有效请求发生时,PHP会根据全局变量 session.gc_probability/sessi ...
- Spring通过SchedulerFactoryBean实现调度任务的配置(定时器)
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- android7.0以上使用融云即使通讯的坑
一.连接服务器不走connect()方法 在android6.0以下,在使用融云sdk时,直接将依赖库引入到项目中即可.但是在7.0及以上时,直接应用会发现消息一直发送不出去,错误提示为dlopen ...
- 洛谷P4841 城市规划 [生成函数,NTT]
传送门 题意简述:求\(n\)个点的简单无向连通图的数量\(\mod \;1004535809\),\(n \leq 130000\) 经典好题呀!这里介绍两种做法:多项式求逆.多项式求对数 先 ...
- web前端识别文字转语音
const msg = new SpeechSynthesisUtterance("hello world"); window.speechSynthesis.speak(msg) ...
- Ubuntu下Mongodb和Robo3T的安装与使用
Mongodb的安装:https://blog.csdn.net/Canhui_WANG/article/details/78995388 Robo3T的安装:https://www.jianshu. ...
- nginx官方模块之http_random_index_module
作用 目录中选择一个随机主页 语法