其他分享
首页 > 其他分享> > P1656 炸铁路

P1656 炸铁路

作者:互联网

题目传送门

知识点

#include <bits/stdc++.h>

using namespace std;

const int N = 5010, M = 20010;
typedef pair<int, int> PII;
int n, m;
int h[N], e[M], ne[M], idx;
int dfn[N], low[N], timestamp;
int stk[N], top;
int id[N], dcc_cnt;
bool is_bridge[M];
int d[N];
vector<PII> res;
//排序函数
bool cmp(const PII &a, const PII &b) {
    if (a.first == b.first) return a.second < b.second;
    return a.first < b.first;
}

void add(int a, int b) {
    e[idx] = b, ne[idx] = h[a], h[a] = idx++;
}
void tarjan(int u, int from) {
    dfn[u] = low[u] = ++timestamp;
    stk[++top] = u;
    for (int i = h[u]; ~i; i = ne[i]) {
        int j = e[i];
        if (!dfn[j]) {
            tarjan(j, i);
            low[u] = min(low[u], low[j]);
            if (dfn[u] < low[j]) {
                res.push_back({min(u, j), max(u, j)});
                is_bridge[i] = is_bridge[i ^ 1] = true;
            }
        } else if (i != (from ^ 1))
            low[u] = min(low[u], dfn[j]);
    }
    if (dfn[u] == low[u]) {
        ++dcc_cnt;
        int y;
        do {
            y = stk[top--];
            id[y] = dcc_cnt;
        } while (y != u);
    }
}
int main() {
    cin >> n >> m;
    memset(h, -1, sizeof h);
    for (int i = 0; i < m; i++) {
        int a, b;
        cin >> a >> b;
        add(a, b), add(b, a);
    }

    for (int i = 1; i <= n; i++)
        if (!dfn[i]) tarjan(i, -1);

    sort(res.begin(), res.end(), cmp);
    for (int i = 0; i < res.size(); i++)
        cout << res[i].first << " " << res[i].second << endl;
    return 0;
}

标签:PII,idx,int,P1656,铁路,++,dfn,low
来源: https://www.cnblogs.com/littlehb/p/16093332.html