Time Limit: 7000MS | Memory Limit: 65536K | |
Total Submissions: 12439 | Accepted: 4126 |
Description
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 is terminated by a block with n = m = 0 .
Output
Sample Input
5 5
1 4
1 5
2 5
3 4
4 5
0 0
Sample Output
2
Hint
Source
求点双连通分量,然后每个分量二分图染色,如果不是二分图,则一定可以构造出奇环经过每个点
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#define C(x) memset(x,0,sizeof(x))
using namespace std;
const int N=,M=1e6+;
inline int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
return x*f;
}
int n=,m,u,v,g[N][N];
struct edge{
int v,ne;
}e[M<<];
int h[N],cnt=;
inline void ins(int u,int v){
cnt++;
e[cnt].v=v;e[cnt].ne=h[u];h[u]=cnt;
cnt++;
e[cnt].v=u;e[cnt].ne=h[v];h[v]=cnt;
}
void buildGraph(){
memset(h,,sizeof(h)); cnt=;
for(int i=;i<=n;i++)
for(int j=i+;j<=n;j++) if(!g[i][j]) ins(i,j);
}
struct data{
int u,v;
data(int a=,int b=):u(a),v(b){}
}st[M<<];
int top=;
int dfn[N],low[N],iscut[N],belong[N],dfc=,bcc=;
int col[N],ok[N];
bool color(int u,int id){//printf("%d %d %d\n",u,col[u],id);
for(int i=h[u];i;i=e[i].ne){
int v=e[i].v;
if(belong[v]!=id) continue;
if(col[v]==col[u]) return ;//first do this
if(!col[v]){
col[v]=-col[u];
if(!color(v,id)) return ;
}
}
return ;
}
void dfs(int u,int fa){
dfn[u]=low[u]=++dfc;
int child=;
for(int i=h[u];i;i=e[i].ne){
int v=e[i].v;
if(!dfn[v]){
st[++top]=data(u,v);
child++;
dfs(v,u);
low[u]=min(low[u],low[v]);
if(low[v]>=dfn[u]){
iscut[u]=;
bcc++;
while(true){
int tu=st[top].u,tv=st[top--].v;
if(belong[tu]!=bcc) belong[tu]=bcc;
if(belong[tv]!=bcc) belong[tv]=bcc;
if(tu==u&&tv==v) break;
}
col[u]=;
if(!color(u,bcc))
for(int i=;i<=n;i++) if(belong[i]==bcc) ok[i]=;
col[u]=;//for cut vertex
}
}else if(dfn[v]<dfn[u]&&v!=fa){
st[++top]=data(u,v);//notice!!!
low[u]=min(low[u],dfn[v]);
}
}
if(child==&&fa==) iscut[u]=;
}
void BCC(){
dfc=bcc=;top=;
C(dfn);C(low);C(iscut);C(belong);
C(col);C(ok);
for(int i=;i<=n;i++) if(!dfn[i]) dfs(i,);
}
int main(){
while(scanf("%d%d",&n,&m)!=EOF&&(n||m)){
memset(g,,sizeof(g));
for(int i=;i<=m;i++){u=read();v=read();g[u][v]=g[v][u]=;}
buildGraph();
BCC();
int ans=;
for(int i=;i<=n;i++) if(!ok[i]) ans++;
printf("%d\n",ans);
}
}