输出字典序最小的最小割集

USACO 5.4Telecowmunication

/*
ID: gyarena2
PROG: telecow
LANG: C++
*/

char *Task = "telecow";

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef unsigned UI;
typedef pair<int, int> PAIR;

const int MaxN(210);
const int MaxM(10010);
const int MaxE(3010);
const int MaxL(110);
const int MaxD(510);
const int MaxH(16);
const int Inf((INT_MAX - 1) / 3);
const int F(0);
const double Eps(1e-7);
UI Base(1001);
UI Fix0(9);

template<typename T>
inline bool checkmax(T &a, const T &b) {
    return b > a ? ((a = b), true) : false;
}

template<typename T>
inline bool checkmin(T &a, const T &b) {
    return b < a ? ((a = b), true) : false;
}

template<typename T>
inline T Abs(T a) {
    return a < 0 ? -a : a;
}

inline int Dcmp(double t) {
    return Abs(t) < Eps ? 0 : (t < 0 ? -1 : 1);
}

void SetIO() {
    char inf[64], ouf[64];
    sprintf(inf, "%s.in", Task);
    sprintf(ouf, "%s.out", Task);
    freopen(inf, "r", stdin);
    freopen(ouf, "w", stdout);
}

struct E {
    int u, v, f;
    E *next;
    E() {}
    E(int u_, int v_, int f_, E *n_) :u(u_), v(v_), f(f_), next(n_) {}
};

int level[MaxN];
int value[MaxN];
queue<int> que;
struct G {
    E *h[MaxN], e[MaxE], *re;
    void init(int n) {
        memset(h, 0, sizeof(h[0])*n);
        re = e;
    }
    void add(int u, int v, int f) {
        *re = E(u, v, f, h[u]);
        h[u] = re++;
        *re = E(v, u, 0, h[v]);
        h[v] = re++;
    }
    bool bfs(int sour, int sink, int n) {
        memset(level, -1, sizeof(level[0])*n);
        level[sour] = 0;
        que.push(sour);
        while (!que.empty()) {
            int u = que.front();
            que.pop();
            for (E *i = h[u]; i; i = i->next) {
                if (i->f == 0 || level[i->v] != -1) continue;
                level[i->v] = level[u] + 1;
                que.push(i->v);
            }
        }
        return level[sink] != -1;
    }
    int dfs(int u, int sink, int lim) {
        if (u == sink) return lim;
        int ret = 0;
        for (E *i = h[u]; i; i = i->next) {
            if (i->f == 0 || level[i->v] != level[u] + 1) continue;
            int t = dfs(i->v, sink, min(lim, i->f));
            i->f -= t;
            e[(i - e) ^ 1].f += t;
            ret += t;
            lim -= t;
            if (lim == 0) break;
        }
        if (ret == 0) level[u] = -1;
        return ret;
    }
    int maxFlow(int sour, int sink, int n) {
        int ret = 0;
        while (bfs(sour, sink, n)) {
            ret += dfs(sour, sink, Inf);
        }
        return ret;
    }
} g;

int main() {
    SetIO();
    int n, m, sour, sink;
    scanf("%d%d%d%d", &n, &m, &sour, &sink);
    --sour;
    --sink;
    g.init(n * 2);
    int u, v;
    for (int i = 0; i < n; ++i) g.add(i * 2, i * 2 + 1, 1);
    for (int i = 0; i < m; ++i) {
        scanf("%d%d", &u, &v);
        --u;
        --v;
        g.add(u * 2 + 1, v * 2, 1);
        g.add(v * 2 + 1, u * 2, 1);
    }
    int ans = g.maxFlow(sour * 2 + 1, sink * 2, n * 2);
    vector<int> rec;
    for (int i = 0; i < n; ++i) {
        value[i] = 1;
        if (g.e[i * 2].f == 0) rec.push_back(i);
    }
    sort(rec.begin(), rec.end());
    int tans = ans;
    vector<int> ans2;
    for (vector<int>::iterator it = rec.begin(); it != rec.end(); ++it) {
        value[*it] = 0;
        for (int i = 0; i < n; ++i) {
            g.e[i * 2].f = value[i];
            g.e[i * 2 + 1].f = 0;
        }
        for (int i = 0; i < 2*m; ++i) {
            g.e[2 * n + i * 2].f = 1;
            g.e[2 * n + i * 2 + 1].f = 0;
        }
        int temp = g.maxFlow(sour * 2 + 1, sink * 2, 2 * n);
        if (temp == tans - 1) {
            --tans;
            ans2.push_back(*it);
        }
        else
            value[*it] = 1;
    }
    printf("%d\n", ans);
    for (vector<int>::iterator it = ans2.begin(); it != ans2.end(); ++it) {
        if (it != ans2.begin()) printf(" ");
        printf("%d", *it + 1);
    }
    printf("\n");
    return 0;
}

你可能感兴趣的:(图论)