题解:P17535 [JAG 2026 Summer Camp #1] Sliding Puzzle on Tree

· · 题解

看了一圈代码发现自己写的和别人的不太一样,于是分享一下自己赛时想的的神秘做法。

讨论情况

拿到题目先进行神秘分类讨论找性质,将一节点连接若干子树的情况先简化为一子树的树根直接连接若干叶子节点,分别考虑树根上有石子或无石子,叶子节点上均有石子或均无石子或有无石子混合的情况之后,我们可以发现实际上树根上有无石子并不重要,重点是借助树根将有石子叶子节点上的石子转移到无石子叶子节点上来满足题目要求。

考虑忽略交换次序

我们考虑将叶子节点上的石子统一收回到根节点上进行分配,先填补无石子叶子节点,再记录剩余的石子数(可以为负,先欠着再向上借石子)用于向上传递,于是这样我们就可以利用递归计算剩余石子数来达成我们刚刚讨论情况时的简化。

建图求操作顺序

接下来我们考虑如何求出操作顺序,我们在递归时按照子节点欠石子还是有多余石子来判断石子传递的方向,连边建图并记录入度,于是就得到了一张 DAG。最后从所有出度为零的有石子节点出发运送石子,途中遇到有石子的点阻塞时就先传递该节点上的石子,传递完再回来继续传递原来的石子,注意要把走过的边删掉。

void dfs(int x, int fa)
{
    int need = 0;
    for (auto [to, ind] : g[x]) if (to != fa)
    {
        dfs(to, x);
        if (cnt[to] > 0) ++cnt[x], G[to].emplace_back(x, ind), ++du[x];
        else ++need, G[x].emplace_back(to, ind), ++du[to];
    }
    cnt[x] -= need;
}

void dfs2(int x)
{
    for (int& i = cur[x]; i < G[x].size(); ++i){
        int to = G[x][i].first, ind = G[x][i].second;
        if (now[to]) now[to] = 0, dfs2(to);
        if (now[to]) continue;
        ans.push_back(ind);
        ++i;
        dfs2(to);
        return;
    }
    now[x] = 1;
}

判断可行性

最简单的方式就是求完操作顺序后直接判断操作数是否为恰好为总边数,不过我们是否有其他方法来判断呢?考虑前面递归分配石子并建图时的性质。当回收分配完石子后,如果欠了一个石子或无剩余石子,显然可以从父节点上借一个石子,如果多出一个石子或两颗石子则可以直接传给父亲一个。由于一条边最多传递一颗石子,因此其余情况下均不可行,注意特判树根。这样我们就实现了在建图时直接判断可行性。

bool dfs(int x, int fa)
{
    int need = 0;
    for (auto [to, ind] : g[x]) if (to != fa)
    {
        if (!dfs(to, x)) return false;
        if (cnt[to] > 0) ++cnt[x], G[to].emplace_back(x, ind), ++du[x];
        else ++need, G[x].emplace_back(to, ind), ++du[to];
    }
    cnt[x] -= need;
    if (cnt[x] < -1 || cnt[x] > 2) return false;
    if (x == 1 && (cnt[x] != 1 && cnt[x] != 0)) return false;
    return true;
}

复杂度分析

显然是线性的。

AC Code

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

vector<int> ans;
vector<pair<int, int> > g[200100], G[200100];
int cnt[200100], du[200100], now[200100], cur[200100], n, u, v;
queue<int> q;

bool dfs(int x, int fa)
{
    int need = 0;
    for (auto [to, _] : g[x]) if (to != fa)
    {
        if (!dfs(to, x)) return false;
        if (cnt[to] > 0) ++cnt[x], G[to].emplace_back(x, _), ++du[x];
        else ++need, G[x].emplace_back(to, _), ++du[to];
    }
    cnt[x] -= need;
    if (cnt[x] < -1 || cnt[x] > 2) return false;
    if (x == 1 && (cnt[x] != 1 && cnt[x] != 0)) return false;
    return true;
}

void dfss(int x)
{
    for (int& i = cur[x]; i < G[x].size(); ++i){
        int to = G[x][i].first, ind = G[x][i].second;
        if (now[to]) now[to] = 0, dfss(to);
        if (now[to]) continue;
        ans.push_back(ind);
        ++i;
        dfss(to);
        return;
    }
    now[x] = 1;
}

int main()
{
    int T;
    cin >> T;
    while (T--)
    {
        cin >> n;
        for (int i = 1; i < n; ++i)
        {
            cin >> u >> v;
            g[u].emplace_back(v, i);
            g[v].emplace_back(u, i);
        }
        for (int i = 1; i <= n; ++i) cin >> cnt[i], now[i] = cnt[i];
        if (!dfs(1, 0)) cout << "No" << endl;
        else
        {
            for (int i = 1; i <= n; ++i) if (now[i] && du[i] == 0) q.push(i);
            while (!q.empty())
            {
                now[q.front()] = 0;
                dfss(q.front());
                q.pop();
            }
            cout << "Yes" << endl;
            for (auto to : ans) cout << to << " ";
            cout << endl;
        }
        for (int i = 1; i <= n; ++i) g[i].clear(), G[i].clear(), du[i] = 0, cur[i] = 0;
        q = queue<int>();
        ans.clear();
    }
    return 0;
}