Codeforces 782E Underground Lab【极限思维+Dfs】

本文介绍了一种在限定时间内寻找出口的算法,该算法利用多个克隆体在复杂且相互连接的图中进行搜索,确保每个节点都被至少一个克隆体访问到。文章详细解释了算法的设计思路和实现细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 n, m, and k (1 ≤ n ≤ 2·105, n - 1 ≤ m ≤ 2·105, 1 ≤ 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.


题目大意:

一共有N个点,M条边,K个起点。

我们要求必须用K个起点来沿着图走,要求每个点最多能够走 个点。

保证题目有解,使得这N个点一定都至少被走到过一次。


思路:


极限思维,因为题目保证有解,那么考虑深搜加回溯最多可能走多少个点:2n.

那么明显有:K* >=2n;

那么我们只要从任意一点出发深搜一波,然后分段分配给这K个点去走就好了。

注意可能深搜次数比较少,不用K个点就能完成任务,那么剩余部分,输出1 1即可。


Ac代码:

#include<stdio.h>
#include<string.h>
#include<vector>
#include<queue>
using namespace std;
int ans[600000];
int vis[300000];
int duan[600000];
int tot;
vector<int >mp[300000];
void Dfs(int u)
{
    vis[u]=1;
    ans[tot++]=u;
    for(int i=0;i<mp[u].size();i++)
    {
        int v=mp[u][i];
        if(vis[v]==1)continue;
        else Dfs(v);
        ans[tot++]=u;
    }
}
int main()
{
    int n,m,k;
    while(~scanf("%d%d%d",&n,&m,&k))
    {
        memset(vis,0,sizeof(vis));
        for(int i=1;i<=n;i++)mp[i].clear();
        for(int i=0;i<m;i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            mp[x].push_back(y);
            mp[y].push_back(x);
        }
        int contz=0;
        tot=0;
        Dfs(1);
        int len=2*n/k;
        if(2*n%k!=0)len++;
        int i=0;
        int cnt=0;
        while(i<tot)
        {
            if(cnt<len)
            {
                duan[cnt++]=ans[i];
                i++;
            }
            else
            {
                contz++;
                printf("%d ",len);
                for(int j=0;j<cnt;j++)
                {
                    printf("%d ",duan[j]);
                }
                printf("\n");
                cnt=0;
                continue;
            }
        }
        if(cnt>0)
        {
            contz++;
            printf("%d ",cnt);
            for(int j=0;j<cnt;j++)
            {
                printf("%d ",duan[j]);
            }
            printf("\n");
        }
        if(contz<k)
        {
            while(contz<k)
            {
                printf("1 1\n");
                contz++;
            }
        }
    }
}








当前提供的引用内容并未提及关于Codeforces比赛M1的具体时间安排[^1]。然而,通常情况下,Codeforces的比赛时间会在其官方网站上提前公布,并提供基于不同时区的转换工具以便参赛者了解具体开赛时刻。 对于Codeforces上的赛事而言,如果一场名为M1的比赛被计划举行,则它的原始时间一般按照UTC(协调世界时)设定。为了得知该场比赛在UTC+8时区的确切开始时间,可以遵循以下逻辑: - 前往Codeforces官网并定位至对应比赛页面。 - 查看比赛所标注的标准UTC起始时间。 - 将此标准时间加上8小时来获取对应的北京时间(即UTC+8)。 由于目前缺乏具体的官方公告链接或者确切日期作为依据,无法直接给出Codeforces M1比赛于UTC+8下的实际发生时段。建议定期访问Codeforces平台查看最新动态更新以及确认最终版程表信息。 ```python from datetime import timedelta, datetime def convert_utc_to_bj(utc_time_str): utc_format = "%Y-%m-%dT%H:%M:%SZ" bj_offset = timedelta(hours=8) try: # 解析UTC时间为datetime对象 utc_datetime = datetime.strptime(utc_time_str, utc_format) # 转换为北京时区时间 beijing_time = utc_datetime + bj_offset return beijing_time.strftime("%Y-%m-%d %H:%M:%S") except ValueError as e: return f"错误:{e}" # 示例输入假设某场Codeforces比赛定于特定UTC时间 example_utc_start = "2024-12-05T17:35:00Z" converted_time = convert_utc_to_bj(example_utc_start) print(f"Codeforces比赛在北京时间下将是:{converted_time}") ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值