POJ2942 Knights of the Round Table 双连通分量+二分图+奇圈判断

有n个骑士,其中有m对关系恶劣

现在开会要坐在圆桌上面,可以做很多张圆桌,但是关系恶劣的两人不能坐在相邻位置,现在问你有多少个骑士不能参加会议。将关系不恶劣的其实连边

首先这道题目为什么是双联通分量而不是边双联通,因为圆桌,是一个环,每两个点之间存在两条(或以上)点不重复的路径,因此是一个双联通分量,一个联通分量重的骑士,可能组成一个圆桌会议。

至于一个联通分量的中的骑士能不能组成一个圆桌,则是二分图的知识,首先我们知道二分图的意义。一条边的两个端点能够染上不同的颜色,而由于题目中要我们找奇圈,可以知在奇圈中有一条边两端点颜色相同,这就破坏了二分图性质。因此如果一个联通分量是二分图则无法组成圆桌会议。否则能够组成圆桌会议。

至于其中那些点能够参加会议呢?并不是所有联通分量中的点都在奇圈中的,这是我们要注意的,首先我们了解到联通分量重有几圈,因此我们要从中找圈上的点


Knights of the Round Table
Time Limit: 7000MS Memory Limit: 65536K
Total Submissions: 7684 Accepted: 2387

Description

Being a knight is a very attractive career: searching for the Holy Grail, saving damsels in distress, and drinking with the other knights are fun things to do. Therefore, it is not very surprising that in recent years the kingdom of King Arthur has experienced an unprecedented increase in the number of knights. There are so many knights now, that it is very rare that every Knight of the Round Table can come at the same time to Camelot and sit around the round table; usually only a small group of the knights isthere, while the rest are busy doing heroic deeds around the country. 

Knights can easily get over-excited during discussions-especially after a couple of drinks. After some unfortunate accidents, King Arthur asked the famous wizard Merlin to make sure that in the future no fights break out between the knights. After studying the problem carefully, Merlin realized that the fights can only be prevented if the knights are seated according to the following two rules:
  • The knights should be seated such that two knights who hate each other should not be neighbors at the table. (Merlin has a list that says who hates whom.) The knights are sitting around a roundtable, thus every knight has exactly two neighbors.
  • An odd number of knights should sit around the table. This ensures that if the knights cannot agree on something, then they can settle the issue by voting. (If the number of knights is even, then itcan happen that ``yes" and ``no" have the same number of votes, and the argument goes on.)
Merlin will let the knights sit down only if these two rules are satisfied, otherwise he cancels the meeting. (If only one knight shows up, then the meeting is canceled as well, as one person cannot sit around a table.) Merlin realized that this means that there can be knights who cannot be part of any seating arrangements that respect these rules, and these knights will never be able to sit at the Round Table (one such case is if a knight hates every other knight, but there are many other possible reasons). If a knight cannot sit at the Round Table, then he cannot be a member of the Knights of the Round Table and must be expelled from the order. These knights have to be transferred to a less-prestigious order, such as the Knights of the Square Table, the Knights of the Octagonal Table, or the Knights of the Banana-Shaped Table. To help Merlin, you have to write a program that will determine the number of knights that must be expelled. 

Input

The input contains several blocks of test cases. Each case begins with a line containing two integers 1 ≤ n ≤ 1000 and 1 ≤ m ≤ 1000000 . The number n is the number of knights. The next m lines describe which knight hates which knight. Each of these m lines contains two integers k1 and k2 , which means that knight number k1 and knight number k2 hate each other (the numbers k1 and k2 are between 1 and n ). 

The input is terminated by a block with n = m = 0 . 

Output

For each test case you have to output a single integer on a separate line: the number of knights that have to be expelled. 

Sample Input

5 5
1 4
1 5
2 5
3 4
4 5
0 0

Sample Output

2

#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
#include<cstdio>

using namespace std;

#define MAXN 2100
#define MAXM 2001000

struct node
{
    int to,next;
}edge[MAXM];

bool maps[MAXN][MAXN];
int head[MAXN],en;
int n,m;
int vis[MAXN],dfn[MAXN],low[MAXN],color[MAXN];
int stack[MAXN],top;
bool in[MAXN],tag[MAXN];

void add(int a,int b)
{
    edge[en].to=b;
    edge[en].next=head[a];
    head[a]=en++;
    edge[en].to=a;
    edge[en].next=head[b];
    head[b]=en++;
}

bool func(int pre,int u)
{
	color[u]=1;
	bool ok=0;
	for(int i=head[u];i!=-1;i=edge[i].next)
	{
		int v=edge[i].to;
		if(in[v])
		{
			if(color[v]==1)
			{
				if(v!=pre)
					ok=true;
			}
			else
			{
				if(func(u,v))
					ok=true;
			}
		}
	}
	if(ok)
		tag[u]=1;
	return ok;
}

bool bipartite(int u,int c)
{
    color[u]=c;
    for(int i=head[u];i!=-1;i=edge[i].next)
    {
        int v=edge[i].to;
        if(in[v])
		{
		    if(color[v]==0)
		    {
                if(!bipartite(v,-c))
                    return false;
		    }
			else
				if(color[v]==color[u])
					return false;
		}
    }
    return true;
}

void find(int u,int fat)
{
	memset(in,0,sizeof(in));
	in[fat]=1,in[u]=1;
	while(stack[top-1]!=u)
	{
		in[stack[top-1]]=1;
		top--;
	}
	memset(color,0,sizeof(color));
	if(!bipartite(u,1))
	{
		memset(color,0,sizeof(color));
		func(fat,fat);
	}

}


void dfs(int u,int fat,int dis)
{
    stack[top++]=u;
    dfn[u]=low[u]=dis;
    vis[u]=1;
    for(int i=head[u];i!=-1;i=edge[i].next)
    {
        int v=edge[i].to;
        if(!vis[v])
        {
            dfs(v,u,dis+1);
            low[u]=min(low[u],low[v]);
            if(low[v]>=dfn[u]) find(v,u);
        }
        else
           low[u]=min(low[u],dfn[v]);
    }
}

void tarjan()
{
    top=0;
    memset(vis,0,sizeof(vis));
    for(int i=1;i<=n;i++)
		if(!vis[i]) dfs(i,-1,1);
}

void solve()
{
    int x,y;
    memset(maps,0,sizeof(maps));
    memset(tag,0,sizeof(tag));
    for(int i=1;i<=m;i++)
    {
        scanf("%d%d",&x,&y);
        maps[x][y]=maps[y][x]=1;
    }
    memset(head,-1,sizeof(head)),en=0;
    for(int i=1;i<=n;i++)
        for(int j=i+1;j<=n;j++)
            if(!maps[i][j]) add(i,j);
     tarjan();
     int ans=n;
     for(int i=1;i<=n;i++)
     {
		if(tag[i]) ans--;
	 }
	 printf("%d\n",ans);

}


int main()
{
    while(~scanf("%d%d",&n,&m)&& n+m )
        solve();
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值