782E. Underground Lab DFS 好题
题意:给出一个图,有n个点,m条边,k个人,每个人至多只能走$\lceil\frac{2n}{k}\rceil$步,输出可行的方案即输出每个人所走的步数和所走点
思路: 由于保证给出的是连通图,且每人$\lceil\frac{2n}{k}\rceil$的步数都用完的话,显然必定有答案。那么我们不妨构造一个DFS的遍历序列(包括回溯的过程),那么最后对这个序列进行分配即可,如果图已经走完,而还有人没走,那么直接输出 即可
/** @Date : 2017-05-10 18:06:33
* @FileName: 782E DFS.cpp
* @Platform: Windows
* @Author : Lweleth (SoundEarlf@gmail.com)
* @Link : https://github.com/Lweleth
* @Version : $Id$
*/
#include <bits/stdc++.h>
#define LL long long
#define PII pair
#define MP(x, y) make_pair((x),(y))
#define fi first
#define se second
#define PB(x) push_back((x))
#define MMG(x) memset((x), -1,sizeof(x))
#define MMF(x) memset((x),0,sizeof(x))
#define MMI(x) memset((x), INF, sizeof(x))
using namespace std; const int INF = 0x3f3f3f3f;
const int N = 1e5+20;
const double eps = 1e-8; int n, m, k;
vector vt[2*N];
int a[2*N];
int ans[2*N];
bool vis[N]; int cnt = 0;
void dfs(int x)
{
vis[x] = 1;
a[cnt++] = x;
int l = vt[x].size();
for(int i = 0; i < l; i++)
{
int np = vt[x][i];
if(vis[np])
continue;
dfs(np);
a[cnt++] = x;//模拟回溯
}
} int main()
{
while(cin >> n >> m >> k)
{
for(int i = 0; i < m; i++)
{
int x, y;
scanf("%d%d", &x, &y);
vt[x].PB(y);
vt[y].PB(x);
}
int ma = 2*n/k + ((2*n)%k?1:0);
MMF(vis);
cnt = 0;
dfs(1);
int cc = 0;
for(int i = 0; i < cnt; i++)
{
//cout << a[i]<<"~";
if(cc < ma)
ans[cc++] = a[i];
else
{
k--;
printf("%d ", ma);
for(int j = 0; j < cc; j++)
printf("%d%s", ans[j], j==cc-1?"\n":" ");
cc = 0;
ans[cc++] = a[i];
}
}
if(cc > 0)
printf("%d ", cc), k--;
for(int i = 0; i < cc; i++)
printf("%d%s", ans[i], i==cc-1?"\n":" ");
for(int i = 0; i < k; i++)
printf("1 1\n");
}
return 0;
}
782E. Underground Lab DFS 好题的更多相关文章
- 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 ...
- codeforces781C Underground Lab
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...
- CodeForces 780 E Underground Lab
Underground Lab 题解: 如果遍历一棵树,我们可以发现最多需要走的步数也不会超过2 * n步. 所以我们选出一棵树,然后遍历一边这颗树. 然后把序列分成k块就好了. 代码: #inclu ...
- POJ 1321 棋盘问题(DFS板子题,简单搜索练习)
棋盘问题 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 44012 Accepted: 21375 Descriptio ...
- poj1564 Sum It Up dfs水题
题目描述: Description Given a specified total t and a list of n integers, find all distinct sums using n ...
- 咸鱼的ACM之路:DFS水题集
DFS的核心就是从一种状态出发,转向任意的一个可行状态,直到达到结束条件为止.(个人理解) 下面全是洛谷题,毕竟能找到测试点数据的OJ我就找到这一个....在其他OJ上直接各种玄学问题... P159 ...
- Underground Lab CodeForces - 782E (欧拉序)
大意:$n$结点,$m$条边无向图, 有$k$个人, 每个人最多走$\left\lceil\frac {2n}{k}\right\rceil$步, 求一种方案使得$k$个人走遍所有的点 $n$结点树的 ...
- Oil Deposits(poj 1526 DFS入门题)
http://poj.org/problem?id=1562 ...
- hdu 1045:Fire Net(DFS经典题)
Fire Net Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su ...
随机推荐
- textarea中文提交乱码问题解决
在A.jsp中有如下语句: <textarea rows="10" cols="30" name="texts"><%=r ...
- jquery新版本旧版本之间的坑
JQuery自1.6.1版本开始增加一些属性,使用时尽量使用这些新的属性,例如:selected.checked.在高版本中赋值时最好用prop,如果用attr就会出现赋值不成功的问题, 一般自定义属 ...
- Django之logging日志
简介 Django使用python自带的logging 作为日志打印工具.简单介绍下logging. logging 是线程安全的,其主要由4部分组成: Logger 用户使用的直接接口,将日志传递给 ...
- PHP之implode()方法
implode — 将一个一维数组的值转化为字符串 string implode ( string $glue , array $pieces ) string implode ( array $pi ...
- Java进行Base64的编码(Encode)与解码(Decode)
关于base64编码Encode和Decode编码的几种方式 Base64是一种能将任意Binary资料用64种字元组合成字串的方法,而这个Binary资料和字串资料彼此之间是可以互相转换的,十分方便 ...
- Android Studio -导入项目 gradle处理
如果导入 android studio 项目, 那么一定要注意 需要合适的gradle版本,具体方法为: 首先导入步骤: 打开android studio ==> File ==> New ...
- java 使用volatile实现线程数据的共享
java 使用volatile实现线程数据的共享 直接上代码看效果: public class VolatileTest extends Thread { private volatile boole ...
- mvc4扩展方法
制作扩展方法,方便网页中使用,下面做了两个例子 using System; using System.Collections.Generic; using System.Linq; using Sys ...
- 【bzoj3456】城市规划 容斥原理+NTT+多项式求逆
题目描述 求出n个点的简单(无重边无自环)无向连通图数目mod 1004535809(479 * 2 ^ 21 + 1). 输入 仅一行一个整数n(<=130000) 输出 仅一行一个整数, 为 ...
- MariaDB插入中文出现???情况
本来打算创建一个测试表进行一个简单的实验,发现创建完python_test表后插入数据后,select发现所有中文都变成问号了,这一看就是出现了乱码 MariaDB [lhc]> create ...