Knights of the Round Table
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:
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 Hint
Huge input file, 'scanf' recommended to avoid TLE.
Source |
题意:
一群武士 某些武士之间互相仇视 如果在一起会发生斗争事件
参加圆桌会议的条件:
1)圆桌上任意任意两个相邻的武士不能互相仇视
2)同一个圆桌上的武士数量必须是奇数
思路:
将能坐在一起的武士分为一组 将所有的武士分组 得到双连通分量
然后 判断双连通分量中是否存在奇圈 如果存在 则这一组武士都能参与回忆
奇圈的判断方法 用交叉染色判断是否是二分图 二分图中肯定没有奇圈
#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <string>
#include <vector>
#include <queue>
#define MEM(a,x) memset(a,x,sizeof a)
#define eps 1e-8
#define MOD 10009
#define MAXN 1010
#define MAXM 1000010
#define INF 99999999
#define ll __int64
#define bug cout<<"here"<<endl
#define fread freopen("ceshi.txt","r",stdin)
#define fwrite freopen("out.txt","w",stdout)
using namespace std;
void read(int &x)
{
char ch;
x=0;
while(ch=getchar(),ch!=' '&&ch!='\n')
{
x=x*10+ch-'0';
}
}
int n,m,tol,cnt,top;
int head[MAXN],dfn[MAXN],low[MAXN],vis[MAXN],col[MAXN],mark[MAXN],stack[MAXN],odd[MAXN];
int mp[MAXN][MAXN];
struct Edge
{
int from,to,next,vis;
}edge[2*MAXM];
void addedge(int u,int v)
{
edge[tol].to=v; edge[tol].from=u; edge[tol].next=head[u];
edge[tol].vis=0; head[u]=tol++;
edge[tol].to=u; edge[tol].from=v; edge[tol].next=head[v];
edge[tol].vis=0; head[v]=tol++;
}
int find(int u)
{
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].to;
if(mark[v])
{
if(col[v]==-1)
{
col[v]=!col[u];
return find(v);
}
else if(col[v]==col[u]) return 1;
}
}
return 0;
}
void color(int u)
{
MEM(mark,0);
int i;
do
{
i=stack[top--];
mark[edge[i].from]=1;
mark[edge[i].to]=1;
}while(edge[i].from!=u);
MEM(col,-1);
col[u]=0;
if(find(u))
{
for(int j=1;j<=n;j++)
{
if(mark[j])
odd[j]=1;
}
}
}
void dfs(int u)
{
dfn[u]=low[u]=++cnt;
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].to;
if(edge[i].vis) continue;
edge[i].vis=edge[i^1].vis=1;
stack[++top]=i;
if(!dfn[v])
{
dfs(v);
low[u]=min(low[u],low[v]);
if(low[v]>=dfn[u]) color(u);
}
else
{
low[u]=min(low[u],dfn[v]);
}
}
}
int main()
{
// fread;
while(scanf("%d%d",&n,&m)!=EOF)
{
if(n==0&&m==0) break;
MEM(mp,0);
for(int i=0;i<m;i++)
{
int u,v;
scanf("%d%d",&u,&v);
mp[u][v]=mp[v][u]=1;
}
tol=0;
MEM(head,-1);
for(int i=1;i<=n;i++)
{
for(int j=i+1;j<=n;j++)
{
if(mp[i][j]==0)
addedge(i,j);
}
}
MEM(dfn,0);
MEM(odd,0);
cnt=0; top=0;
for(int i=1;i<=n;i++)
{
if(!dfn[i])
dfs(i);
}
int ans=0;
for(int i=1;i<=n;i++)
{
if(!odd[i])
ans++;
}
printf("%d\n",ans);
}
return 0;
}