跳到正文

题解

题解:P12954 [GCJ Farewell Round #2] Railroad Maintenance

zhongzhong517约 3 分钟
原文
文章信息与版本

文章信息

发布
2026年1月6日
收录
2026年1月6日
更新
2026年11月11日
篇幅
约 3 分钟
题目
P12954

版本

  1. v1当前

题目大意

给 NN 个点,LL 条铁路线,定义关键线路为移除它后会导致至少一对车站之间的行程变得不可能的线路,求 LL 条线路中关键线路数量。

思路

很明显,直接求关键线路不好办,所以将第 ii 条线路变成一个编号为 N+iN+i 的虚点,向线路上所有点建双向边,跑一遍 Tarjan,求出 N+1∼N+L−1N+1\sim N+L-1 割点数量即可。

代码

C++
#include <bits/stdc++.h>
using namespace std;
int t, n, l, k, s, tot, dfn[200005], low[200005], cut[200005];
vector<int> g[200005];
void tarjan(int x, int root){
    dfn[x] = low[x] = ++tot;
    int son = 0;
    for(auto i : g[x]){
        if(!dfn[i]){
            son++;
            tarjan(i, x);
            low[x] = min(low[x], low[i]);
            if(x != root && dfn[x] <= low[i]) cut[x] = 1;
        }
        else low[x] = min(low[x], dfn[i]);
    }
}
int main(){
    cin >> t;
    for(int _ = 1; _ <= t; _++){
        memset(dfn, 0, sizeof dfn);
        memset(low, 0, sizeof low);
        memset(cut, 0, sizeof cut);
        tot = 0;
        for(int i = 0; i <= 2e5; i++) g[i].clear();
        cin >> n >> l;
        for(int i = 1; i <= l; i++){
            cin >> k;
            for(int j = 1; j <= k; j++){
                cin >> s;
                g[n + i].push_back(s);
                g[s].push_back(n + i);
            }
        }
        for(int i = 1; i <= n + l; i++) tarjan(i, i);
        int cnt = 0;
        for(int i = n + 1; i <= n + l; i++) if(cut[i]) cnt++;
        cout << "Case #" << _ << ": " << cnt << "\n";
    }
    return 0;
}
原文