http://poj.org/problem?id=2942

题意:n个武士,某些武士之间相互仇视,如果在一起容易发生争斗事件。所以他们只有满足一定的条件才能参加圆桌会议:(1)相互仇视的两个武士不能相邻 (2)同一个圆桌边上的武士数量必须是奇数。输出需要剔除的武士人数。
   / \    思路:根据图中的关系建立该图的补图,(Tarjan算法)求出图中的双连通分量,判断每个双连通分量中是否存在奇圈,若存在奇圈则该连通分量中的武士都符合条件,否则都不符合        |.|  判断奇圈的方法:由于二分图中不含奇圈,可判断连通分量是否为二分图,若是,则不含奇圈,否则存在奇圈。。
   |.|
   |:|   __    
  ,_|:|_,  / )
   (Oo  / _I_
   +\ \ ||^ ^|
     \ \||_0→  
      \ /.:.\-\
      |.:. /-----\
      |___|::oOo::|
      /  |:<_T_>:|(无意中发现的武士字符画。。哈哈)
    ""....""

 #include <stdio.h>
#include <string.h>
#include <algorithm>
#include <vector>
#include <stack>
using namespace std;
const int N=;
struct Edge
{
int u,v;
Edge(int u,int v):u(u),v(v) {}
};
int low[N],dfn[N],iscut[N],bridge[N][N];
int color[N],odd[N],bccno[N],map[N][N];
int dfs_clock,bcc_cnt,n,m;
vector<int>G[N],bcc[N];
stack<Edge>S; void init()
{
bcc_cnt = ;
dfs_clock = ;
while(!S.empty()) S.pop();
memset(low,,sizeof(low));
memset(dfn,,sizeof(dfn));
memset(iscut,,sizeof(iscut));
memset(bridge,,sizeof(bridge));
memset(bccno,,sizeof(bccno));
memset(odd,,sizeof(odd));
memset(map,,sizeof(map));
for (int i = ; i < N; i++)
{
G[i].clear();
bcc[i].clear();
}
}
void dfs(int u,int father)//Tarjan
{
low[u]=dfn[u]=++dfs_clock;
int child = ;
for (int i = ; i < G[u].size(); i++)
{
int v = G[u][i];
Edge e(u,v);
if (!dfn[v])
{
S.push(e);
child++;
dfs(v,u);
low[u] = min(low[u],low[v]);
if(dfn[u] <= low[v])
{
iscut[u] = ;//u为割点
++bcc_cnt;
while()
{
Edge x = S.top();
S.pop();
if(bccno[x.u]!=bcc_cnt)
{
bccno[x.u] = bcc_cnt;//点u属于第bcc_cnt个连通分量
bcc[bcc_cnt].push_back(x.u);
}
if (bccno[x.v]!=bcc_cnt)
{
bccno[x.v] = bcc_cnt;
bcc[bcc_cnt].push_back(x.v);
}
if (x.u==u&&x.v==v)
break;
}
}
if (low[v] > dfn[u]) bridge[u][v] = ;//u-v之间为桥
}
else if (dfn[v]<dfn[u]&&v!=father)
{
S.push(e);
low[u] = min(low[u],dfn[v]);
}
}
if (father<&&child==)
iscut[u]=;
}
bool bipartite(int u,int b)//判断二分图(交叉染色法)
{
for (int i = ; i < G[u].size(); i++)
{
int v = G[u][i];
if (bccno[v]!=b) continue;
if (color[u]==color[v]) return false;
if (!color[v])
{
color[v] = -color[u];
if (!bipartite(v,b))
return false;
}
}
return true;
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
if (n==&&m==)
break;
int u,v;
init();
for (int i = ; i < m; i++)
{
scanf("%d%d",&u,&v);
--u;
--v;
map[u][v]=map[v][u]=;
}
for (int i = ; i < n; i++)
{
for (int j = i+; j <n; j++)
{
if (!map[i][j])
{
G[i].push_back(j);//建立无向的补图
G[j].push_back(i);
}
}
}
for (int i = ; i <n; i++)
{
if (!dfn[i])
dfs(i,-);
}
for (int i = ; i <= bcc_cnt; i++)
{
memset(color,,sizeof(color));
for (int j = ; j < bcc[i].size(); j++)
{
bccno[bcc[i][j]] = i;
}
int u = bcc[i][];
color[u] = ;
if(!bipartite(u,i))
{
for (int j = ; j < bcc[i].size(); j++)
{
odd[bcc[i][j]] =;//标记奇圈中的点
}
}
}
int ret = ;
for (int i = ; i < n; i++)
{
if (!odd[i])
ret++;
}
printf("%d\n",ret);
}
return ;
}

Knights of the Round Table(Tarjan+奇圈)的更多相关文章

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

  2. 【POJ 2942】Knights of the Round Table(双联通分量+染色判奇环)

    [POJ 2942]Knights of the Round Table(双联通分量+染色判奇环) Time Limit: 7000MS   Memory Limit: 65536K Total Su ...

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

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

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

  5. Knights of the Round Table

    Knights of the Round Table Being a knight is a very attractive career: searching for the Holy Grail, ...

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

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

  7. POJ 2942 Knights of the Round Table

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

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

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

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

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

随机推荐

  1. 一只小蜜蜂(hdoj 2044,动态规划递推)

    Problem Description 有一只经过训练的蜜蜂只能爬向右侧相邻的蜂房,不能反向爬行.请编程计算蜜蜂从蜂房a爬到蜂房b的可能路线数.其中,蜂房的结构如下所示. Input 输入数据的第一行 ...

  2. 洛谷——P2007 魔方

    P2007 魔方 常神牛家的魔方都是3*3*3的三阶魔方,大家都见过. 模拟即可: #include<iostream> #include<cstdio> #include&l ...

  3. Scrapy用Cookie实现模拟登录

    模拟登录是爬取某些站点内容的一个关键,有些网站(特别是论坛类),不登录的话,一个数据也拿不到. 模拟登录有这样几个关键: 弄清楚登录的url一些网站打开出现登录的页面,地址栏大多数不是登录提交表单的u ...

  4. buf.writeUInt8()函数详解

    buf.writeUInt8(value, offset[, noAssert]) value {Number} 需要被写入到 Buffer 的字节 offset {Number} 0 <= o ...

  5. 解决windows安装TensorFlow2.0beta版本时ERROR: Cannot uninstall 'wrapt'问题

    pip install -U --ignore-installed wrapt enum34 simplejson netaddr 参考:https://bugs.launchpad.net/rall ...

  6. reading/writing files in Python

    file types: plaintext files, such as .txt .py Binary files, such as .docx, .pdf, iamges, spreadsheet ...

  7. Spring核心技术(一)——IoC容器和Bean简介

    IoC容器和Bean简介 这章包括了Spring框架对于IoC规则的实现.Ioc也同DI(依赖注入).而对象是通过构造函数,工厂方法,或者一些Set方法来定义对象之间的依赖的.容器在创建这些Bean对 ...

  8. Delphi语法

    类与对象 从用户角度考虑,用户并不需要了解面向对象编程的知识,就可编写Delphi应用程序.当用户在建立新窗体.添加新组件以及处理事件时,大部分相关代码会由Delphi自动产生.但是,知道语言及其细节 ...

  9. Java并发编程:线程池 - 实例

    代码块: public class test { public static void main(String[] args) { test t = new test(); ThreadPoolExe ...

  10. H5存储------localStorage和sessionStorage

    web现在随着计算机的高速发展,客户端干的事情越来越多了,随着事情的增多,很多东西存储就不止在服务器了,本地存储越来越强大了(大神原谅我废话了