圆桌骑士。有的骑士之间是相互憎恨的,不能连坐,需要安排奇数个骑士围着桌子坐着,大于3个,求哪些骑士不可能安排到座位。

根据给定的关系,如果两个骑士之间没有憎恨关系,那么连边。最终就是求有多少个点无法位于奇圈之内。

首先求所有联通分量,对于每个连通分量二分图染色,看看是否存在一个奇圈,如果有一个,那么这个联通分量里面的所有点都可以在至少一个奇圈之内。(详细的见白书)

下面重点说说如何找联通分量的。

方法是用一个栈来维护下面走过的边,如果当前点是割点,那么把在这个点后面加入的边全部退出来,把这些点拿出来,就构成了一个独立的联通分量。

这个维护是很有意思的,因为是递归操作,有点难以理解。

结合下面的图来说:

这也就是题目的样例,如图,假设我们现在走1->2->4,现在2是关键点,于是就把2->4这条边出来,(2,4)两个点构成一个分量。

同理的有3,5。

但是问题也许会是,我一开是走的是1->2->3->5,那么会不会导致(2,3,5)构成一个分量,不会的!因为在你处理完3后,3->5这条边就已经被拿出来了,最终也只有(1,2,3)。这个递归和栈的混合运用是有一点难以理解,好好想想就清楚了。

还有一个问题,2-4并不能看成是一个双联通分量,但是在这个题目里面不会对答案产生影响,因为2个点也无法构成奇圈(3个点呢?嘿嘿不会有3个点的情况)。想想就知道了,加油!

召唤代码君:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#define maxn 2000100
using namespace std; vector<int> bcc[maxn];
int next[maxn],first[],to[maxn],edge;
int low[],d[],belong[],color[];
int a[][];
int n,m,T=,bccnum,ans,u,v,dfs_clock;
int U[maxn],V[maxn],top;
bool can[]; void _init()
{
T++,ans=dfs_clock=bccnum=top=,edge=-;
for (int i=; i<=n; i++) low[i]=d[i]=belong[i]=first[i]=-,can[i]=false;
} void addedge(int uu,int vv)
{
edge++;
to[edge]=vv,next[edge]=first[uu],first[uu]=edge;
edge++;
to[edge]=uu,next[edge]=first[vv],first[vv]=edge;
} bool find(int cur,int tag)
{
for (int i=first[cur]; i!=-; i=next[i])
{
if (belong[to[i]]!=tag) continue;
if (color[to[i]]==color[cur]) return true;
if (color[to[i]]!=-) continue;
color[to[i]]=-color[cur];
if (find(to[i],tag)) return true;
}
return false;
} void dfs(int cur,int fa)
{
low[cur]=d[cur]=++dfs_clock;
for (int i=first[cur]; i!=-; i=next[i])
{
if ((i^)==fa) continue;
if (d[to[i]]==-)
{
U[++top]=cur,V[top]=to[i];
dfs(to[i],i);
low[cur]=min(low[cur],low[to[i]]);
if (low[to[i]]>=d[cur])
{
bcc[++bccnum].clear();
for (;;top--)
{
if (belong[U[top]]!=bccnum) belong[U[top]]=bccnum,bcc[bccnum].push_back(U[top]);
if (belong[V[top]]!=bccnum) belong[V[top]]=bccnum,bcc[bccnum].push_back(V[top]);
if (U[top]==cur && V[top]==to[i])
{
top--;
break;
}
}
for (unsigned j=; j<bcc[bccnum].size(); j++) color[bcc[bccnum][j]]=-;
color[bcc[bccnum][]]=;
if (find(bcc[bccnum][],bccnum))
for (unsigned j=; j<bcc[bccnum].size(); j++) can[bcc[bccnum][j]]=true;
}
}
else low[cur]=min(low[cur],low[to[i]]);
}
} int main()
{
while (scanf("%d%d",&n,&m) && (n|m))
{
_init();
while (m--)
{
scanf("%d%d",&u,&v);
a[u][v]=a[v][u]=T;
}
for (int i=; i<=n; i++)
for (int j=i+; j<=n; j++)
if (a[i][j]!=T) addedge(i,j);
for (int i=; i<=n; i++)
if (d[i]==-) dfs(i,-);
for (int i=; i<=n; i++) if (!can[i]) ans++;
printf("%d\n",ans);
}
return ;
}

UVAlive3523_Knights of the Round Table的更多相关文章

  1. POJ2942 Knights of the Round Table[点双连通分量|二分图染色|补图]

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

  2. POJ 2942 Knights of the Round Table

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

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

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

  4. 【LA3523】 Knights of the Round Table (点双连通分量+染色问题?)

    Being a knight is a very attractive career: searching for the Holy Grail, saving damsels in distress ...

  5. 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 ...

  6. UVALive - 3523 - Knights of the Round Table

    Problem  UVALive - 3523 - Knights of the Round Table Time Limit: 4500 mSec Problem Description Input ...

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

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

  8. 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 ...

  9. 【POJ】2942 Knights of the Round Table(双连通分量)

    http://poj.org/problem?id=2942 各种逗.... 翻译白书上有:看了白书和网上的标程,学习了..orz. 双连通分量就是先找出割点,然后用个栈在找出割点前维护子树,最后如果 ...

随机推荐

  1. 总结一下公司项目使用各种较新的前端技术和 Api 的一些经验。

    关于 ES6: 需要注意 ES6 的一些特性和 API 是需要一个 200k 的 Polyfill 才能得到支持的,特性如 for ... of 循环,generator,API 如 Object.a ...

  2. python通讯录系统

    ---恢复内容开始--- 对于一般的通讯录系统,主要有两个参数:姓名和电话号码,所以可以利用python编程里面的字典来进行建立之间的所属关系, 可以利用以下代码简单实现: print('|--- 欢 ...

  3. java高并发之锁的使用以及原理浅析

    锁像synchronized同步块一样,是一种线程同步机制.让自Java 5开始,java.util.concurrent.locks包提供了另一种方式实现线程同步机制——Lock.那么问题来了既然都 ...

  4. NO--15 微信小程序,scroll-view选项卡和跳转

    大多数的商城类小程序都有这个功能,点击“全部订单”,“待付款”,“待发货”,“待收货”,“已完成”,会跳转页面且跳至与之相对应的选项卡中.所以我们在开发该小程序时也做了相同的功能.如下图:   scr ...

  5. 提高JetBrains软件的性能

    在Java开发中,我用的开发工具是Idea,它是JetBrains公司旗下的产品. 电脑内存较大,但是Idea加载的慢,我们可以通过 \bin 下的 idea64.exe.vmoptions 和 id ...

  6. Cocos2dx源码赏析(2)之渲染

    Cocos2dx源码赏析(2)之渲染 这篇,继续从源码的角度来跟踪下Cocos2dx引擎的渲染过程,以此来梳理下Cocos2dx引擎是如何将精灵等元素显示在屏幕上的. 从上一篇对Cocos2dx启动流 ...

  7. c++虚继承与虚函数

    学习继承与多态时看到这两个概念,记录整理. 虚继承与虚函数都是用virtual关键字实现,虚继承为了防止多重继承,而虚函数为了实现多态. 是几个例子. 虚继承: class A{}; class B: ...

  8. ssh软件及命令的使用

    常用软件安装及使用目录 第1章 ssh常用用法小结 1.1 连接到远程主机: 命令格式 : ssh name@remoteserver 或者 ssh remoteserver -l name 说明:以 ...

  9. [linux] ssh远程执行本地脚本

    1.ssh密钥登录 略 2.免确认机器指纹,ssh -o StrictHostKeyChecking=no [root@XM-v125 ~]# ssh wykai@192.168.0.110 The ...

  10. 插件使用-HighChart

    一.介绍 让数据可视化更简单,兼容 IE6+.完美支持移动端.图表类型丰富.方便快捷的 HTML5 交互性图表库. 官网(英):https://www.highcharts.com/download ...