[CF780E] Underground Lab - 构造

[CF780E] Underground Lab - 构造

Description

给出一个图,有 n 个点, m 条边,k 个人,每个人至多只能走 \(\lceil \frac{2n}{k} \rceil\) 步 ,要求每个点都被走到过,输出可行的方案即输出每个人所走的步数和所走点。

Solution

由于序列总长不小于 2n,考虑直接用 DFS 遍历过程形成的点列,即 DFS 一遍然后拆成 k 段输出就可以了

#include <bits/stdc++.h>
using namespace std;

#define int long long

int n, m, k, l;
const int N = 1000005;

vector<int> g[N];
vector<int> seq;

int vis[N];

void dfs(int p)
{
    vis[p] = 1;
    seq.push_back(p);
    for (int q : g[p])
    {
        if (vis[q])
            continue;
        dfs(q);
        seq.push_back(p);
    }
}

signed main()
{
    ios::sync_with_stdio(false);

    cin >> n >> m >> k;
    l = (2 * n + k - 1) / k;

    for (int i = 1; i <= m; i++)
    {
        int u, v;
        cin >> u >> v;
        g[u].push_back(v);
        g[v].push_back(u);
    }

    dfs(1);

    int pos = 0;
    for (int i = 0; i < k; i++)
    {
        if (pos >= seq.size())
        {
            cout << 1 << " " << 1 << endl;
        }
        else
        {
            vector<int> ans;
            for (int j = pos; j < seq.size() && j < pos + l; j++)
                ans.push_back(seq[j]);
            cout << ans.size() << " ";
            for (int j : ans)
                cout << j << " ";
            cout << endl;
            pos += l;
        }
    }
}
上一篇:python3 力扣 刷题 随笔


下一篇:【LeetCode每天一题】Candy(分糖果)