백준 #5651 완전 중요한 간선


BOJ #5651 - 완전 중요한 간선

https://www.acmicpc.net/problem/5651

# 문제 분류

최대 유량

풀이 접근 방법


crucial path인 경우에는 max flow일 때 flow를 통해 다른 간선을 이용, 서로 만나는 것이 불가능함.


#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <queue>
#include <utility>
#include <vector>

#define mp make_pair
#define pb push_back
#define X first
#define Y second
#define fup(i, a, b, c) for (int(i) = (a); (i) <= (b); (i) += (c))
#define fdn(i, a, b, c) for (int(i) = (a); (i) >= (b); (i) -= (c))
#define endl "\n"

using namespace std;

typedef long long ll;
typedef pair<ll, ll> pl;
typedef pair<int, int> pi;
typedef vector<int> vi;
typedef vector<pi> vii;
const ll MOD = 1e9 + 7;
const int stMAX = 1 << 18;
const int INF = 1e9;
const int MAX_V = 400;

int c[MAX_V][MAX_V], f[MAX_V][MAX_V];
int level[MAX_V];
int work[MAX_V];
vi adj[MAX_V];

int S, E;

bool bfs() {
    fill(level, level + MAX_V, -1);
    level[S] = 0;

    queue<int> Q;
    Q.push(S);
    while (!Q.empty()) {
        int curr = Q.front();
        Q.pop();
        for (int next : adj[curr]) {

            if (level[next] == -1 && c[curr][next] - f[curr][next] > 0) {
                level[next] = level[curr] + 1;
                Q.push(next);
            }
        }
    }

    return level[E] != -1;
}

int dfs(int curr, int dest, int flow) {
    if (curr == dest)
        return flow;

    for (int &i = work[curr]; i < adj[curr].size(); i++) {
        int next = adj[curr][i];
        if (level[next] == level[curr] + 1 && c[curr][next] - f[curr][next] > 0) {
            int df = dfs(next, dest, min(c[curr][next] - f[curr][next], flow));
            if (df > 0) {
                f[curr][next] += df;
                f[next][curr] -= df;
                return df;
            }
        }
    }
    return 0;
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int K;
    cin >> K;

    fup(_, 1, K, 1) {
        memset(f, 0, sizeof f);
        memset(c, 0, sizeof c);
        fup(i, 0, MAX_V - 1, 1) adj[i].clear();

        int N, M;
        cin >> N >> M;

        S = 1, E = N;

        vii edge;
        edge.clear();

        fup(i, 1, M, 1) {
            int u, v, w;

            cin >> u >> v >> w;

            adj[u].pb(v);
            adj[v].pb(u);
            c[u][v] += w;

            edge.pb({u, v});
        }

        int total = 0;

        while (bfs()) {
            fill(work, work + MAX_V, 0);
            while (1) {
                int flow = dfs(S, E, INF);
                if (flow == 0)
                    break;
                total += flow;
            }
        }

        int cnt = 0;

        for (pi p : edge) {
            int prev[MAX_V];
            memset(prev, -1, sizeof prev);

            queue<int> q;
            q.push(p.X);
            while (!q.empty()) {
                int curr = q.front();
                q.pop();

                for (int next : adj[curr]) {
                    if (prev[next] == -1 && c[curr][next] - f[curr][next] > 0) {
                        prev[next] = curr;
                        q.push(next);
                    }
                }
            }

            if (prev[p.Y] == -1)
                cnt++;
        }

        cout << cnt << endl;
    }
}