跳到正文

题解

题解:P12077 [OOI 2025] Strong Connectivity Strikes Back

徐晨轩✅约 10 分钟
原文
文章信息与版本

文章信息

发布
2026年5月24日
收录
2026年10月15日
更新
2026年11月10日
篇幅
约 10 分钟
获赞
1
题目
P12077

版本

  1. v1当前

定义:

  • 图 GG 上的一个划分 C\mathscr{C} 是一个集簇,它们两两无交,且并为 VGV_G。
  • G/CG/\mathscr{C} 表示将 C\mathscr{C} 中每个集合中的点缩为一个大点并删除重边后形成的图。
  • EG(e)E_{G}(e) 表示某个缩点图 G/CG/\mathscr{C} 中边 ee 在原图 GG 中对应的边集。
  • EG(G/C)E_{G}(G/\mathscr{C}) 表示 ⋃e∈EG/CEG(e)\bigcup\limits_{e \in E_{G/\mathscr{C}}} E_{G}(e)。

求出原图 GG 的强连通划分 C\mathscr{C}。考虑 G/CG/\mathscr{C} 中的边 u→vu \to v,若存在另外一个 u⇝vu \rightsquigarrow v 的路径,则 EG(u→v)E_{G}(u \to v) 都可以删向,否则 EG(u→v)E_{G}(u \to v) 中要保留一条边不删向。到此为止考虑了所有 EG(G/C)E_{G}(G/\mathscr{C}) 中的边。容易证明 EG(G/C)E_{G}(G/\mathscr{C}) 和 EG\EG(G/C)E_G\backslash E_{G}(G/\mathscr{C}) 互不影响。

接下来考虑剩下的边,也就是 GG 是一个强连通图的情况。对于 e∈EGe \in E_G,记 C(e)\mathscr{C}(e) 表示 (VG,EG\{e})(V_G,E_G \backslash \{e\}) 的强连通划分,S(e)=EG(G/C(e))∪{e}\mathscr{S}(e)=E_{G}(G/\mathscr{C}(e)) \cup \{e\}。

若 ∣C(e)∣=1|\mathscr{C}(e)|=1,即反向 ee 后图仍强连通,那么显然不能删去 ee 的方向。接下来考虑什么样的边集 AA 不能同时被删向(∣A∣≥2|A| \ge 2)。这等价于存在 B⊆AB \subseteq A 满足反向 BB 中所有边之后图仍强连通。

分析一下 S(e)\mathscr{S}(e) 的性质。容易发现 ∀f∈S(e):S(f)⊆S(e)\forall f \in \mathscr{S}(e): \mathscr{S}(f) \subseteq \mathscr{S}(e)。如果 ∀f∈S(e):S(f)=S(e)\forall f \in \mathscr{S}(e): \mathscr{S}(f)=\mathscr{S}(e),那么 G/C(e)G/\mathscr{C}(e) 是一个环,且 ∣EG/C(e)∣=∣S(e)∣|E_{G/\mathscr{C}(e)}|=|\mathscr{S}(e)|。称这样的 S(e)\mathscr{S}(e) 是必要的。

再分析 BB 的性质。找到任意一个 e=u→ve=u\to v 满足 e∈Be \in B 且 ∣S(e)∣|\mathscr{S}(e)| 最小,则 S(e)\mathscr{S}(e) 是必要的。

证明:根据 BB 的定义,BB 中除了 u→vu \to v 之外,肯定还要包含一个 v⇝uv \rightsquigarrow u 的路径,这样反转之后才可能强连通(注意这里路径是在 G/C(e)G/\mathscr{C}(e) 内讨论的)。若 S(e)\mathscr{S}(e) 不是必要的,则说明存在多条 v⇝uv \rightsquigarrow u 的路径。设除了 BB 中 v⇝uv \rightsquigarrow u 路径 PP 之外,还有 QQ 也是一条 v⇝uv \rightsquigarrow u 的路径。此时,必然存在 f∈Bf \in B(ff 可以是 P\QP \backslash Q 中的任何一条边)满足 S(f)⊂S(e)\mathscr{S}(f) \subset \mathscr{S}(e)(因为 C(f)\mathscr{C}(f) 中 u,vu,v 被缩起来了),与 ∣S(e)∣|\mathscr{S}(e)| 的最小性矛盾。

由于 G/C(e)G/\mathscr{C}(e) 是一个环,故 {e}∪P=EG/C(e)\{e\} \cup P = E_{G/\mathscr{C}(e)}。那么 S(e)⊆B⊆A\mathscr{S}(e) \subseteq B \subseteq A。于是,我们得出了:若 AA 不能同时被删向,则 AA 包含某个必要的 S(e)\mathscr{S}(e)。又根据必要的定义,显然有:若 AA 包含某个必要的 S(e)\mathscr{S}(e),则 AA 不能同时被删向(否则将 S(e)\mathscr{S}(e) 全部反向仍强连通)。

综上,我们得出:AA 可以被删向   ⟺  \iff AA 不包含任何必要的 S(e)\mathscr{S}(e)。可以证明:对于所有必要的 S(e)\mathscr{S}(e) 和 S(f)\mathscr{S}(f)(S(e)≠S(f)\mathscr{S}(e) \ne \mathscr{S}(f)),S(e)∩S(f)=∅\mathscr{S}(e) \cap \mathscr{S}(f) = \varnothing。

证明:若 ∃g∈S(e)∩S(f)\exist g \in \mathscr{S}(e) \cap \mathscr{S}(f),则 S(g)=S(e),S(g)=S(f)\mathscr{S}(g)=\mathscr{S}(e),\mathscr{S}(g)=\mathscr{S}(f),矛盾。

那么,我们只要找出所有必要的 S(e)\mathscr{S}(e),并从每个 S(e)\mathscr{S}(e) 中各删去任意一条边即可。方案数容易计算。

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

using ll = long long;
const int N = 2005, P = 1e9 + 7;

int n, m, g, ans1, ans2 = 1, a[N], b[N], samescc[N];
vector<int> G[N], S[N];
unordered_map<int, int> mp[N];

namespace tarjan {

int tot, top, dfn_clock, bel[N], dfn[N], low[N], instk[N], stk[N];

void tarjan(int u) {
    dfn[u] = low[u] = ++dfn_clock;
    instk[u] = 1;
    stk[++top] = u;
    for (int v : G[u]) {
        if (!dfn[v]) tarjan(v), low[u] = min(low[u], low[v]);
        else if (instk[v]) low[u] = min(low[u], dfn[v]);
    }
    if (low[u] == dfn[u]) {
        int t;
        tot++;
        do {
            t = stk[top--];
            instk[t] = 0;
            bel[t] = tot;
        } while (t != u);
    }
}

void run() {
    tot = dfn_clock = 0;
    for (int i = 1; i <= n; i++) dfn[i] = 0;
    for (int i = 1; i <= n; i++) if (!dfn[i]) tarjan(i);
    for (int i = 1; i <= n; i++) G[i].clear();
}

}

using tarjan::bel, tarjan::tot;

int main() {
    cin >> n >> m >> g;
    for (int i = 1; i <= m; i++) {
        cin >> a[i] >> b[i];
        G[a[i]].emplace_back(b[i]);
    }
    tarjan::run();
    for (int i = 1; i <= m; i++)
        if (bel[a[i]] == bel[b[i]])
            samescc[i] = 1;
    // SCC Part
    for (int i = 1; i <= m; i++) if (samescc[i]) {
        for (int j = 1; j <= m; j++)
            if (i != j)
                G[a[j]].emplace_back(b[j]);
        tarjan::run();
        if (bel[a[i]] != bel[b[i]]) {
            ans1++;
            S[i].emplace_back(i);
            for (int j = 1; j <= m; j++)
                if (j != i && samescc[j] && bel[a[j]] != bel[b[j]])
                    S[i].emplace_back(j);
            sort(S[i].begin(), S[i].end());
        }
    }
    for (int e = 1; e <= m; e++) if (samescc[e]) {
        if (S[e].empty()) continue;
        bool flag = 1;
        for (int f : S[e])
            if (f < e || S[f] != S[e])
                flag = 0;
        if (flag) {
            // S[e] is necessary
            ans1--;
            ans2 = (ll)ans2 * S[e].size() % P;
        }
    }
    for (int i = 1; i <= m; i++)
        G[a[i]].emplace_back(b[i]);
    tarjan::run();
    // DAG Part
    for (int i = 1; i <= m; i++)
        if (bel[a[i]] != bel[b[i]])
            mp[bel[a[i]]][bel[b[i]]]++;
    for (int x = 1; x <= tot; x++) {
        for (auto [y, cnt] : mp[x]) {
            vector<int> vis(n + 1);
            function<void(int)> dfs = [&](int u) {
                vis[u] = 1;
                for (auto [v, _] : mp[u])
                    if (!vis[v] && !(u == x && v == y))
                        dfs(v);
            };
            dfs(x);
            if (vis[y]) {
                ans1 += cnt;
            } else {
                ans1 += cnt - 1;
                ans2 = (ll)ans2 * cnt % P;
            }
        }
    }
    cout << ans1 << endl << ans2 << endl;
    return 0;
}
原文
存档评论2 条抓取于