圆桌骑士

AcWing-365-圆桌骑士open in new window

分析

见《进阶指南》第407页。

实现

#include <iostream>
#include <cstring>
#include <vector>
#include <stack>
using namespace std;

const int N = 1010, M = 1000010;
struct edge {
    int to, next;
};
edge e[2 * M];
int idx, head[N];
int n, m;
bool g[N][N], mark[N];
int root, cnt, dfn[N], low[N];
stack<int> s;
int tot;
vector<int> dcc[N];
bool flag;
int d[N], c[N];

void add_edge (int u, int v) {
    e[idx].to = v;
    e[idx].next = head[u];
    head[u] = idx ++;
}
void initialize () {
    idx = cnt = tot = 0;
    memset(head, -1, sizeof(head));
    memset(g, 0, sizeof(g));
    memset(mark, 0, sizeof(mark));
    memset(dfn, 0, sizeof(dfn));
    memset(d, 0, sizeof(d));
    for (int i = 1; i <= n; ++ i) dcc[i].clear();
}
void tarjan (int cur) {
    low[cur] = dfn[cur] = ++ cnt;
    s.push(cur);
    if (cur == root && head[cur] == -1) {
        dcc[++ tot].push_back(cur);
        return;
    }
    for (int i = head[cur]; i != -1; i = e[i].next) {
        int to = e[i].to;
        if (dfn[to] == 0) {
            tarjan(to);
            low[cur] = min(low[cur], low[to]);

            if (low[to] >= dfn[cur]) {
                ++ tot;
                int t;
                do {
                    t = s.top();
                    s.pop();
                    dcc[tot].push_back(t);
                } while (t != to);
                dcc[tot].push_back(cur);
            }
        } else {
            low[cur] = min(low[cur], dfn[to]);
        }
    }
}
void dfs (int cur, int color, int cur_dcc) {
    c[cur] = color;
    for (int i = head[cur]; i != -1; i = e[i].next) {
        int to = e[i].to;
        if (d[to] != cur_dcc) continue;
        if (c[to] == color) {
            flag = true;
            return;
        }
        if (c[to] == 0) dfs(to, 3 - color, cur_dcc);
    }
}

int main () {
    while (scanf("%d%d", &n, &m), n || m) {
        initialize();
        for (int i = 1, u, v; i <= m; ++ i) {
            scanf("%d%d", &u, &v);
            g[u][v] = g[v][u] = true;
        }
        
        for (int i = 1; i <= n; ++ i) {
            for (int j = i + 1; j <= n; ++ j) {
                if (g[i][j] == false) {
                    add_edge(i, j);
                    add_edge(j, i);
                }
            }
        }
        for (int i = 1; i <= n; ++ i) {
            if (dfn[i] == 0) {
                root = i;
                tarjan(i);
            }
        }
        for (int i = 1; i <= tot; ++ i) {
            flag = false;
            for (int j = 0; j < dcc[i].size(); ++ j) {
                d[dcc[i][j]] = i;
                c[dcc[i][j]] = 0;
            }
            dfs(dcc[i][0], 1, i);
            if (flag == true) {
                for (int j = 0; j < dcc[i].size(); ++ j)
                    mark[dcc[i][j]] = true;
            }
        }
        int res = 0;
        for (int i = 1; i <= n; ++ i)
            if (mark[i] == false)
                ++ res;
        printf("%d\n", res);
    }
    return 0;
}
最后修改于: