~~~题面~~~

题解:

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

  我们考虑对图取补集,这样两点之间连边就代表它们可以相邻, 那么一个点合法当且仅当有至少一个大小至少为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. 如果看懂git -help

    每一个git 命令,都可以git * --help 打开git 的网页去看详细内容,也可以git * -help 在当前命令行里面看. 如下: zy@caoxinyu MINGW64 /f/git/i ...

  2. 13、Java并发编程:线程池的使用

    Java并发编程:线程池的使用 在前面的文章中,我们使用线程的时候就去创建一个线程,这样实现起来非常简便,但是就会有一个问题: 如果并发的线程数量很多,并且每个线程都是执行一个时间很短的任务就结束了, ...

  3. CakePHP Model中( 获取Session)使用Component的方法

    有时候我们需要在Model中使用Session,大家知道CakePHP把操作Session的方法封装为了一个Component, 在Model中正常读取Session的方法: 在 "app_ ...

  4. 「国庆训练」Kingdom of Obsession(HDU-5943)

    题意 给定\(s,n\),把\(s+1,s+2,...,s+n\)这\(n\)个数填到\(1,2,...,n\)里,要求\(x\)只能填到\(x\)的因子的位置(即题目中\(x\%y=0\)那么x才能 ...

  5. Objective-C description方法 SEL类型

    description方法 #import "Person.h" @implementation Person - (void) setAge : (int) age { _age ...

  6. Django学习总结①

    Django基础环境配置好以后,打开pycharm,创建Django项目 视图views 中需要导入 django.http ---> HttpResponse models库 - 常用方法: ...

  7. 解决ssh_exchange_identification:read connection reset by peer 原因

    服务器改了密码,试过密码多次后出现: ssh_exchange_identification: read: Connection reset by peer 可以通过ssh -v查看连接时详情 Ope ...

  8. 【上传-下载】-jmeter工具

    上 传 ================================================================================================ ...

  9. CSP201609-1:最大波动

    引言:CSP(http://www.cspro.org/lead/application/ccf/login.jsp)是由中国计算机学会(CCF)发起的"计算机职业资格认证"考试, ...

  10. poj 2155 (二维树状数组 区间修改 求某点值)

    Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 33682   Accepted: 12194 Descript ...