【代码求助】
查看原帖
【代码求助】
24056
vda96楼主2020/5/5 23:52

T了8个点

#include<bits/stdc++.h>
using namespace std;

typedef pair<int, int> pii;
const int N = 100000 + 5, M = 150000 + 5, inf = 0x3f3f3f3f;

int n, m, u[M], v[M];


int cnt, head[N], cur[N], nxt[(1000010) << 1], to[(1000010) << 1], cap[(1000010) << 1], id[M];

int addedge(int u, int v, int c) {
    nxt[++cnt] = head[u], head[u] = cnt, to[cnt] = v, cap[cnt] = c;
    nxt[++cnt] = head[v], head[v] = cnt, to[cnt] = u, cap[cnt] = 0;
    return cnt - 1;
}

int s, t, d[N];
queue<int> q;

bool bfs() {
    for (int i = s; i <= t; ++i) d[i] = 0;
    q.push(s), d[s] = 1;
    while (!q.empty()) {
        int x = q.front();
        q.pop();
        for (int i = head[x]; i; i = nxt[i]) {
            int y = to[i];
            if (!cap[i] || d[y]) continue;
            d[y] = d[x] + 1;
            if (y == t) return true;
            q.push(y);
        }
    }
    return false;
}

int dfs(int x, int f) {
    if (x == t) return f;
    int rest = f;
    for (int &i = cur[x]; i && rest; i = nxt[i]) {
        int y = to[i];
        if (!cap[i] || d[x] + 1 != d[y]) continue;
        int w = dfs(y, min(rest, cap[i]));
        rest -= w, cap[i] -= w, cap[i ^ 1] += w;
        if (!w) d[y] = 0;
    }
    return f - rest;
}

void maxflow() {
    while (bfs()) {
        for (int i = s; i <= t; ++i) cur[i] = head[i];
        dfs(s, inf);
    }
}

int cnt1, head1[N], nxt1[M << 1], to1[M << 1];

void addedge1(int u, int v) {
    nxt1[++cnt1] = head1[u], head1[u] = cnt1, to1[cnt1] = v;
}

int color[N];
queue<int> Q;

void bfs1(int s) {
    Q.push(s), color[s] = 0;
    while (!Q.empty()) {
        int x = Q.front();
        Q.pop();
        for (int i = head1[x]; i; i = nxt1[i]) {
            int y = to1[i];
            if (color[y] == -1) {
                color[y] = !color[x];
                Q.push(y);
            }
        }
    }
}

void build() {
    for (int i = 1; i <= n; ++i) color[i] = -1;
    for (int i = 1; i <= n; ++i) if (color[i] == -1) bfs1(i);
    cnt = 1, s = 0, t = n + 1;
    for (int i = 1; i <= n; ++i) if (color[i]) addedge(s, i, 1); else addedge(i, t, 1);
    for (int i = 1; i <= m; ++i)
        if (color[u[i]])
            id[i] = addedge(u[i], v[i], 1);
        else id[i] = addedge(v[i], u[i], 1);
    maxflow();
}


int num, tot, top, belong[N], dfn[N], low[N], stk[N];
bool ins[N];

void dfs1(int x) {
    dfn[x] = low[x] = ++num, stk[++top] = x, ins[x] = true;
    for (int i = head[x]; i; i = nxt[i]) {
        int y = to[i];
        if (!cap[i]) continue;
        if (!dfn[y]) dfs1(y), low[x] = min(low[x], low[y]);
        else if (ins[y]) low[x] = min(low[x], dfn[y]);
    }
    if (dfn[x] == low[x]) {
        ++tot;
        int y;
        do {
            y = stk[top--], ins[y] = false;
            belong[y] = tot;
        } while (x != y);
    }
}

void solve() {
    for (int i = s; i <= t; ++i) if (!dfn[i]) dfs1(i);
}

void work() {
    vector<pii> ans;
    for (int i = 1; i <= m; ++i) {
        if (cap[id[i]]) continue;
        int a = u[i], b = v[i];
        if (a > b) swap(a, b);
        if (belong[a] == belong[b]) continue;
        ans.push_back(make_pair(a, b));
    }
    sort(ans.begin(), ans.end());
    printf("%lu\n", ans.size());
    for (int i = 0; i < ans.size(); ++i) printf("%d %d\n", ans[i].first, ans[i].second);
}

int main() {
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= m; ++i) scanf("%d%d", &u[i], &v[i]), addedge1(u[i], v[i]), addedge1(v[i], u[i]);
    build();
    solve();
    work();
    return 0;
}

查了半天也没发现哪里写错了

2020/5/5 23:52
加载中...