Knights of the Round Table(Tarjan+奇圈)
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+奇圈)的更多相关文章
- 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 ...
- 【POJ 2942】Knights of the Round Table(双联通分量+染色判奇环)
[POJ 2942]Knights of the Round Table(双联通分量+染色判奇环) Time Limit: 7000MS Memory Limit: 65536K Total Su ...
- 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 ...
- 【POJ】2942 Knights of the Round Table(双连通分量)
http://poj.org/problem?id=2942 各种逗.... 翻译白书上有:看了白书和网上的标程,学习了..orz. 双连通分量就是先找出割点,然后用个栈在找出割点前维护子树,最后如果 ...
- Knights of the Round Table
Knights of the Round Table Being a knight is a very attractive career: searching for the Holy Grail, ...
- POJ2942 UVA1364 Knights of the Round Table 圆桌骑士
POJ2942 洛谷UVA1364(博主没有翻墙uva实在是太慢了) 以骑士为结点建立无向图,两个骑士间存在边表示两个骑士可以相邻(用邻接矩阵存图,初始化全为1,读入一对憎恨关系就删去一条边即可),则 ...
- POJ 2942 Knights of the Round Table
Knights of the Round Table Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 10911 Acce ...
- poj 2942 Knights of the Round Table 圆桌骑士(双连通分量模板题)
Knights of the Round Table Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 9169 Accep ...
- POJ 2942Knights of the Round Table(tarjan求点双+二分图染色)
Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 13954 Accepted: 4673 Description Bein ...
随机推荐
- Python学习-while循环练习
1.计算1-100的和 i = 1; total = 0; while i <= 100: total = total + i; i = i + 1; print(total); 2.打印出1- ...
- CentOS \Linux文件权限详解
文件和目录权限概述 在linux中的每一个文件或目录都包含有访问权限,这些访问权限决定了谁能访问和如何访问这些文件和目录. 通过设定权限可以从以下三种访问方式限制访问权限:只允许用户自己访问:允许一个 ...
- 网络基础——UDP
UDP 1.UDP首部格式 源端口号(16) 目标端口号(16) UDP长度(16) UDP校验和(16) UDP长度:用来指出UDP的总长度 校验和:用来完成对UDP数据的差错检验,它是UDP协议提 ...
- SERE0014: Illegal HTML character: decimal 154
问题:jmeter,生成报告转化成html,报错SERE0014: Illegal HTML character: decimal 154 原因: 某些字符,特别是控制字符#x7F-#x9F ,在XM ...
- 51nod 1285 山峰和分段
[题解] 枚举n的各个因数作为段长,O(n)判断每一段内是否有山峰即可. #include<cstdio> #include<cstring> #include<algo ...
- java nio--采用Selector实现Socket通信
server: /** * 选择器服务端 * Created by ascend on 2017/6/9 9:30. */ public class SelectorServer { // publi ...
- Butterfly
Butterfly 时间限制:C/C++ 2秒,其他语言4秒空间限制:C/C++ 131072K,其他语言262144K64bit IO Format: %lld 题目描述 给定一个n*m的矩阵,矩阵 ...
- hdu 1185 状压dp 好题 (当前状态与上两行有关系)
/* 状压dp 刚开始&写成&&看了好长时间T0T. 状态转移方程 dp[i][k][j]=Max(dp[i][k][j],dp[i-1][l][k]+num[i][j]);( ...
- poj 1562 简单深搜
//搜八个方向即可 #include<stdio.h> #include<string.h> #define N 200 char ma[N][N]; int n,m,vis[ ...
- git常见操作---由简入深
常用命令 常用指令 ls 显示文件或目录 -l 列出文件详细信息l(list) -a 列出当前目录下所有文件及目录,包括隐藏的a(all) mkdir 创建目录 -p 创建目录,若无父目录,则创建p( ...