CF - 782E. Underground Lab - 思维+dfs

25 篇文章 0 订阅

1.题目描述:

E. Underground Lab
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

The evil Bumbershoot corporation produces clones for gruesome experiments in a vast underground lab. On one occasion, the corp cloned a boy Andryusha who was smarter than his comrades. Immediately Andryusha understood that something fishy was going on there. He rallied fellow clones to go on a feud against the evil corp, and they set out to find an exit from the lab. The corp had to reduce to destroy the lab complex.

The lab can be pictured as a connected graph with n vertices and m edges. k clones of Andryusha start looking for an exit in some of the vertices. Each clone can traverse any edge once per second. Any number of clones are allowed to be at any vertex simultaneously. Each clone is allowed to stop looking at any time moment, but he must look at his starting vertex at least. The exit can be located at any vertex of the lab, hence each vertex must be visited by at least one clone.

Each clone can visit at most  vertices before the lab explodes.

Your task is to choose starting vertices and searching routes for the clones. Each route can have at most  vertices.

Input

The first line contains three integers nm, and k (1 ≤ n ≤ 2·105n - 1 ≤ m ≤ 2·1051 ≤ k ≤ n) — the number of vertices and edges in the lab, and the number of clones.

Each of the next m lines contains two integers xi and yi (1 ≤ xi, yi ≤ n) — indices of vertices connected by the respective edge. The graph is allowed to have self-loops and multiple edges.

The graph is guaranteed to be connected.

Output

You should print k lines. i-th of these lines must start with an integer ci () — the number of vertices visited by i-th clone, followed by ci integers — indices of vertices visited by this clone in the order of visiting. You have to print each vertex every time it is visited, regardless if it was visited earlier or not.

It is guaranteed that a valid answer exists.

Examples
input
3 2 1
2 1
3 1
output
3 2 1 3
input
5 4 2
1 2
1 3
1 4
1 5
output
3 2 1 3
3 4 1 5
Note

In the first sample case there is only one clone who may visit vertices in order (2, 1, 3), which fits the constraint of 6 vertices per clone.

In the second sample case the two clones can visited vertices in order (2, 1, 3) and (4, 1, 5), which fits the constraint of 5 vertices per clone.


2.题意概述:

给你一个无向图n个顶点m条边,k个克隆人,他们最多行走(2 * n) / k向上取整个步数,要你安排具体每个机器人行走的方案,题目保证有解

3.解题思路:

考虑DFS+回溯的话,发现正好最多走2*n条边,而k个机器人 2nk×k2n 可以总共遍历点大于 2n。那么直接爆搜一遍记录路径就行。然后注意分配当k特别大时候,剩余的机器人只要停留在原地(即1 1)就行。

4.AC代码:

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define maxn 200100
#define N 1111
#define eps 1e-6
#define pi acos(-1.0)
#define e exp(1.0)
using namespace std;
const int mod = 1e9 + 7;
typedef long long ll;
typedef unsigned long long ull;
vector<int> G[maxn];
bool vis[maxn];
int path[maxn * 2], cnt;
void dfs(int from)
{
	vis[from] = 1;
	path[++cnt] = from;
	for (int i = 0; i < (int)G[from].size(); i++)
	{
		int to = G[from][i];
		if (!vis[to])
		{
			dfs(to);
			path[++cnt] = from;	//相当于回溯
		}
	}
}
int main()
{
#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
	long _begin_time = clock();
#endif
	int n, m, k;
	while (~scanf("%d%d%d", &n, &m, &k))
	{
		cnt = 0;
		memset(vis, 0, sizeof(vis));
		for (int i = 1; i <= n; i++)
			G[i].clear();
		for (int i = 0; i < m; i++)
		{
			int u, v;
			scanf("%d%d", &u, &v);
			G[u].push_back(v);
			G[v].push_back(u);
		}
		dfs(1);
		int maxC = (2 * n + k - 1) / k;
		for (int i = 0; i < k; i++)
		{
			int c = min(maxC, cnt);
			if (c)
			{
				printf("%d", c);
				for (int j = 0; j < c && cnt; j++)
					printf(" %d", path[cnt--]);
				puts("");
			}
			else
				puts("1 1");
		}
	}

#ifndef ONLINE_JUDGE
	long _end_time = clock();
	printf("time = %ld ms.", _end_time - _begin_time);
#endif
	return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 以下是一个简单的Python代码,可以爬取https://samples.vx-underground.org/samples/Families/网址下的内容并将其下载到本地: ```python import requests url = "https://samples.vx-underground.org/samples/Families/" response = requests.get(url) if response.status_code == 200: contents = response.content with open("downloaded_content.zip", "wb") as f: f.write(contents) else: print("Failed to download content.") ``` 这个代码使用requests库向指定的URL发出GET请求,如果返回状态码为200,说明请求成功。然后,将请求返回的内容保存在一个变量中,再通过Python内置的`open()`函数将内容写入一个文件中,文件名为`downloaded_content.zip`,并指定以二进制模式写入。如果请求失败,则打印一条错误信息。 ### 回答2: 以下是一个用Python爬取网页内容并下载的示例代码: ```python import requests # 定义要爬取的网址 url = 'https://samples.vx-underground.org/samples/Families/' # 发起请求获取网页内容 response = requests.get(url) # 获取响应的二进制内容 content = response.content # 定义要保存的文件路径 file_path = 'downloaded_file.mp3' # 将响应的二进制内容保存到文件中 with open(file_path, 'wb') as file: file.write(content) print('文件下载完成!') ``` 以上代码使用了Python中的requests库来发送HTTP请求,并将网页响应的二进制内容保存到指定文件中。在示例中,我们指定了要下载的网址为'https://samples.vx-underground.org/samples/Families/',文件将被保存为'downloaded_file.mp3'。你可以根据实际需要修改这些值来适应不同的网址和文件路径。 ### 回答3: 以下是一个使用Python爬取https://samples.vx-underground.org/samples/Families/网站内容的示例代码: ```python import requests url = 'https://samples.vx-underground.org/samples/Families/' response = requests.get(url) if response.status_code == 200: content = response.content # 这里可以对获取到的内容进行进一步的处理,比如保存到文件或者解析网页等 print(content) else: print('请求失败') ``` 首先,我们使用`requests`库来发送GET请求获取网页的内容。将目标网址赋值给变量`url`,然后使用`requests.get(url)`发送请求。接着,通过判断响应的状态码`response.status_code`是否等于200来确定请求是否成功。 如果请求成功,就可以通过`response.content`获取网页的原始内容。你可以根据自己的需求对这个内容进行进一步的处理,比如保存到本地文件、解析网页等。 如果请求失败,会打印出"请求失败"的提示信息。你可以根据实际情况进行错误处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值