SP2878 KNIGHTS - Knights of the Round Table(点双联通分量+奇环判断模板题)

传送门

题目描述
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 is there, 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 round table, 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 it can 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.

输入格式
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 k _{1}
1

and k _{2}
2

, which means that knight number k _{1}
1

and knight number k _{2}
2

hate each other (the numbers k _{1}
1

and k _{2}
2

are between 1 and n).

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

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

题意翻译
题目大意

有n个骑士经常举行圆桌会议,商讨大事。每次圆桌会议至少有3个骑士参加,且相互憎恨的骑士不能坐在圆桌的相邻位置。如果发生意见分歧,则需要举手表决,因此参加会议的骑士数目必须是大于1的奇数,以防止赞同和反对票一样多。知道那些骑士相互憎恨之后,你的任务是统计有多少骑士不可能参加任何一个会议。

输入数据

包含多组数据,每组数据格式如下: 第一行为两个整数n和m(1<=n<=1000, 1<=m<=10^6)。 以下m行每行包含两个整数k1和k2(1<=k1,k2<=n),表示骑士k1和k2相互憎恨。 输入结束标记为n=m=0

输出数据

对于每组数据,在一行中输出一个整数,即无法参加任何会议的骑士个数。

#include <bits/stdc++.h>
#define inf 0x7fffffff
#define ll long long
//#define int long long
//#define double long double
#define eps 1e-5
//#define mod 1e9+7
using namespace std;
const int mod=1e9+7;
const int N=1e3+5;
struct node
{
	int ver,next;
}e[N*N];
struct cp
{
	int vc,nc;
}ec[N*N];
int tc,hc[N*N];
int tot,head[N*N];
int n,m;
int hate[N][N];
vector < int > dcc[N];
int root;
int now;
int low[N],dfn[N];
int instack[N];
int cnt,top,num;
int cut[N];
int v[N],vis[N];
int new_id[N];
int flag1;
int fail[N];
void add(int x,int y)
{
	e[++tot].ver=y;
	e[tot].next=head[x];
	head[x]=tot;
}
void add_c(int x,int y)
{
	ec[++tc].vc=y;
	ec[tc].nc=hc[x];
	hc[x]=tc;
}
void init()
{
	memset(head,0,sizeof(head));
	tot=0;
	memset(hate,0,sizeof(hate));
	memset(e,0,sizeof(e));
	for(int i=0;i<=N;i++)  dcc[i].clear(); 
	memset(cut,0,sizeof(cut));
	cnt=0;
	memset(dfn,0,sizeof(dfn));
	memset(low,0,sizeof(low));
	memset(fail,0,sizeof(fail)); 
	num=0;
	top=0;
	memset(instack,0,sizeof(instack));
//	memset(new_id,0,sizeof(new_id));
//	memset(ec,0,sizeof(ec));
//	memset(hc,0,sizeof(hc));
//	tc=0;
}
void tarjan(int x)
{
	dfn[x]=low[x]=++num;
	instack[++top]=x;
	int flag=0;
	if(x==root&&!head[x])
	{
		dcc[++cnt].push_back(x);
		return;
	}
	for(int i=head[x];i;i=e[i].next)
	{
		int y=e[i].ver;
		if(!dfn[y])
		{
			tarjan(y);
			low[x]=min(low[x],low[y]);
			if(low[y]>=dfn[x])
			{
				flag++;
				if(x!=root||flag>1)	cut[x]=1;
				cnt++;
				int z;
				do
				{
					z=instack[top--];
					dcc[cnt].push_back(z);
				}while(y!=z);
				dcc[cnt].push_back(x);
			}
		}
		else low[x]=min(low[x],dfn[y]);
	}
}
void dfs(int x,int color)
{
    v[x]=color;
    if(flag1)  return;
    for(int i=head[x];i;i=e[i].next)
    {
        int y=e[i].ver;
        if(vis[y]!=now)  continue;
     	if(v[y]==v[x]&&v[y])
     	{
     	 	flag1=1;
     	    return;
     	}
     	if(!v[y])  dfs(y,3-color);
    }
}
signed main()
{
//	ios::sync_with_stdio(false);
    while(scanf("%d%d",&n,&m)&&n&&m)
    {
    	init();
    	int ans=0;
    	for(int i=1;i<=m;i++)
    	{
    		int x,y;
    		scanf("%d%d",&x,&y);
    		if(x==y)  continue;
    		hate[x][y]=hate[y][x]=1;
    	}
    	for(int i=1;i<n;i++)
    	    for(int j=i+1;j<=n;j++)
    	        if(!hate[i][j])
    	        {
    	        	add(i,j);
    	        	add(j,i);
    	        }
    	for(int i=1;i<=n;i++)
    	    if(!dfn[i])
    	    {
    	    	root=i;
    	    	tarjan(i);
    	    }
    	for(int i=1;i<=cnt;i++)
    	{
    		now=i;
    		memset(vis,0,sizeof(vis));
    		memset(v,0,sizeof(v));
    		for(int j=0;j<dcc[i].size();j++)
    		{
    			int x=dcc[i][j];
    			vis[x]=now;
    			v[x]=0;
    		}
    		flag1=0;
    		if(dcc[i].size())
    		{
    			int x=dcc[i][0];
    			dfs(x,1);
    		}
    		if(flag1)
    		    for(int j=0;j<dcc[i].size();j++)
    		    {
    		    	int x=dcc[i][j];
    		    	fail[x]=1;
    		    }
    	}
    	for(int i=1;i<=n;i++)
    	    if(!fail[i]) ans++;
		printf("%d\n",ans); 
//    	for(int i=1;i<=n;i++)
//    	    if(cut[i])  new_id[i]=++num;
//    	
//    	tc=1;
//    	for(int i=1;i<=cnt;i++)
//    	    for(int j=0;j<dcc[i].size();j++)
//    	    {
//    	    	int x=dcc[i][j];
//    	    	int y=new_id[x];
//    	    	if(cut[x])
//    	    	{
//    	    		add_c(i,y);
//    	    		add_c(y,i);
//    	    	}
//    	    	else c[x]=i;
//    	    }
//    	for(int i=1;i<=n;i++)
//    	{
//    		if(!v[i])  dfs(i,1);
//    	}
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值