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. 40条常见的移动端Web页面问题解决方案

    1.安卓浏览器看背景图片,有些设备会模糊.想让图片在手机里显示更为清晰,必须使用2x的背景图来代替img标签(一般情况都是用2倍).例如一个div的宽高是100100,背景图必须得200200,然后b ...

  2. C++ string使用

    在c语言里,我们使用一个字符串时,是通过字符数组或者字符指针的方式来进行使用,在C++里,标准模板库已经给我们提供了string类型(string是以类的方式提供给我们使用). 定义和初始化strin ...

  3. C语言输入一行整数(OJ输入格式)

    就是说输入一行用空格隔开的函数,可是它没说用回车符结束,所以一定要用EOF了 第一种方法: ; char ch; do { scanf("%ld",&a[++t]); } ...

  4. Gym - 101670B Pond Cascade(CTU Open Contest 2017 贪心,二分)

    题目: The cascade of water slides has been installed in the park recently and it has to be tested. The ...

  5. 集合:ListIterator

    why ? when ? how ? what ? Java 集合框架图 有了 Iterator 为什么还要有 ListIterator 呢? Iterator 遍历的时候如果你想修改集合中的元素怎么 ...

  6. 关于vuex自己理解的三幅图

  7. AC自动机模板浅讲

    Description 给你N个单词,然后给定一个字符串,问一共有多少单词在这个字符串中出现过(输入相同的字符串算不同的单词,同一个单词重复出现只计一次). Input 第一行一个整数N,表示给定单词 ...

  8. UVA - 10870 UVA - 10870

    Problem ARecurrencesInput: standard inputOutput: standard output Consider recurrent functions of the ...

  9. sql语句的字段转成Date

    ms_sql:convert(datetime,'2010-11-13')cast('2017-01-01' as datetime) Oracle:to_date('2017-01-01') mys ...

  10. [K/3Cloud]关于数据库sa密码更改,管理中心登录不上的问题。

    有时候可能应为别的原因可能一不小心更改了数据库的密码,导致K/3 Cloud管理中心和单据打不开. 这个时候其实只要在注册一下就能解决了,在浏览器中输入http://192.168.25.35:800 ...