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. Humidex POJ - 3299 (数学)

    题目大意 给定你三个变量中的两个输出剩下的那一个 题解 没有什么,就是把公式推出来即可,完全的数学题 代码 #include <iostream> #include <cmath&g ...

  2. Linux学习笔记记录(二)

  3. Python学习第二阶段,Day2,import导入模块方法和内部原理

    怎样导入模块和导入包?? 1.模块定义:代码越来越多的时候,所有代码放在一个py文件无法维护.而将代码拆分成多个py文件,同一个名字的变量互不影响,模块本质上是一个.py文件或者".py&q ...

  4. 创建sum求多元素的和

    a = [1, 2, 3] b = [4, 5, 6] def sum_super(* args): s = 0 for i in args: s += sum(i) return s # print ...

  5. DS26C31M和DS26C32AM

    DS26C31M是产生差分信号的芯片,实际使用的时候,测得在5V供电的情况下,输出+和输出-的压差是5V其中输出+和输入的特性相同. DS26C32AM是接收差分信号的芯片,与DS26C31M可以向配 ...

  6. vue axios请求超时,设置重新请求的完美解决方法

    //在main.js设置全局的请求次数,请求的间隙 axios.defaults.retry = 4; axios.defaults.retryDelay = 1000; axios.intercep ...

  7. 一、ECharts简介

    ECharts,缩写来自Enterprise Charts,商业级数据图表,一个纯Javascript的图表库,可以流畅的运行在PC和移动设备上,兼容当前绝大部分浏览器(IE6/7/8/9/10/11 ...

  8. 48. spring boot单元测试restfull API【从零开始学Spring Boot】

    回顾并详细说明一下在在之前章节中的中使用的@Controller.@RestController.@RequestMapping注解.如果您对Spring MVC不熟悉并且还没有尝试过快速入门案例,建 ...

  9. android中listview点击事件的监听实现

    listview_bookmark.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public vo ...

  10. kfk: async disk IO深度解析

    http://www.itpub.net/thread-1724044-1-1.html