UVA 1364 - Knights of the Round Table(双连通+二分图判定)
2014-08-29 15:21
316 查看
UVA 1364 - Knights of the Round Table
题目链接题意:有n个圆桌骑士,知道一些骑士互相憎恨,现在要开圆桌会议,每次最少3个人,必须是奇数人数,并且互相憎恨的骑士不能在相邻,问有多少骑士是一次都无法参加的
思路:把每个骑士可以相邻的连边,然后做双连通分量,然后对于每个连通分量,利用二分图染色判定去判断是否是奇圈
代码:
#include <cstdio>
#include <cstring>
#include <vector>
#include <stack>
using namespace std;
const int N = 1005;
struct Edge {
int u, v;
Edge() {}
Edge(int u, int v) {
this->u = u;
this->v = v;
}
};
int pre
, bccno
, dfs_clock, bcc_cnt;
bool iscut
;
vector<int> g
, bcc
;
stack<Edge> S;
int dfs_bcc(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 = Edge(u, v);
if (!pre[v]) {
S.push(e);
child++;
int lowv = dfs_bcc(v, u);
lowu = min(lowu, lowv);
if (lowv >= pre[u]) {
iscut[u] = true;
bcc_cnt++; bcc[bcc_cnt].clear(); //start from 1
while(1) {
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] = false;
return lowu;
}
void find_bcc(int n) {
memset(pre, 0, sizeof(pre));
memset(iscut, 0, sizeof(iscut));
memset(bccno, 0, sizeof(bccno));
dfs_clock = bcc_cnt = 0;
for (int i = 0; i < n; i++)
if (!pre[i]) dfs_bcc(i, -1);
}
int odd
, color
;
bool bipartite(int u, int b) {
for (int i = 0; i < 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 n, m, A
;
int main() {
int cas = 0;
while (~scanf("%d%d", &n, &m) && n) {
for (int i = 0; i < n; i++) g[i].clear();
memset(A, 0, sizeof(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);
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++)
ans -= odd[i];
printf("%d\n", ans);
}
return 0;
}
相关文章推荐
- UVALive - 3523 Knights of the Round Table(【点双连通分量】+【二分图判定】)
- 【连通图|双连通+二分图判定】POJ-2942 Knights of the Round Table
- UVALive3523-Knights of the Round Table(BCC+二分图判定)
- UVA 1364 Knights of the Round Table(双联通+二分图)
- UVALive 3523 Knights of the Round Table(BCC+二分图)
- uvalive 3523 Knights of the Round Table 圆桌骑士(强连通+二分图)
- uva 1364 - Knights of the Round Table(点双连通分量,5级)
- POJ_2942_Knights of the Round Table(点的双连通分量+二分图判定)
- POJ 2942 Knights of the Round Table ★(点双连通分量+二分图判定)
- POJ 2942 Knights of the Round Table(【点双连通分量】+【二分图判定】)
- UVA 1364 - Knights of the Round Table (找双连通分量 + 二分图染色法判断)
- poj 2942 Knights of the Round Table(无向图的双连通分量+二分图判定)
- poj2942 Knights of the Round Table,无向图点双联通,二分图判定
- uva1364 - Knights of the Round Table 点-双联通分量
- poj 2942 Knights of the Round Table(无向图的双连通分量+二分图判定)
- poj 2942 Knights of the Round Table(双连通分量+tarjan+二分图判定)
- LA 3523 Knights of the Round Table 边双连通分量+二分图判定
- uva 1364 - Knights of the Round Table(二分图+强连通)
- POJ 2942 Knights of the Round Table ★(点双连通分量+二分图判定)
- uva 1364 - Knights of the Round Table(点双连通分量,5级)