首页 > 代码库 > uva 1364

uva 1364

刘书上例题 

#include <cstdio>#include <cstdlib>#include <cmath>#include <set>#include <stack>#include <vector>#include <sstream>#include <cstring>#include <string>#include <map>#include <queue>#include <algorithm>#include <iostream>#define FFI freopen("in.txt", "r", stdin)#define maxn 1010#define INF 0x3f3f3f3f#define inf 10000000#define MOD 1000000007#define ULL unsigned long long#define LL long long#define _setm(houge) memset(houge, INF, sizeof(houge))#define _setf(houge) memset(houge, -1, sizeof(houge))#define _clear(houge) memset(houge, 0, sizeof(houge))using namespace std;struct Edge{    int u, v;};int color[maxn], pre[maxn], iscut[maxn], bccno[maxn], dfs_clock, bcc_cnt;vector<int> G[maxn], bcc[maxn];stack<Edge> S;int A[maxn][maxn], odd[maxn];bool bipartite(int u, int b) {    for(int i = 0; i < (int)G[u].size(); ++ i) {        int v = G[u][i];        if(bccno[v] != b) continue;        if(color[v] == color[u]) return false;        if(!color[v]) {            color[v] = 3-color[u];            if(!bipartite(v, b)) return false;        }    }    return true;}int dfs(int u, int fa) {    int lowu = pre[u] = ++ dfs_clock;    int child = 0;    for(int i = 0; i < (int)G[u].size(); ++ i) {        int v = G[u][i];        Edge e = (Edge){u, v};        if(!pre[v]) {            S.push(e);            child ++;            int lowv = dfs(v, u);            lowu = min(lowu, lowv);            if(lowv >= pre[u]) {                iscut[u] = true;                bcc_cnt ++;                bcc[bcc_cnt].clear();                for(;;) {                    Edge x = S.top();                    S.pop();                    if(bccno[x.u] != bcc_cnt) {                        bcc[bcc_cnt].push_back(x.u);                        bccno[x.u] = bcc_cnt;                    }                    if(bccno[x.v] != bcc_cnt) {                        bcc[bcc_cnt].push_back(x.v);                        bccno[x.v] = bcc_cnt;                    }                    if(x.u == u && x.v == v) break;                }            }        }        else if(pre[v] < pre[u] && v != fa) {            S.push(e);            lowu = min(lowu, pre[v]);        }    }    if(fa < 0 && child == 1) iscut[u] = 0;    return lowu;}void find_bcc(int n) {    _clear(pre);    _clear(iscut);    _clear(bccno);    dfs_clock = bcc_cnt = 0;    for(int i = 0; i < n; ++ i) {        if(!pre[i]) dfs(i, -1);    }}int main() {    int n, m;    // FFI;    while(scanf("%d%d", &n, &m) == 2 && n) {        for(int i = 0; i < n; ++ i) G[i].clear();        _clear(A);        for(int i = 0; i < m; ++ i) {            int u, v;            scanf("%d%d", &u, &v);            u--, v--;            A[u][v] = A[v][u] = 1;        }        for(int u = 0; u < n; ++ u) {            for(int v = u+1; v < n; ++ v) {                if(!A[u][v]) {                    G[u].push_back(v);                    G[v].push_back(u);                }            }        }        find_bcc(n);        _clear(odd);        for(int i = 1; i <= bcc_cnt; ++ i) {            _clear(color);            for(int j = 0; j < (int)bcc[i].size(); ++ j) {                bccno[bcc[i][j]] = i;            }            int u = bcc[i][0];            color[u] = 1;            if(!bipartite(u, i)) {                for(int j = 0; j < (int)bcc[i].size(); ++ j) {                    odd[bcc[i][j]] = 1;                }            }        }        int ans = n;        for(int i = 0; i < n; ++ i) if(odd[i]) --ans;        printf("%d\n", ans);    }    return 0;}