POJ 2942 Knights of the Round Table 补图+tarjan求点双联通分量+二分图染色+debug
题面还好,就不描述了
重点说题解:
由于仇恨关系不好处理,所以可以搞补图存不仇恨关系,
如果一个桌子上面的人能坐到一起,显然他们满足能构成一个环
所以跑点双联通分量
求点双联通分量我用的是向栈中push边的方法
请注意:只向栈中push树枝边
这样每次一对父子(u,v)
如果low[v]<=dfn[u] 显然u是v所在点双联通分量的割点
所以栈中边(u,v)之前的边都pop,他们连接的点构成点双联通分量
我们找到一个点双联通分量之后,由于要求奇数个人坐一桌
所以满足存在奇环,dfs染色即可
注意割点可能属于很多点双联通分量,所以染完色之后需要给他把颜色去掉
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<stack>
#define N 1010
using namespace std;
int n,m,head[N],adj[N][N],ans,dfn[N],low[N],ok[N],ecnt,indx,clr[N],inst[N];
int read()
{
int ret=,neg=;
char j=getchar();
for (;j<'' || j>'';j=getchar())
if (j=='-') neg=-;
for (;j<='' && j>='';j=getchar())
ret=ret*+j-'';
return ret*neg;
}
struct edge
{
int u,v,nxt;
}e[N*N];
stack <edge> stk;
stack <int> tmp;
void add(int u,int v)
{
e[++ecnt].u=u;
e[ecnt].v=v;
e[ecnt].nxt=head[u];
head[u]=ecnt;
}
void getG()//还是推荐链式前向星存图= =
{
for (int i=,u,v;i<=m;i++)
{
u=read(),v=read();
adj[u][v]=;
adj[v][u]=;
}
for (int i=;i<=n;i++)
for (int j=;j<=n;j++)
if (adj[i][j]== && i!=j) add(i,j);
}
int dfs(int u,int nw)//染色
{
int ret=;
for (int i=head[u];i;i=e[i].nxt)
{
int v=e[i].v;
if (inst[v]==)
{
if (clr[v]==-)
{
clr[v]=-nw;
ret&=dfs(v,-nw);
}
else
if (clr[v]==nw) ret=;
}
}
return ret;
}
void init()
{
memset(ok,,sizeof(ok));
memset(head,,sizeof(head));
memset(adj,,sizeof(adj));
memset(inst,,sizeof(inst));
ecnt=;
memset(dfn,,sizeof(dfn));
memset(clr,-,sizeof(clr));
indx=;
ans=;
}
void tar(int u,int fa)
{
dfn[u]=low[u]=++indx;
for (int i=head[u];i;i=e[i].nxt)
{
int v=e[i].v;
if (!dfn[v])
{
stk.push(e[i]);
tar(v,u);
low[u]=min(low[u],low[v]);
if (low[v]>=dfn[u])//题解找双联通分量部分
{
while ()
{
edge e=stk.top();
stk.pop();
inst[e.u]=;
inst[e.v]=;
tmp.push(e.u);
tmp.push(e.v);
if (e.u==u && e.v==v) break;
}
clr[u]=;
if (dfs(u,)==)
while (!tmp.empty())
{
ok[tmp.top()]=;
inst[tmp.top()]=;
tmp.pop();
}
else while (!tmp.empty()) inst[tmp.top()]=,tmp.pop();
clr[u]=-;
}
}
else if (v!=fa)
low[u]=min(low[u],dfn[v]);
}
}
int main()
{
n=read();
m=read();
while (n || m)
{
init();
getG();
for (int i=;i<=n;i++)
if (!dfn[i]) tar(i,-);
for (int i=;i<=n;i++)
ans+=ok[i];
printf("%d\n",n-ans);
n=read(),m=read();
}
return ;
}
POJ 2942 Knights of the Round Table 补图+tarjan求点双联通分量+二分图染色+debug的更多相关文章
- POJ 2942 Knights of the Round Table 黑白着色+点双连通分量
题目来源:POJ 2942 Knights of the Round Table 题意:统计多个个骑士不能參加随意一场会议 每场会议必须至少三个人 排成一个圈 而且相邻的人不能有矛盾 题目给出若干个条 ...
- poj 2942 Knights of the Round Table - Tarjan
Being a knight is a very attractive career: searching for the Holy Grail, saving damsels in distress ...
- POJ 2942 Knights of the Round Table
Knights of the Round Table Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 10911 Acce ...
- poj 2942 Knights of the Round Table 圆桌骑士(双连通分量模板题)
Knights of the Round Table Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 9169 Accep ...
- POJ 2942 Knights of the Round Table - from lanshui_Yang
Description Being a knight is a very attractive career: searching for the Holy Grail, saving damsels ...
- poj 2942 Knights of the Round Table(点双连通分量+二分图判定)
题目链接:http://poj.org/problem?id=2942 题意:n个骑士要举行圆桌会议,但是有些骑士相互仇视,必须满足以下两个条件才能举行: (1)任何两个互相仇视的骑士不能相邻,每个骑 ...
- POJ 2942 Knights of the Round Table(双连通分量)
http://poj.org/problem?id=2942 题意 :n个骑士举行圆桌会议,每次会议应至少3个骑士参加,且相互憎恨的骑士不能坐在圆桌旁的相邻位置.如果意见发生分歧,则需要举手表决,因此 ...
- POJ 2942 Knights of the Round Table (点双连通分量)
题意:多个骑士要开会,3人及以上才能凑一桌,其中部分人已经互相讨厌,肯定不坐在同一桌的相邻位置,而且一桌只能奇数个人才能开台.给出多个人的互相讨厌图,要求多少人开不成会(注:会议不要求同时进行,一个人 ...
- POJ 2942.Knights of the Round Table (双连通)
简要题解: 意在判断哪些点在一个图的 奇环的双连通分量内. tarjan求出所有的点双连通分量,再用二分图染色判断每个双连通分量是否形成了奇环,记录哪些点出现在内奇环内 输出没有在奇环内的点的数目 ...
随机推荐
- 用Java读取xml文件内容
在AXP中,DOM解析器是1 Document Builder类的一个实例,该实例由 DocumenBailderfactorv类负责创,步如下 DocumentBuilderFactory fa ...
- MySql开启GTID和多线程复制功能
1.修改参数 master: gtid_mode = ON --开启gtid这个必须打开 enforce-gtid-consistency = ON ...
- python导包学习总结
python初学者,对于导包纠结了不少时间,总结分享,持续前进~ Python导包的两种方法: 1.1 from 包.模块 import 方法名,调用时直接使用方法名() 1.2 from 包. ...
- mysql 1055 的错误
1.Err1055,出现这个问题往往是在执行sql语句时候,在最后一行会出现这个问题. [Err] 1055 - Expression #1 of ORDER BY clause is not in ...
- IE hack 和手机尺寸
来自为知笔记(Wiz)
- 命名空间“Microsoft.Office”中不存在类型或命名空间名称“Interop”(是否缺少程序集引用?
在一个web项目中需要导出word打印,引用Microsoft.Office.Interop.Word后,在pages里使用正常,在app_code里新建类引用就报错. Report.cs里using ...
- php扩展开发-MINFO
我们在用PHPinfo函数或命令行的php -i命令查看php环境相关的信息,当我们开发完成一个自己的扩展,除非这个扩展就是你自己所使用,否则你就需要对扩展进行相关的介绍,或者显示扩展用到的ini配置 ...
- IAR 编译时找不到头文件的解决方法
Fatal Error[Pe1696]: cannot open source file "x.h" 那是因为头文件路径没有找对 到报错的.c源文件 选中右键 选择options ...
- Hadoop三大发行版本
apache 提供基础版本 cloudera 主要是修改Hadoop,提供更加稳定的发行版本,以及可视化的管理服务,主要产品如下: CDH:Cloudera Distributed Hadoop Cl ...
- saltstack plug in
目录 可插拔的子系统 灵活性 虚拟模块 salt的核心架构提供了一种高速的交流总线,在核心架构的上层,salt暴露出来的特征是:松散耦合,可插拔的子系统. 可插拔的子系统 salt包含20中插件系统, ...