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 ...
随机推荐
- LeetCode141LinkedListCycle和142LinkedListCycleII
141题:判断链表是不是存在环! // 不能使用额外的存储空间 public boolean hasCycle(ListNode head) { // 如果存在环的 两个指针用不一样的速度 会相遇 L ...
- console.log格式化及console对象
一.console.log格式化打印 console.log格式化这一用法一般都在个人博客或其他官网上有,当F12查看网页元素时,在控制台(console)那里偶尔会发现一些个性化的输出,感觉很奇特很 ...
- [USACO15JAN]Grass Cownoisseur
\(tarjan\)缩点+\(DAG\)上最长路. 求一个以\(1\)为起点的最长路和一个以\(1\)为终点的最长路,然后找那个逆行边就行了. 然后这个我\(RE\)了好久,原因是\(vector\) ...
- 集合:ListIterator
why ? when ? how ? what ? Java 集合框架图 有了 Iterator 为什么还要有 ListIterator 呢? Iterator 遍历的时候如果你想修改集合中的元素怎么 ...
- 洛谷 2966 2966 [USACO09DEC]牛收费路径Cow Toll Paths
[题意概述] 给出一个图,点有正点权,边有正边权,通过两点的代价为两点间的最短路加上路径通过的点的点权最大值. 有M个询问,每次询问通过两点的代价. [题解] 先把点按照点权从小到大排序,然后按照这个 ...
- [bzoj2242][SDOI2011][计算器] (Baby-Step-Giant-Step+快速幂+exgcd)
Description 你被要求设计一个计算器完成以下三项任务: 1.给定y,z,p,计算Y^Z Mod P 的值: 2.给定y,z,p,计算满足xy≡ Z ( mod P )的最小非负整数: 3.给 ...
- String类的转换功能
/* * String类的转换功能 * char[] toCharArray():把字符串转换为字符数组 * String toLowerCase():把字符串转换为小写字符串 * String to ...
- Android layer-list:联合shape(2)
Android layer-list:联合shape(2) 附录文章3简单说明了Android layer-list的用法,现在把Android layer-list联合shape做出一些特殊的 ...
- 开源的多行字符串工具: 在JS中整段地写HTML
这样看来ES6的多行字符模板可能就不需要了-- 通过这个你可以整段整段地在JS中写HTML.SQL了. 示例 之前你得这样写 var str = '' +'<!doctype html>' ...
- J2EE 课件3 JSP标记
•JSP标记包括指令标记.动作标记和自定义标记.其中自定义标记主要讲述与Tag文件有关的Tag标记 1.指令标记page page 指令用来定义整个JSP页面的一些属性和这些属性的值,属性值用单 ...