Knights of the Round Table
Time Limit: 7000MS | Memory Limit: 65536K | |
Total Submissions: 10122 | Accepted: 3305 |
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:
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.)
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 .
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.
题解:将不相互讨厌的人建边,问题就转换为求哪些人不在奇数环上?
对于一个点双联通分量来说,假设存在奇数环,那么整个点双联通分量中的点一定都在奇数环上。反之若点双联通分量不存在奇数环,则整个点双连通分量的点都不在奇数环上。
所以我们求出点双联通分量以后,判断这个点联通分量是否存在奇数环即可。判断一个图有没有奇数环,就是判断该图是否为二分图,方法很多,我是直接进行二染色判断的。
代码如下:
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<map>
#include<math.h>
#include<string.h>
#include<string>
#define nn 1100
#define inff 0x3fffffff
typedef long long LL;
using namespace std;
int n,m;
struct node
{
int st,en,next;
}E[nn*nn*2];
int p[nn],num;
void init()
{
memset(p,-1,sizeof(p));
num=0;
}
void add(int st,int en)
{
E[num].st=st;
E[num].en=en;
E[num].next=p[st];
p[st]=num++;
}
bool hate[nn][nn];
int dfn[nn],low[nn];
int df;
stack<int>sta;
vector<int>tu[nn];
queue<int>que;
int cl[nn];
bool ans[nn];
bool go(int id)
{
int i,w;
bool re=true;
for(i=0;i<(int)tu[id].size();i=i++)
{
w=tu[id][i];
if(cl[w]==-1)
{
cl[w]=1-cl[id];
re=re&&go(w);
}
else if(cl[w]==cl[id])
return false;
}
return re;
}
void dfs(int id,int fa)
{
dfn[id]=low[id]=++df;
int i,w;
for(i=p[id];i+1;i=E[i].next)
{
w=E[i].en;
if(w==fa)
continue;
if(dfn[w]==-1)
{
sta.push(i);
dfs(w,id);
low[id]=min(low[id],low[w]);
if(low[w]>dfn[id])
sta.pop();
else if(low[w]>=dfn[id])
{
int ix;
do
{
ix=sta.top();
sta.pop();
cl[E[ix].st]=-1;
cl[E[ix].en]=-1;
tu[E[ix].st].push_back(E[ix].en);
tu[E[ix].en].push_back(E[ix].st);
que.push(ix);
}
while(ix!=i);
cl[id]=0;
if(!go(id))
{
while(que.size())
{
ix=que.front();
que.pop();
tu[E[ix].st].clear();
tu[E[ix].en].clear();
ans[E[ix].st]=true;
ans[E[ix].en]=true;
}
}
else
{
while(que.size())
{
ix=que.front();
que.pop();
tu[E[ix].st].clear();
tu[E[ix].en].clear();
}
}
}
}
else if(dfn[w]<dfn[id])
{
low[id]=min(low[id],dfn[w]);
sta.push(i);
}
}
}
void solve()
{
memset(dfn,-1,sizeof(dfn));
df=0;
int i;
for(i=1;i<=n;i++)
{
if(dfn[i]==-1)
{
dfs(i,i);
}
}
int re=0;
for(i=1;i<=n;i++)
{
if(!ans[i])
re++;
}
printf("%d\n",re);
}
int main()
{
int i,j;
int u,v;
while(scanf("%d%d",&n,&m)&&n+m)
{
memset(hate,false,sizeof(hate));
for(i=1;i<=m;i++)
{
scanf("%d%d",&u,&v);
hate[u][v]=hate[v][u]=true;
}
memset(ans,false,sizeof(ans));
init();
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(i==j)
continue;
if(!hate[i][j])
{
add(i,j);
}
}
}
solve();
}
return 0;
}