跳到正文

题解

P2416 泡芙

key_l约 8 分钟
原文
文章信息与版本

文章信息

发布
2026年8月13日
收录
2026年8月13日
更新
2026年11月14日
篇幅
约 8 分钟

版本

  1. v1当前

P2416 泡芙

前置算法:边双缩点树

删除所有桥后,每个连通块是一个边双连通分量。把分量缩成点、把桥保留下来,得到一棵树。原图中的不重复边路线投影到桥树后,只能沿两端之间的唯一路径移动。

思路

先标记每条边是否有泡芙,并用 Tarjan 求桥。

  • 非桥上的泡芙记到它所在的边双分量上;进入该分量后,可以在不重复边的前提下绕过这条边并继续前往出口。
  • 桥上的泡芙记到桥树对应的树边上。桥不能走过去再走回来,所以只有位于起终点桥树路径上的桥才可能经过。

于是每次询问只需判断桥树路径上是否存在“带泡芙的分量”或“带泡芙的桥”。给每个分量点和它到父亲的桥赋 0/10/1 权值,预处理根到点的权值前缀和。路径权值和大于 00 就输出 YES。

正确性说明

桥树路径以外的分量只能通过同一座桥进出,必然重复该桥,不合法;所以它们不能贡献泡芙。路径上的桥可以依次经过。对于路径上的边双分量,任意两条接入边之间存在不重复边路线,并可把某条非桥边纳入路线,因此分量内部只需记录是否存在泡芙边。故路径权值和是否为正与是否存在合法采集路线完全等价。

复杂度

预处理 O((N+M)+Nlog⁡N)O((N+M)+N\log N),每次询问 O(log⁡N)O(\log N),空间 O(Nlog⁡N+M)O(N\log N+M)。

参考代码

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

struct Edge { int to, id; };

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n, m;
    cin >> n >> m;
    vector<vector<Edge>> graph(n + 1);
    vector<int> eu(m), ev(m), hasPuff(m);
    for (int id = 0; id < m; ++id) {
        cin >> eu[id] >> ev[id] >> hasPuff[id];
        graph[eu[id]].push_back({ev[id], id});
        graph[ev[id]].push_back({eu[id], id});
    }

    vector<int> dfn(n + 1), low(n + 1);
    vector<char> bridge(m, false);
    int timer = 0;
    auto tarjan = [&](auto &&self, int u, int parentEdge) -> void {
        dfn[u] = low[u] = ++timer;
        for (auto e : graph[u]) {
            if (!dfn[e.to]) {
                self(self, e.to, e.id);
                low[u] = min(low[u], low[e.to]);
                if (low[e.to] > dfn[u]) bridge[e.id] = true;
            } else if (e.id != parentEdge) {
                low[u] = min(low[u], dfn[e.to]);
            }
        }
    };
    tarjan(tarjan, 1, -1);

    vector<int> component(n + 1);
    int componentCount = 0;
    for (int s = 1; s <= n; ++s) if (!component[s]) {
        ++componentCount;
        stack<int> st;
        st.push(s); component[s] = componentCount;
        while (!st.empty()) {
            int u = st.top(); st.pop();
            for (auto e : graph[u]) {
                if (bridge[e.id] || component[e.to]) continue;
                component[e.to] = componentCount;
                st.push(e.to);
            }
        }
    }

    vector<int> nodeWeight(componentCount + 1);
    vector<vector<pair<int,int>>> tree(componentCount + 1);
    for (int id = 0; id < m; ++id) {
        int a = component[eu[id]], b = component[ev[id]];
        if (a == b) nodeWeight[a] |= hasPuff[id];
        else {
            tree[a].push_back({b, hasPuff[id]});
            tree[b].push_back({a, hasPuff[id]});
        }
    }

    int LOG = 1;
    while ((1 << LOG) <= componentCount) ++LOG;
    vector<vector<int>> up(LOG, vector<int>(componentCount + 1));
    vector<int> depth(componentCount + 1, -1), prefix(componentCount + 1);
    queue<int> que;
    depth[1] = 0; prefix[1] = nodeWeight[1]; que.push(1);
    while (!que.empty()) {
        int u = que.front(); que.pop();
        for (auto [v, edgeWeight] : tree[u]) if (depth[v] == -1) {
            depth[v] = depth[u] + 1;
            up[0][v] = u;
            prefix[v] = prefix[u] + edgeWeight + nodeWeight[v];
            que.push(v);
        }
    }
    for (int k = 1; k < LOG; ++k)
        for (int v = 1; v <= componentCount; ++v)
            up[k][v] = up[k - 1][up[k - 1][v]];

    auto lca = [&](int a, int b) {
        if (depth[a] < depth[b]) swap(a, b);
        int diff = depth[a] - depth[b];
        for (int k = 0; k < LOG; ++k) if (diff >> k & 1) a = up[k][a];
        if (a == b) return a;
        for (int k = LOG - 1; k >= 0; --k) if (up[k][a] != up[k][b]) {
            a = up[k][a]; b = up[k][b];
        }
        return up[0][a];
    };

    int q;
    cin >> q;
    while (q--) {
        int s, t;
        cin >> s >> t;
        s = component[s]; t = component[t];
        int x = lca(s, t);
        int pathSum = prefix[s] + prefix[t] - 2 * prefix[x] + nodeWeight[x];
        cout << (pathSum > 0 ? "YES" : "NO") << '\n';
    }
    return 0;
}

易错点

  • 分量点权在路径和中只应计算一次,LCA 的点权要补回。
  • 桥上的泡芙属于树边,非桥上的泡芙属于分量点。
  • 数据规模较大;若评测环境栈空间较小,应把递归 Tarjan 改为显式栈版本。
原文