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

224 篇文章 2 订阅

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++;
            }
        }
    }
}








  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值