本文版权归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. Win查询注册表获取CPU与内存参数

    #include "stdafx.h" void dump_machine_info( HANDLE fh ) { CHAR Str[MAX_PATH*3]; CHAR MHzSt ...

  2. Linux内核调试技术——jprobe使用与实现

    前一篇博文介绍了kprobes的原理与kprobe的使用与实现方式,本文介绍kprobes中的另外一种探測技术jprobe.它基于kprobe实现,不能在函数的任何位置插入探測点,仅仅能在函数的入口处 ...

  3. Appium-Java滑动操作

    Java滑动操作,通常可以直接使用API中AndroidDriver类中的swipe方法,直接进行调用 swipe(int startx, int starty, int endx, int endy ...

  4. Delphi APP 開發入門(六)Object Pascal 語法初探

    Delphi APP 開發入門(六)Object Pascal 語法初探 分享: Share on facebookShare on twitterShare on google_plusone_sh ...

  5. Leetcode 236

    思路:1.如果p或q就是根节点,那么LCA=p或q,返回根节点(递归出口) 2.分治 2.1 Divide:分别计算左字树和右子树的LCA 2.2 Conquer:如果左字树和右子树的计算结果均不为空 ...

  6. kubernetes上安装MongoDB-3.6.5集群副本集方式

    一.安装部署: 想直接一步创建集群的小伙伴直接按以下步骤安装(再往后是记录自己出过的错): 1.生成docker镜像: docker build -t 144.202.127.156/library/ ...

  7. 48. Rotate Image(旋转矩阵)

      You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise) ...

  8. [转]运动检测(前景检测)之(二)混合高斯模型GMM

    转自:http://blog.csdn.net/zouxy09/article/details/9622401 因为监控发展的需求,目前前景检测的研究还是很多的,也出现了很多新的方法和思路.个人了解的 ...

  9. CCProgressTimer Loading条

    CCProgressTimer *p_SpTime = CCProgressTimer::create(CCSprite::createWithSpriteFrameName("IM_Pro ...

  10. c++之旅:继承

    继承 继承有关于权限的继承,多继承和虚继承 权限继承 权限继承有公有继承,保护继承和私有继承 公有继承 公有继承可以继承父类的public和protected属性和方法 #include <io ...