UVALive3523-Knights of the Round Table(BCC+二分图判定)

题目链接

题意:有n个骑士经常举行圆桌会议,每次至少3人参加,且相互厌恶的其实不能坐在圆桌相邻的位置。如果发生意见分歧,则要举手表决,因此参加的骑士数目一定要为奇数。统计有多少人不能参加任何一个会议。

思路:这是大白上面的一道例题。我们可以先根据骑士之间的关系建立无向图G,则题目就转化为求不再任何一个简单奇圈上的结点个数。如果图G不连通,就分别对G的连通分量求解。简单圈上的所有结点必然属于同一个双连通分量,因此我们只要求出所有的双连通分量。但是二分图是不存在奇圈的,所以我们只需要关注那些不是二分图的双连通分量。当结点v所属的某一个双连通分量B不是二分图,v一定属于一个奇圈。所以我们只要判断双连通分量是不是二分图就可以了。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <stack>
#include <algorithm>

using namespace std;

const int MAXN = 1050;

struct edge{
    edge() {}
    edge(int uu, int vv) {
        u = uu;
        v = vv;
    }
    int u, v;
};

vector<int> g[MAXN], bcc[MAXN];
stack<edge> S;
int pre[MAXN], bccno[MAXN], iscut[MAXN], odd[MAXN], color[MAXN], map[MAXN][MAXN];
int n, m, dfs_clock, bcc_cnt;

int dfs(int u, int fa) {
    int lowu = pre[u] = ++dfs_clock;
    int child = 0;
    for (int i = 0; i < g[u].size(); i++) {
        int v = g[u][i];
        edge e(u, v);
        if (!pre[v]) {
            S.push(e);
            child++;
            int lowv = dfs(v, u);
            lowu = min(lowu, lowv);
            if (lowv >= pre[u]) {
                iscut[u] = 1;
                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() {
    memset(pre, 0, sizeof(pre));
    memset(bccno, 0, sizeof(bccno));
    memset(iscut, 0, sizeof(iscut));
    dfs_clock = bcc_cnt = 0;
    for (int i = 0; i < n; i++)
        if (!pre[i])
            dfs(i, -1);
}

int bipartite(int u, int cur_bccno) {
    for (int i = 0; i < g[u].size(); i++) {
        int v = g[u][i];
        if (bccno[v] != cur_bccno) continue;
        if (color[v] == color[u]) return 0;
        if (!color[v]) {
            color[v] = 3 - color[u];
            if (!bipartite(v, cur_bccno)) return 0;
        }
    }
    return 1;
}

int main() {
    while (scanf("%d%d", &n, &m) && (n + m)) {
        for (int i = 0; i < n; i++) g[i].clear();

        memset(map, 0, sizeof(map));
        for (int i = 0; i < m; i++) {
            int u, v;
            scanf("%d%d", &u, &v);
            u--;
            v--;
            map[u][v] = map[v][u] = 1;
        }
        for (int i = 0; i < n; i++)
            g[i].clear();
        for (int i = 0; i < n; i++)
            for (int j = i + 1; j < n; j++)
                if (!map[i][j]) {
                    g[i].push_back(j);
                    g[j].push_back(i);
                }

        find_bcc();

        memset(odd, 0, sizeof(odd));
        for (int i = 1; i <= bcc_cnt; i++) {
            memset(color, 0, sizeof(color));
            for (int j = 0; j < 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 < 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;
}

时间: 2024-08-03 14:34:46

UVALive3523-Knights of the Round Table(BCC+二分图判定)的相关文章

UvaLive3523 Knights of the Round Table(点双联通分量+二分图染色)

UvaLive3523 Knights of the Round Table 参考了Kuangbin巨的题解. /* POJ 2942 Knights of the Round Table 亚瑟王要在圆桌上召开骑士会议,为了不引发骑士之间的冲突, 并且能够让会议的议题有令人满意的结果,每次开会前都必须对出席会议的骑士有如下要求: 1. 相互憎恨的两个骑士不能坐在直接相邻的2个位置: 2. 出席会议的骑士数必须是奇数,这是为了让投票表决议题时都能有结果. 注意:1.所给出的憎恨关系一定是双向的,不

UVALive 3523 Knights of the Round Table(二分图+双连通分量)

Description Being a knight is a very attractive career: searching for the Holy Grail, saving damsels in distress, and drinking with the other knights are fun things to do. Therefore, it is not very surprising that in recent years the kingdom of King

UVALive-3523 Knights of the Round Table (双连通分量+二分图匹配)

题目大意:有n个骑士要在圆桌上开会,但是相互憎恶的两个骑士不能相邻,现在已知骑士们之间的憎恶关系,问有几个骑士一定不能参加会议.参会骑士至少有3个且有奇数个. 题目分析:在可以相邻的骑士之间连一条无向边,构成一张图G.则问题变成了有几个节点不在奇圈(有奇数个节点的圈)内,并且一个点在圈内最多出现一次.如果G不连通,应该对每一个分量分别求解.奇圈上的点一定在同一个双连通分量内,要找出所有的双连通分量.但是能构成二分图的双连通分量中一定没有奇圈,不能构成二分图的双连通分量中一定含有奇圈,并且分量中所

POJ2942 Knights of the Round Table[点双连通分量|二分图染色|补图]

Knights of the Round Table Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 12439   Accepted: 4126 Description Being a knight is a very attractive career: searching for the Holy Grail, saving damsels in distress, and drinking with the oth

POJ 2942 Knights of the Round Table (点-双连通分量 + 交叉法染色判二分图)

POJ 2942 Knights of the Round Table 链接:http://poj.org/problem?id=2942 题意:亚瑟王要在圆桌上召开骑士会议,为了不引发骑士之间的冲突,并且能够让会议的议题有令人满意的结果,每次开会前都必须对出席会议的骑士有如下要求: 1. 相互憎恨的两个骑士不能坐在直接相邻的2个位置: 2. 出席会议的骑士数必须是奇数,这是为了让投票表决议题时都能有结果. 如果出现有某些骑士无法出席所有会议(例如这个骑士憎恨所有的其他骑士),则亚瑟王为了世界和

UVA 1364 - Knights of the Round Table(双连通+二分图判定)

UVA 1364 - Knights of the Round Table 题目链接 题意:有n个圆桌骑士,知道一些骑士互相憎恨,现在要开圆桌会议,每次最少3个人,必须是奇数人数,并且互相憎恨的骑士不能在相邻,问有多少骑士是一次都无法参加的 思路:把每个骑士可以相邻的连边,然后做双连通分量,然后对于每个连通分量,利用二分图染色判定去判断是否是奇圈 代码: #include <cstdio> #include <cstring> #include <vector> #in

POJ 2942 Knights of the Round Table 黑白着色+点双连通分量

题目来源:POJ 2942 Knights of the Round Table 题意:统计多个个骑士不能參加随意一场会议 每场会议必须至少三个人 排成一个圈 而且相邻的人不能有矛盾 题目给出若干个条件表示2个人直接有矛盾 思路:求补图  能够坐在一起 就是能够相邻的人建一条边 然后假设在一个奇圈上的都是满足的 那些不再不论什么一个奇圈的就是不满足 求出全部奇圈上的点 总数减去它就是答案 首先有2个定理 1.一个点双连通分量是二分图 你就没有奇圈 假设有奇圈  那就不是二分图  充分必要条件 所

POJ 2942 - Knights of the Round Table(双连通图 Tarjan + 二分判定)

POJ 2942 - Knights of the Round Table(双连通图 Tarjan + 二分判定) ACM 题目地址: POJ 2942 - Knights of the Round Table 题意: 有N个骑士,给出某些骑士之间的仇恨关系,骑士们开会时会围坐在一个圆桌旁.一次会议能够顺利举行,要满足两个条件: 任意相互憎恨的两个骑士不能相邻 开会人数为大于2的奇数 若某个骑士任何会议都不能参加,那么就必须将他踢出,给出骑士之间的仇恨关系,问最少需要踢出多少个骑士? 分析: 把

poj 2942 Knights of the Round Table 【双连通缩点+判奇圈】【经典】

题目:poj 2942 Knights of the Round Table 题意:n个骑士经常一起开会,其中有一些两两相互憎恨,他们不能同一桌,开会要表决一些事情,所以必须奇数个人,最少3个,求永远也参加不了会议的人的个数. 分析:这个题目两点 首先,建图求双连通缩点 建图的话,因为相互憎恨的不能再一块,所以要建补图,让能够在一起的所有的连接,这样的话,如果能存在环且环上的点是奇数个的话就可以参加会议,标记求不能参加的即可. 建好图之后用tarjan算法双连通缩点,把在一个环上的点保存起来.