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

· · 题解

定义:

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

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

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

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

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

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

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

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

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

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

#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;
}