思路:

对于该图,直接用建图貌似没法解,所以也很容易想到建补图,这样存在边的两个点就能再圆桌上做一起。也就将问题转化为对双连通分量中是否存在奇圈了。

我们将每次查询的边保存在stack中,当遇到关键点的时候,stack里面保存的就是一个连通分量。在该连通分量中进行深搜,每次标记一个与父节点相反的颜色。当某次子节点与父节点颜色相同,那么就存在奇圈,且该连通分量中所有的点都在奇圈中。将这些点标记,最后进行遍历就行了。

引用discuss里的话:

一个块若无法做二分图染色,势必存在一个长度为奇数的环
任找一个奇环C,则对于任意一个非环上的点A,一定有两条不相交的路,连向这个奇环,交奇环于两个不同的点P、Q(否则这就不是一个双连通分量)
那么在环C上有两条P-->Q的路径,一条经过奇数条边,一条经过偶数条边
其中一条同PA、AQ相连后,一定是个奇环,所以A一定也在一个奇环上

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#define Maxn 1010
#define Maxm Maxn*Maxn
using namespace std;
int index[Maxn],vi[Maxn],dfn[Maxn],col[Maxn],low[Maxn],map[Maxn][Maxn],e,n,lab=,stack[Maxm],top,odd[Maxn];
void init()
{
memset(index,-,sizeof(index));
memset(vi,,sizeof(vi));
memset(map,,sizeof(map));
memset(col,,sizeof(col));
memset(low,,sizeof(low));
memset(dfn,,sizeof(dfn));
memset(odd,,sizeof(odd));
e=lab=top=;
}
struct Edge{
int from,to,next,v;
}edge[Maxm];
void addedge(int from, int to)
{
edge[e].v=;
edge[e].from=from;
edge[e].to=to;
edge[e].next=index[from];
index[from]=e++;
edge[e].v=;
edge[e].to=from;
edge[e].from=to;
edge[e].next=index[to];
index[to]=e++;
}
int find(int u)
{
int i,j,temp;
for(i=index[u];i!=-;i=edge[i].next)
{
temp=edge[i].to;
if(vi[temp])
{
if(col[temp]==-)
{
col[temp]=!col[u];
if(find(temp))//寻找奇圈
return ;
}
else
if(col[temp]==col[u]) return ;
}
}
return ;
}
int color(int u)
{
memset(col,-,sizeof(col));
memset(vi,,sizeof(vi));
col[u]=;
int i;
do{//将该连通分量进行标记
i=stack[--top];
vi[edge[i].from]=;
vi[edge[i].to]=;
}
while(edge[i].from!=u);
if(find(u))//如果找到就进行标记
{
for(i=;i<=n;i++)
{
if(vi[i])
odd[i]=;
}
}
return ;
}
int dfs(int u)
{
dfn[u]=low[u]=++lab;
int i,j,temp;
for(i=index[u];i!=-;i=edge[i].next)
{
temp=edge[i].to;
if(edge[i].v) continue;//一开始没加这个判断,一直WA
edge[i].v=edge[i^].v=;
stack[top++]=i;
if(!dfn[temp])
{
dfs(temp);
if(low[temp]>=dfn[u]) color(u);
low[u]=min(low[u],low[temp]);
}
low[u]=min(low[u],dfn[temp]);
}
return ;
}
int main()
{
int m,i,j,a,b;
while(scanf("%d%d",&n,&m),n||m)
{
init();
for(i=;i<=m;i++)
{
scanf("%d%d",&a,&b);
map[a][b]=map[b][a]=;
}
for(i=;i<=n;i++)
{
for(j=i+;j<=n;j++)
{
if(map[i][j])
addedge(i,j);
}
}
for(i=;i<=n;i++)
if(!dfn[i])
dfs(i);
int ans=;
for(i=;i<=n;i++)
if(!odd[i])
ans++;
printf("%d\n",ans);
}
return ;
}

poj 2942 点的双连通分量的更多相关文章

  1. POJ 3352 无向图边双连通分量,缩点,无重边

    为什么写这道题还是因为昨天多校的第二题,是道图论,HDU 4612. 当时拿到题目的时候就知道是道模版题,但是苦于图论太弱.模版都太水,居然找不到. 虽然比赛的时候最后水过了,但是那个模版看的还是一知 ...

  2. Redundant Paths POJ - 3177(边—双连通分量)

    题意: 在图中加边 看最少能通过加多少条边把 图变成边—双连通分量 解析: 先做一次dfs,不同的连通分量的low是不同的  注意重边 缩点 统计度为1的点  那么需要加的边为(ret+1)/2 #i ...

  3. poj 2942 求点双联通+二分图判断奇偶环+交叉染色法判断二分图

    http://blog.csdn.net/lyy289065406/article/details/6756821 http://www.cnblogs.com/wuyiqi/archive/2011 ...

  4. poj 3694 Network(双连通分量)

    题目:http://poj.org/problem?id=3694 #include <iostream> #include <cstring> #include <cs ...

  5. poj 1144 Network【双连通分量求割点总数】

    Network Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 11042   Accepted: 5100 Descript ...

  6. poj 2942 Knights of the Round Table(点双连通分量+二分图判定)

    题目链接:http://poj.org/problem?id=2942 题意:n个骑士要举行圆桌会议,但是有些骑士相互仇视,必须满足以下两个条件才能举行: (1)任何两个互相仇视的骑士不能相邻,每个骑 ...

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

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

  8. POJ 2942 Knights of the Round Table 黑白着色+点双连通分量

    题目来源:POJ 2942 Knights of the Round Table 题意:统计多个个骑士不能參加随意一场会议 每场会议必须至少三个人 排成一个圈 而且相邻的人不能有矛盾 题目给出若干个条 ...

  9. 【POJ 2942】Knights of the Round Table(点双连通分量,二分图染色)

    圆桌会议必须满足:奇数个人参与,相邻的不能是敌人(敌人关系是无向边). 求无论如何都不能参加会议的骑士个数.只需求哪些骑士是可以参加的. 我们求原图的补图:只要不是敌人的两个人就连边. 在补图的一个奇 ...

随机推荐

  1. iOS 开发的9个超有用小技巧

    http://www.jianshu.com/p/221507eb8590 1.如何快速的查看一段代码的执行时间. 1 2 #define TICK   NSDate *startTime = [NS ...

  2. CodeForces 455C Civilization (并查集+树的直径)

    Civilization 题目链接: http://acm.hust.edu.cn/vjudge/contest/121334#problem/B Description Andrew plays a ...

  3. HDU 1312 http://acm.hdu.edu.cn/showproblem.php?pid=1312

    #include<stdio.h> #include<string.h> #include<math.h> #include<stdlib.h> #de ...

  4. codeforces 630C Lucky Numbers

    C. Lucky Numbers time limit per test 0.5 seconds memory limit per test 64 megabytes input standard i ...

  5. 使用Unity制作游戏关卡的教程(一)

    转自: http://gamerboom.com/archives/74131 作者:Matthias Zarzecki 我正在制作<Looking For Group – The Fork O ...

  6. PostgreSQL的 initdb 源代码分析之三

    继续 其实接前面,整个while循环是这样的: ) { switch (c) { ...... } ...... } 这一句,c = getopt_long(argc, argv, "dD: ...

  7. SASS优化响应式断点管理

    前端开发whqet,csdn,王海庆,whqet,前端开发专家 原文:<Managing Responsive Breakpoints with Sass> 作者:Hugo Giraude ...

  8. PS常见错误-无法完成请求,因为文件格式模块不能解析该文件

    无法完成请求,因为文件格式模块不能解析该文件 将图片格式变成.jpg格式就可以了

  9. Codeforces Round #278 (Div. 1) A. Fight the Monster 暴力

    A. Fight the Monster Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/487/ ...

  10. Codeforces Gym 100002 Problem F "Folding" 区间DP

    Problem F "Folding" Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/ ...