LA 3523 圆桌骑士
题目链接:http://vjudge.net/contest/141787#problem/A
http://poj.org/problem?id=2942
此题很经典
知识点:DFS染色,点-双连通
题意:
亚瑟王要在圆桌上召开骑士会议,为了不引发骑士之间的冲突,并且能够让会议的议题有令人满意的结果,每次开会前都必须对出席会议的骑士有如下要求:
1、 相互憎恨的两个骑士不能坐在直接相邻的2个位置;
2、 出席会议的骑士数必须是奇数,这是为了让投票表决议题时都能有结果。
如果出现有某些骑士无法出席所有会议(例如这个骑士憎恨所有的其他骑士),则亚瑟王为了世界和平会强制把他剔除出骑士团。
现在给定准备去开会的骑士数n,再给出m对憎恨对(表示某2个骑士之间使互相憎恨的),问亚瑟王至少要剔除多少个骑士才能顺利召开会议?
能够坐在一起的人,连一条边,题目就是求所有的点中,有多少个点不在任何一个奇圈里。
这个无向图,求出每个点双连通分量,但是不是每个点双连通分量都是奇圈,如果它是偶圈,那么就可以用 dfs 染色,这样我们把每个点连通分量染色,要是染色失败,那么这些点都可以构成奇圈,也就是说这些点都OK。最后查一遍这些点。
有了之前的 无向图的割顶,桥,点-双连通分量,DFS染色,就好写好多了!!!
#include <bits/stdc++.h>
#include <stdio.h>
#include <stack>
#include <algorithm>
#include <string.h>
#include <vector> using namespace std; const int Maxn = +; int A[Maxn][Maxn]; int pre[Maxn<<];
bool iscut[Maxn];
int bccno[Maxn];
int dfs_clock;
int bcc_cnt; vector <int> G[Maxn],bcc[Maxn]; struct Edge
{
int u,v;
Edge(int u=,int v=) : u(u),v(v) {}
}; stack <Edge> S; int dfs(int u, int fa)
{
int lowu = pre[u] = ++dfs_clock;
int child = ;
for(int i = ; 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(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 < && child == ) iscut[u] = ;
return lowu;
} void find_bcc(int n)
{
memset(pre, , sizeof(pre));
memset(iscut, , sizeof(iscut));
memset(bccno, , sizeof(bccno));
dfs_clock = bcc_cnt = ;
for(int i = ; i < n; i++)
if(!pre[i]) dfs(i, -);
} int odd[Maxn], color[Maxn];
bool bipartite(int u, int b)
{
for(int i = ; 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] = - color[u];
if(!bipartite(v, b)) return false;
}
}
return true;
} int main()
{
int n, m;
while(scanf("%d%d", &n, &m) == && n)
{
for(int i = ; i < n; i++) G[i].clear(); memset(A, , sizeof(A));
for(int i = ; i < m; i++)
{
int u, v;
scanf("%d%d", &u, &v);
u--;
v--;
A[u][v] = A[v][u] = ;
}
for(int u = ; u < n; u++)
for(int v = u+; v < n; v++)
if(!A[u][v])
{
G[u].push_back(v);
G[v].push_back(u);
} find_bcc(n); memset(odd, , sizeof(odd));
for(int i = ; i <= bcc_cnt; i++)
{
memset(color, , sizeof(color));
for(int j = ; j < bcc[i].size(); j++)
bccno[bcc[i][j]] = i;
int u = bcc[i][];
color[u] = ;
if(!bipartite(u, i))
{
for(int j = ; j < bcc[i].size(); j++)
odd[bcc[i][j]] = ;
}
}
int ans = n;
for(int i = ; i < n; i++)
if(odd[i])
ans--;
printf("%d\n", ans);
}
return ;
}
LA 3523 圆桌骑士的更多相关文章
- LA 3523 圆桌骑士(二分图染色+点双连通分量)
https://vjudge.net/problem/UVALive-3523 题意: 有n个骑士经常举行圆桌会议,商讨大事.每次圆桌会议至少应有3个骑士参加,且相互憎恨的骑士不能坐在圆桌旁的相邻位置 ...
- POJ2942 UVA1364 Knights of the Round Table 圆桌骑士
POJ2942 洛谷UVA1364(博主没有翻墙uva实在是太慢了) 以骑士为结点建立无向图,两个骑士间存在边表示两个骑士可以相邻(用邻接矩阵存图,初始化全为1,读入一对憎恨关系就删去一条边即可),则 ...
- UVALive 3523 Knights of the Round Table 圆桌骑士 (无向图点双连通分量)
由于互相憎恨的骑士不能相邻,把可以相邻的骑士连上无向边,会议要求是奇数,问题就是求不在任意一个简单奇圈上的结点个数. 如果不是二分图,一定存在一个奇圈,同一个双连通分量中其它点一定可以加入奇圈.很明显 ...
- uvalive 3523 Knights of the Round Table 圆桌骑士(强连通+二分图)
题目真心分析不出来.看了白书才明白,不过有点绕脑. 容易想到,把题目给的不相邻的关系,利用矩阵,反过来建图.既然是全部可行的关系,那么就应该能画出含奇数个点的环.求环即是求双连通分量:找出所有的双连通 ...
- 【LA3523 训练指南】圆桌骑士 【双连通分量】
题意 有n个骑士经常举行圆桌会议,商讨大事.每次圆桌会议至少应有3个骑士参加,且相互憎恨的骑士不能坐在圆桌旁的相邻位置.如果发生意见分歧,则需要举手表决,因此参加会议的骑士数目必须是奇数,以防赞同和反 ...
- POJ 2942 圆桌骑士
之前做过这个题目,现在回想起来,又有新的柑橘. 求必须出去的骑士人数. 每一个双连通分量,如果是一个奇圈,那么一定是二分图染色失败. 依次遍历每个双连通分量,但是,对于邻接表中,有一些点不是双连通分量 ...
- poj 2942 Knights of the Round Table 圆桌骑士(双连通分量模板题)
Knights of the Round Table Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 9169 Accep ...
- la3523 白书例题 圆桌骑士 双联通分量+二分图
具体题解看大白书P316 #include <iostream> #include <algorithm> #include <vector> #include & ...
- KNIGHTS - Knights of the Round Table 圆桌骑士 点双 + 二分图判定
---题面--- 题解: 考场上只想到了找点双,,,,然后不知道怎么处理奇环的问题. 我们考虑对图取补集,这样两点之间连边就代表它们可以相邻, 那么一个点合法当且仅当有至少一个大小至少为3的奇环经过了 ...
随机推荐
- ajaxfileupload.js支持多文件上传【转载】
//修改前代码------- //var oldElement = jQuery('#' + fileElementId); //var newElement = jQuery(oldElement) ...
- C++ STL
1.stackstack 模板类的定义在<stack>头文件中.stack 模板类需要两个模板参数,一个是元素类型,一个容器类型,但只有元素类型是必要的,在不指定容器类型时,默认的容器类型 ...
- 对struct和class使用大括号初始化的测试
#include <iostream> using namespace std; struct struct1{ /* struct1(){ cout<<"this ...
- IOS第二天多线程-04简化单例模式
******HMSingleton-ARC.h // .h文件 #define HMSingletonH(name) + (instancetype)shared##name; // .m文件 #de ...
- 找规律 ZOJ3498 Javabeans
Javabeans are delicious. Javaman likes to eat javabeans very much. Javaman has n boxes of javabeans. ...
- js串讲回顾
注:1.xx.nextSibling.css.xxx->xx的下一个元素的css样式;2. window.opener.document.getElementById("cms&quo ...
- .dmp文件导入个别表
imp bizinfo_qy/bizinfo_qy@orcl161 file=D:\收付费核算系统\分线\bizinfo_qy.dmp log=bizinfo_qy.log TABLES=(al_i ...
- iOS,XMPP本地环境搭建和框架使用
1.XMPP的MySQL和openfire环境配置 2.XmppFramework框架导入和介绍 XMPP的MySQL和openfire环境配置 1.下载mysql安装 mysql下载 打开MySQL ...
- leetcode 111
题目描述: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along th ...
- Leetcode: K-th Smallest in Lexicographical Order
Given integers n and k, find the lexicographically k-th smallest integer in the range from 1 to n. N ...