~~~题面~~~

题解:

  考场上只想到了找点双,,,,然后不知道怎么处理奇环的问题。

  我们考虑对图取补集,这样两点之间连边就代表它们可以相邻, 那么一个点合法当且仅当有至少一个大小至少为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 圆桌骑士 点双 + 二分图判定的更多相关文章

  1. poj 2942 Knights of the Round Table 圆桌骑士(双连通分量模板题)

    Knights of the Round Table Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 9169   Accep ...

  2. uvalive 3523 Knights of the Round Table 圆桌骑士(强连通+二分图)

    题目真心分析不出来.看了白书才明白,不过有点绕脑. 容易想到,把题目给的不相邻的关系,利用矩阵,反过来建图.既然是全部可行的关系,那么就应该能画出含奇数个点的环.求环即是求双连通分量:找出所有的双连通 ...

  3. POJ2942 UVA1364 Knights of the Round Table 圆桌骑士

    POJ2942 洛谷UVA1364(博主没有翻墙uva实在是太慢了) 以骑士为结点建立无向图,两个骑士间存在边表示两个骑士可以相邻(用邻接矩阵存图,初始化全为1,读入一对憎恨关系就删去一条边即可),则 ...

  4. POJ 2942Knights of the Round Table(tarjan求点双+二分图染色)

    Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 13954   Accepted: 4673 Description Bein ...

  5. UVALive 3523 Knights of the Round Table 圆桌骑士 (无向图点双连通分量)

    由于互相憎恨的骑士不能相邻,把可以相邻的骑士连上无向边,会议要求是奇数,问题就是求不在任意一个简单奇圈上的结点个数. 如果不是二分图,一定存在一个奇圈,同一个双连通分量中其它点一定可以加入奇圈.很明显 ...

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

    题目来源:POJ 2942 Knights of the Round Table 题意:统计多个个骑士不能參加随意一场会议 每场会议必须至少三个人 排成一个圈 而且相邻的人不能有矛盾 题目给出若干个条 ...

  7. [POJ2942]Knights of the Round Table(点双+二分图判定——染色法)

    建补图,是两个不仇恨的骑士连边,如果有环,则可以凑成一桌和谐的打麻将 不能直接缩点,因为直接缩点求的是连通分量,点双缩点只是把环缩起来 普通缩点                             ...

  8. poj2942 Knights of the Round Table,无向图点双联通,二分图判定

    点击打开链接 无向图点双联通.二分图判定 <span style="font-size:18px;">#include <cstdio> #include ...

  9. POJ2942 Knights of the Round Table【Tarjan点双联通分量】【二分图染色】【补图】

    LINK 题目大意 有一群人,其中有一些人之间有矛盾,现在要求选出一些人形成一个环,这个环要满足如下条件: 1.人数大于1 2.总人数是奇数 3.有矛盾的人不能相邻 问有多少人不能和任何人形成任何的环 ...

随机推荐

  1. 封装Excls数据导出功能 返回一个下载链接地址

    /// <summary> /// 获取本地存储地址 /// </summary> /// <param name="dt"></para ...

  2. springboot 常用依赖

    <?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven ...

  3. SSH项目中的困惑之一

    1.request.getContextPath()详解 <%=request.getContextPath()%>是为了解决相对路径的问题,可返回站点的根路径. 但不用也可以,比如< ...

  4. leetcode-最长上升子序列LIS

    转载原文地址:http://www.cnblogs.com/GodA/p/5180560.html 给定一个无序的整数数组,找到其中最长上升子序列的长度. 示例: 输入: [10,9,2,5,3,7, ...

  5. (python)leetcode刷题笔记 02 Add Two Numbers

    2. Add Two Numbers You are given two non-empty linked lists representing two non-negative integers. ...

  6. 《Effective C++》读书笔记 资源管理

    C++程序中最常用的资源包括动态分配的内存,文件描述器,互斥锁,数据库连接,网络socket等等.不论哪种资源,重要的是,当你不再使用他时,必须将他归还给系统. 一个很好的做法是以对象管理资源.把资源 ...

  7. Python基础 之 文件操作

    文件操作 一.路径 文件绝对路径:d:\python.txt 文件相对路径:在IDEA左边的文件夹中 二.编码方式 utf-8 gbk... 三.操作方式 1.只读 r 和 rb 绝对路径的打开操作 ...

  8. linux NULL 的定义

    #undef NULL #if defined(__cplusplus) #define NULL 0 #else #define NULL ((void *)0) #endif

  9. Shell 常用命令、基本用法总结

    Filter Filter 常用于从大量文本.数据中提取需求的部分.下面介绍几个常用的 filter 命令. cut $ cut -c 5-8 textfile.txt # 切出 textfile.t ...

  10. Python中的reload函数

    Python中的import语句可以导入module文件,但是import语句只是第一次导入的时候会执行module文件中的代码,然后就会把导入的模块文件存入到内存,当再次导入的时候,Python是直 ...