本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作。

本文作者:ljh2000
作者博客:http://www.cnblogs.com/ljh2000-jump/
转载请注明出处,侵权必究,保留最终解释权!

题目链接:codeforces781C Underground Lab

正解:生成树+欧拉序列

解题报告:

  很容易发现,原图和图的一棵生成树其实是等价的,都是需要遍历全图,是一个类似于$dfs$的东西。

  欧拉序列正可以满足题目所给的性质,所以我只要搞出一棵原图的生成树,然后把欧拉序列分成$k$段即可。

//It is made by ljh2000
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <string>
#include <complex>
using namespace std;
typedef long long LL;
typedef long double LB;
typedef complex<double> C;
const double pi = acos(-1);
const int MAXN = 200011;
const int MAXM = 400011;
int n,m,k,father[MAXN],ecnt,first[MAXN],next[MAXM],to[MAXM];
int dfn[MAXN*2],cnt,ans[MAXN],out;
inline int find(int x){ if(father[x]!=x) father[x]=find(father[x]); return father[x]; }
inline void link(int x,int y){ next[++ecnt]=first[x]; first[x]=ecnt; to[ecnt]=y; }
inline int getint(){
int w=0,q=0; char c=getchar(); while((c<'0'||c>'9') && c!='-') c=getchar();
if(c=='-') q=1,c=getchar(); while (c>='0'&&c<='9') w=w*10+c-'0',c=getchar(); return q?-w:w;
} inline void dfs(int x,int fa){
dfn[++cnt]=x;
for(int i=first[x];i;i=next[i]) {
int v=to[i]; if(v==fa) continue;
dfs(v,x);
dfn[++cnt]=x;
}
} inline void work(){
n=getint(); m=getint(); k=getint(); int x,y;
for(int i=1;i<=n;i++) father[i]=i;
for(int i=1;i<=m;i++) {
x=getint(); y=getint();
if(find(x)==find(y)) continue;
link(x,y); link(y,x);
father[find(x)]=find(y);
}
dfs(1,0); int now=0,cc,lim;
lim=2*n/k; if(lim*k<2*n) lim++;
for(int i=1;i<=k;i++) {
cc=0; out=0;
if(now<cnt) { while(cc<lim && now<cnt) now++,ans[++out]=dfn[now],cc++; }
else ans[out=1]=1;
printf("%d ",out);
for(int j=1;j<=out;j++) printf("%d ",ans[j]);
puts("");
}
} int main()
{
work();
return 0;
}

  

codeforces781C Underground Lab的更多相关文章

  1. 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 ...

  2. CodeForces 780 E Underground Lab

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

  3. Codeforces 781C Underground Lab 构造

    原文链接https://www.cnblogs.com/zhouzhendong/p/CF781C.html 题目传送门 - CF781C 题意 给定一个 n 个点 m 条边的无向连通图,请你用 k ...

  4. Codeforces 781C Underground Lab

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

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

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

  6. 782E. Underground Lab DFS 好题

    Link 题意:给出一个图,有n个点,m条边,k个人,每个人至多只能走$\lceil\frac{2n}{k}\rceil$步,输出可行的方案即输出每个人所走的步数和所走点 思路: 由于保证给出的是连通 ...

  7. Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals)

    Codeforces Round #403 (Div. 2, based on Technocup 2017 Finals) 说一点东西: 昨天晚上$9:05$开始太不好了,我在学校学校$9:40$放 ...

  8. Codeforces Round #403 (Div. 1, based on Technocup 2017 Finals)

    Div1单场我从来就没上过分,这场又剧毒,半天才打出B,C挂了好几次最后还FST了,回紫了. AC:AB Rank:340 Rating:2204-71->2133 Div2.B.The Mee ...

  9. Codeforces Round#403 (Div. 1)

    唉,昨天晚上迷迷糊糊地去打cf,结果fst两题,掉回蓝了... A.Andryusha and Colored Balloons 题意:给定一棵树,任意两个距离小等于二的点不能染相同的颜色,求最小颜色 ...

随机推荐

  1. 网络编程(基于udp协议的套接字/socketserver模块/进程简介)

    一.基于UDP协议的套接字 TCP是建立可靠连接,并且通信双方都可以以流的形式发送数据.相对TCP,UDP则是面向无连接的协议. 使用UDP协议时,不需要建立连接,只需要知道对方的IP地址和端口号,就 ...

  2. Python 之父谈放弃 Python:我对核心成员们失望至极!

    Python 之父讲述退位原因,以及 Python 的未来将何去何从. ​ 在 Python 社区,Python 的发明者 Guido Van Rossum 被称为 “仁慈的终生独裁者”(BDFL,B ...

  3. 使用selenium找出外卖点餐次数最多的10个顾客

    大锅在做外卖,给我说能否统计出这半年点餐次数最多的10个顾客,我不知道APP本身是否有这个功能,想了下最近用selenium较多,就用selenium尝试下吧. 1 定义一个类,这里先描述需要的属性和 ...

  4. sql中的连接表达式,视图,事务等。

    给定两张表 表A ),description )); ,'N1','AD1'); ,'N2','AD2'); mysql> SELECT * FROM a; +----+------+----- ...

  5. 教你编译PHP7 (nginx+mysql+php7)

    # 安装编译工具: yum install gcc automake autoconf libtool gcc-c++ # 安装基础库 yum install gd zlib zlib-devel o ...

  6. Java中树的存储结构实现

    一.树 树与线性表.栈.队列等线性结构不同,树是一种非线性结构. 一棵树只有一个根节点,如果一棵树有了多个根节点,那它已经不再是一棵树了,而是多棵树的集合,也被称为森林. 二.树的父节点表示法 树中除 ...

  7. Lua4.0中getn陷阱

    Lua4.0中getn潜规则 • 使用t[n] = nil方式删除元素会导致意外结果 t = {, , , , , , , , , }; t[] = nil; t = {, , , , , , , , ...

  8. Delphi APP 開發入門(三)簡易計算機

    Delphi APP 開發入門(三)簡易計算機 分享: Share on facebookShare on twitterShare on google_plusone_share   閲讀次數:68 ...

  9. scala和正则表达式常用基础知识示例

    http://www.cnblogs.com/deerchao/archive/2006/08/24/zhengzhe30fengzhongjiaocheng.html .     匹配除换行符以外的 ...

  10. 使用.NET Core和Vue搭建WebSocket聊天室

    博客地址是:https://qinyuanpei.github.io.  WebSocket是HTML5标准中的一部分,从Socket这个字眼我们就可以知道,这是一种网络通信协议.WebSocket是 ...