KNIGHTS - Knights of the Round Table 圆桌骑士 点双 + 二分图判定
题解:
考场上只想到了找点双,,,,然后不知道怎么处理奇环的问题。
我们考虑对图取补集,这样两点之间连边就代表它们可以相邻, 那么一个点合法当且仅当有至少一个大小至少为3的奇环经过了它。
观察到只会出现一棵类似树的结构 + t个相对独立的环, 因为环肯定都是独立出来的,所以可以不用管它。
因此我们先找出所有点双,然后判断这个点双内是否有奇环,用二分图染色来判断。如果有奇环,则说明这个点双内的所有点都可以出现在一个奇环上,反之则都不会出现。
所以我们只需要寻找一下点双,然后判断是否合法并加上相应贡献即可。
#include<bits/stdc++.h>
using namespace std;
#define R register int
#define AC 1100
#define ac 11000000 int n, m, cnt, timer, ans;
int q[AC], top;
int q1[AC], head, tail;
int belong[AC], low[AC], color[AC], dfn[AC];
bool z[AC][AC], vis[AC]; inline int read()
{
int x = ;char c = getchar();
while(c > '' || c < '') c = getchar();
while(c >= '' && c <= '') x = x * + c - '', c = getchar();
return x;
} void pre()
{
n = read(), m = read();
if(!n && !m) exit();
memset(belong, , sizeof(belong));
memset(vis, , sizeof(vis));
memset(dfn, , sizeof(dfn));
timer = cnt = ans = top = ;
memset(z, , sizeof(z));
int a, b;
for(R i = ; i <= m; i ++)
{
a = read(), b = read();
z[a][b] = z[b][a] = true;
}
for(R i = ; i <= n; i ++) z[i][i] = true;//不能走自环
} inline void upmin(int &a, int b){
if(b < a) a = b;
} bool check(int x)//检查x所在点双是否是一个二分图
{
memset(color, , sizeof(color));
head = tail = ;
q1[++tail] = x, color[x] = ;
while(head < tail)
{
x = q1[++head];
for(R i = ; i <= n; i ++)
{
if(!z[x][i] && belong[i] == cnt)//如果有边并且在同一个点双中
{
if(color[x] == color[i]) return false;
else if(!color[i]) q1[++tail] = i, color[i] = color[x] ^ ;
}
}
}
return true;
} void tarjan(int x, int fa)//求点双联通分量
{
low[x] = dfn[x] = ++ timer;
for(R i = ; i <= n; i ++)//枚举边
{
if(!z[x][i])
{
if(!dfn[i])
{
q[++top] = i;//将这个点加入栈
tarjan(i, x);
upmin(low[x], low[i]);
if(low[i] >= dfn[x])//这个点是割点
{
int tot = ;//先加上割点和当前now的tot
belong[x] = ++ cnt;//先给这个割点临时打上标记
while(q[top] != i) ++tot, belong[q[top --]] = cnt;//记录下这个点所属的点双联通分量
belong[q[top --]] = cnt;
if(!check(x))//不是二分图,那么这个bcc当中的点都是合法的
{
int b = top + tot - ;//直接把刚取出来的点打上标记
vis[x] = true;//q[top] 不一定是割点
for(R i = top + ; i <= b; i ++) vis[q[i]] = true;
}
}
}
else if(i != fa) upmin(low[x], dfn[i]);
}
}
} void work()
{
while()
{
pre();
for(R i = ; i <= n; i ++)
if(!dfn[i]) q[++top] = i, tarjan(i, i);//将当前点加入栈
for(R i = ; i <= n; i ++) if(!vis[i]) ++ans;
printf("%d\n", ans);
}
} int main()
{
// freopen("in.in", "r", stdin);
work();
// fclose(stdin);
return ;
}
KNIGHTS - Knights of the Round Table 圆桌骑士 点双 + 二分图判定的更多相关文章
- poj 2942 Knights of the Round Table 圆桌骑士(双连通分量模板题)
Knights of the Round Table Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 9169 Accep ...
- uvalive 3523 Knights of the Round Table 圆桌骑士(强连通+二分图)
题目真心分析不出来.看了白书才明白,不过有点绕脑. 容易想到,把题目给的不相邻的关系,利用矩阵,反过来建图.既然是全部可行的关系,那么就应该能画出含奇数个点的环.求环即是求双连通分量:找出所有的双连通 ...
- POJ2942 UVA1364 Knights of the Round Table 圆桌骑士
POJ2942 洛谷UVA1364(博主没有翻墙uva实在是太慢了) 以骑士为结点建立无向图,两个骑士间存在边表示两个骑士可以相邻(用邻接矩阵存图,初始化全为1,读入一对憎恨关系就删去一条边即可),则 ...
- POJ 2942Knights of the Round Table(tarjan求点双+二分图染色)
Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 13954 Accepted: 4673 Description Bein ...
- UVALive 3523 Knights of the Round Table 圆桌骑士 (无向图点双连通分量)
由于互相憎恨的骑士不能相邻,把可以相邻的骑士连上无向边,会议要求是奇数,问题就是求不在任意一个简单奇圈上的结点个数. 如果不是二分图,一定存在一个奇圈,同一个双连通分量中其它点一定可以加入奇圈.很明显 ...
- POJ 2942 Knights of the Round Table 黑白着色+点双连通分量
题目来源:POJ 2942 Knights of the Round Table 题意:统计多个个骑士不能參加随意一场会议 每场会议必须至少三个人 排成一个圈 而且相邻的人不能有矛盾 题目给出若干个条 ...
- [POJ2942]Knights of the Round Table(点双+二分图判定——染色法)
建补图,是两个不仇恨的骑士连边,如果有环,则可以凑成一桌和谐的打麻将 不能直接缩点,因为直接缩点求的是连通分量,点双缩点只是把环缩起来 普通缩点 ...
- poj2942 Knights of the Round Table,无向图点双联通,二分图判定
点击打开链接 无向图点双联通.二分图判定 <span style="font-size:18px;">#include <cstdio> #include ...
- POJ2942 Knights of the Round Table【Tarjan点双联通分量】【二分图染色】【补图】
LINK 题目大意 有一群人,其中有一些人之间有矛盾,现在要求选出一些人形成一个环,这个环要满足如下条件: 1.人数大于1 2.总人数是奇数 3.有矛盾的人不能相邻 问有多少人不能和任何人形成任何的环 ...
随机推荐
- python generator
def reverse(data): for index in range(len(data)-1, -1, -1): yield data[index] for char in reverse('g ...
- Prism MEF example
Related Attributes These attributes are under namespace System.ComponentModel.Composition Import The ...
- ISE 14.7安装教程最新版(Win10安装)——解决Win10安装完后打不开快捷方式的方法
ISE 14.7安装教程最新版(Win10安装) Xilinx ISE是一款世界著名的硬件设计软件,它为设计流程的每一步都提供了直观的生产力增强工具,覆盖从系统级设计探索.软件开发和基于HDL硬件设计 ...
- 使用js跳转手机站url的若干注意点
引子: 去年年底公司开发手机站平台,经历了前期的用户群.市场调查,产品需求分析,产品原型设计,ui前端到程序开发上线测试等等工作,终于上线...此处略去本人作为前端开发的心情. 应该说,我们的手机站平 ...
- adb 常用命令及操作
获取序列号: adb get-serialno 查看连接计算机的设备: adb devices 重启机器: adb reboot 重启到bootloader,即刷机模式: adb reboot boo ...
- Python变量常量及注释
一.变量命名规则1.有字母.数字.下划线搭配组合而成2.不能以数字开头,更不能全为数字3.不能用Python的关键字4.不要太长5.名字要有意义6.不要用中文7.区分大小写8.采用驼峰体命名(多个单词 ...
- 2.azkaban3.0安装
安装规划安装azkban1.安装配置数据库2.下载安装web server3.安装mulit executor4.安装azkaban插件AZKABAN参数安装出现的问题 安装规划 IP 角色 端口 1 ...
- FZU 1844 Earthquake Damage(最大流最小割)
Problem Description Open Source Tools help earthquake researchers stay a step ahead. Many geological ...
- js经典试题之数据类型
js经典试题之数据类型 1:输出"B" + "a" + + "B" + "a"的值: 答案:BaNaNa. 分析:因为+ ...
- Coprime Sequence(前后缀GCD)
Description Do you know what is called ``Coprime Sequence''? That is a sequence consists of $n$ posi ...