Caocao's Bridges hdu-4738

题面

传送门

题解

缩点, 坑点在最小边为0是, 你至少用1个人去炸桥

代码

int n, m, _, k;
int h[N], ne[M << 1], to[M << 1], co[M << 1], tot;
int dfn[N], low[N], df, ans;

void add(int u, int v, int c) {
    ne[++tot] = h[u]; to[h[u] = tot] = v; co[tot] = c;
}

void tarjan(int x, int e) {
    dfn[x] = low[x] = ++df;
    for (int i = h[x]; i; i = ne[i]) {
        int y = to[i];
        if (!dfn[y]) { 
            tarjan(y, i); umin(low[x], low[y]);
            if (dfn[x] < low[y]) umin(ans, co[i]);
        }
        else if (i != (e ^ 1)) umin(low[x], dfn[y]);
    }
}

int main() {
    IOS; while (cin >> n >> m, n || m) {
        memset(dfn, 0, sizeof dfn); df = 0;
        memset(h, 0, sizeof h); tot = 1;
        rep (i, 1, m) {
            int u, v, c; cin >> u >> v >> c;
            add(u, v, c); add(v, u, c);
        } ans = 1e9;
        tarjan(1, -1); umax(ans, 1);
        rep (i, 2, n) if (!dfn[i]) { ans = 0; break; }
        cout << (ans == 1e9 ? -1 : ans) << '\n';
    }
    return 0;
}
上一篇:POJ 2955


下一篇:蓝桥杯算法训练 矩阵乘法