1 条题解

  • 0
    @ 2026-11-2 15:26:39

    英国菜单(British Menu) 题解

    思路分析

    按题目要求使用对应数据结构与算法模拟/递推求解,注意数据范围边界与时间复杂度。

    参考代码

    #include <iostream>
    #include <vector>
    #include <algorithm>
    using namespace std;
    
    int main() {
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
        int n, m;
        if (!(cin >> n >> m)) return 0;
        vector<vector<int>> adj(n + 1);
        for (int i = 0; i < m; ++i) {
            int u, v;
            cin >> u >> v;
            adj[u].push_back(v);
        }
        vector<int> dfn(n + 1, 0), low(n + 1, 0), in_stk(n + 1, 0), scc(n + 1, 0);
        vector<int> stk, call_u, call_idx;
        vector<vector<int>> scc_nodes(1);
        int timer = 0, scc_cnt = 0;
        for (int i = 1; i <= n; ++i) {
            if (!dfn[i]) {
                call_u.push_back(i);
                call_idx.push_back(0);
                dfn[i] = low[i] = ++timer;
                stk.push_back(i);
                in_stk[i] = 1;
                while (!call_u.empty()) {
                    int u = call_u.back();
                    int& idx = call_idx.back();
                    if (idx < (int)adj[u].size()) {
                        int v = adj[u][idx++];
                        if (!dfn[v]) {
                            dfn[v] = low[v] = ++timer;
                            stk.push_back(v);
                            in_stk[v] = 1;
                            call_u.push_back(v);
                            call_idx.push_back(0);
                        } else if (in_stk[v]) {
                            low[u] = min(low[u], dfn[v]);
                        }
                    } else {
                        if (low[u] == dfn[u]) {
                            ++scc_cnt;
                            vector<int> comp;
                            while (true) {
                                int x = stk.back();
                                stk.pop_back();
                                in_stk[x] = 0;
                                scc[x] = scc_cnt;
                                comp.push_back(x);
                                if (x == u) break;
                            }
                            scc_nodes.push_back(comp);
                        }
                        call_u.pop_back();
                        call_idx.pop_back();
                        if (!call_u.empty()) {
                            low[call_u.back()] = min(low[call_u.back()], low[u]);
                        }
                    }
                }
            }
        }
        vector<int> dp(n + 1, 1), best_out(n + 1, 0), vis(n + 1, 0);
        int ans = 1;
        for (int c = 1; c <= scc_cnt; ++c) {
            const auto& comp = scc_nodes[c];
            for (int u : comp) {
                int bo = 0;
                for (int v : adj[u]) {
                    if (scc[v] != c) bo = max(bo, dp[v]);
                }
                best_out[u] = bo;
            }
            auto dfs_comp = [&](auto& self, int u, int len) -> int {
                int res = len + best_out[u];
                vis[u] = 1;
                for (int v : adj[u]) {
                    if (scc[v] == c && !vis[v]) {
                        res = max(res, self(self, v, len + 1));
                    }
                }
                vis[u] = 0;
                return res;
            };
            for (int u : comp) {
                dp[u] = dfs_comp(dfs_comp, u, 1);
                ans = max(ans, dp[u]);
            }
        }
        cout << ans << "\n";
        return 0;
    }
    
    • 1

    信息

    ID
    8819
    时间
    1000ms
    内存
    256MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者