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 1n1000 and 1m1000000 . 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 numberk1 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
思路:只要存在奇环的点都符合条件,因此,求出点双连通分量,确定有环,再使用二分图判断是否为奇环就可以了。
有一点要注意,求二分图判奇环标记,要使用在线算法,离线个人认为不行,(也许行吧,就是根点处理不好)
AC码
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<stack>
using namespace std;
const int mm=1009;
const int mn=3e6+9;
class node
{
public:int u,v,next,vis;
}e[mn];
int head[mm],edge;
bool mark[mn];
int dfn[mm],low[mm],dfs_clock,bcc_no,n,m;
int corlor[mm],vis_clock;
int stak[mn],top;
bool vis[mm],g[mm][mm];
void data()
{
memset(head,-1,sizeof(head));
memset(vis,0,sizeof(vis));
memset(g,0,sizeof(g));
edge=0;top=0;
}
void add(int u,int v)
{ e[edge].v=v;e[edge].u=u;e[edge].vis=0;e[edge].next=head[u];head[u]=edge++;
}
bool two_graph(int u,int tim)
{ int v,ret=1;
for(int i=head[u];~i;i=e[i].next)
{ v=e[i].v;
if(!mark[v])continue;
// printf("%d -> %d ",u,v);
if(corlor[v]==-1)
{
corlor[v]=3-corlor[u];
ret=two_graph(v,tim);
}
else if(corlor[u]==corlor[v])ret=0;
if(!ret)return 0;
}
return ret;
}
void get_corlor(int u,int tim)
{
memset(mark,0,sizeof(mark));
int z;
do
{
z=stak[top--];
mark[e[z].v]=mark[e[z].u]=1;
//if(e[z].vis!=tim)puts("my god");
// printf("%d d %d ",e[z].v,e[z].u);
//puts("");
}while(e[z].u^u);
memset(corlor,-1,sizeof(corlor));
corlor[u]=1;//printf("%d ",u);
if(!two_graph(u,tim))
{ //puts("yes");
for(int i=1;i<=n;++i)
if(mark[i])vis[i]=1;
}
// puts("");
}
void get_cut(int u)
{
dfn[u]=low[u]=++dfs_clock;
int v=-1;
for(int i=head[u];~i;i=e[i].next)
{
if(e[i].vis)continue;
v=e[i].v;
e[i].vis=e[i^1].vis=vis_clock;
///printf("%d $ %d\n",u,v);
stak[++top]=i;
if(!dfn[v])
{ get_cut(v);
low[u]=min(low[u],low[v]);
if(low[v]>=dfn[u])
{ get_corlor(u,vis_clock);
//puts("");
++vis_clock;
}
}
else if(dfn[v]<dfn[u])
{
low[u]=min(low[u],dfn[v]);
}
}
}
void bcc_find()
{ memset(dfn,0,sizeof(dfn));
vis_clock=1;top=dfs_clock=0;
for(int i=1;i<=n;++i)
if(!dfn[i])
get_cut(i);
}
int main()
{ int a,b;
//freopen("data.txt","r",stdin);
while(~scanf("%d%d",&n,&m))
{ if(n==0&&m==0)break;
data();
for(int i=0;i<m;++i)
{
scanf("%d%d",&a,&b);g[a][b]=g[b][a]=1;
}
//puts("+++++");
for(int i=1;i<=n;++i)
for(int j=i+1;j<=n;++j)
if(!g[i][j])add(i,j),add(j,i)/**,printf("%d %d\n",i,j)*/;
///puts("++++");
memset(vis,0,sizeof(vis));
bcc_find();
int ans=n;
for(int i=1;i<=n;++i)
if(vis[i])--ans;
printf("%d\n",ans);
}
return 0;
}
/**
*/
离线算法。WA
/**#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<stack>
using namespace std;
const int mm=1009;
const int mn=3e6+9;
int head[mm],next[mn],ver[mn],edge;
bool has[mn];
int dfn[mm],low[mm],dfs_clock,bcc_no,n,m;
int p_bcc[mm],corlor[mm];
bool vis[mm],g[mm][mm];
vector<int>bcc[mm];
class node
{
public:int u,v;
};
stack<int> S;
bool iscut[mm];
void data()
{
memset(head,-1,sizeof(head));
memset(corlor,0,sizeof(corlor));
memset(vis,0,sizeof(vis));
memset(g,0,sizeof(g));
memset(has,0,sizeof(has));
edge=0;
}
void add(int u,int v)
{
ver[edge]=v;next[edge]=head[u];head[u]=edge++;
}
int get_cut(int u,int fa)
{
dfn[u]=low[u]=++dfs_clock;
int child=0,v=-1;
for(int i=head[u];~i;i=next[i])
{
v=ver[i];
if(!dfn[v])
{ S.push(v);++child;
///未访问
low[u]=min(low[u],get_cut(v,u));
//cout<<"kk:"<<u<<" "<<v<<endl;
//cout<<"nn:"<<dfn[u]<<" "<<low[v]<<endl;
if(low[v]>=dfn[u])
{
iscut[u]=1;++bcc_no;
//cout<<u<<" "<<v<<endl;
//cout<<"bcc"<<bcc_no<<endl;
puts("=====");
if(u==0)
{ while(1)
{v=S.top();S.pop();
if(v==0){S.push(v);break;}
}
continue;
}
while(1)
{
v=S.top();S.pop();
if(u==v){S.push(v);break;}
if(p_bcc[v]^bcc_no)
{///bcc[bcc_no].push_back(e.u);
p_bcc[v]=bcc_no;
}
cout<<p_bcc[v]<<" "<<v<<endl;
//cout<<"v="<<v<<endl;
}
puts("====");
}
}
else if(dfn[v]<dfn[u]&&v!=fa)
{
low[u]=min(low[u],dfn[v]);
}
}
if(fa<0&&child==1)iscut[u]=0;///根非割点
return low[u];
}
bool two_graph(int u,int _bcc)
{ int v;
for(int i=head[u];~i;i=next[i])
{ v=ver[i];
if(v<=0||v>n)continue;///超级源
if(p_bcc[v]^_bcc)continue;
if(corlor[v]==corlor[u])return 0;
corlor[v]=3-corlor[u];
if(!two_graph(v,_bcc))return 0;
}
return 1;
}
void dfs(int x)
{ vis[x]=1;///cout<<x<<" ";
for(int i=head[x];~i;i=next[i])
{
if(!vis[ver[i]])
dfs(ver[i]);
}
}
void bcc_find()
{ memset(dfn,0,sizeof(dfn));
memset(iscut,0,sizeof(iscut));
memset(p_bcc,0,sizeof(p_bcc));
bcc_no=dfs_clock=0;
memset(vis,0,sizeof(vis));
int ret=0;
for(int i=1;i<=n;++i)
if(!vis[i]){++ret;dfs(i);add(0,i),add(i,0);/*printf("%d ll\n",i);*/}
/// printf("the number is %d \n",ret);
for(int i=0;i<=n;++i)
if(!dfn[i])
get_cut(i,-1);
}
int main()
{ int a,b;
freopen("data.txt","r",stdin);
while(~scanf("%d%d",&n,&m))
{ if(n==0&&m==0)break;
data();
for(int i=0;i<m;++i)
{
scanf("%d%d",&a,&b);g[a][b]=g[b][a]=1;
}
// puts("--------------");
for(int i=1;i<=n;++i)
for(int j=i+1;j<=n;++j)
if(!g[i][j])add(i,j),add(j,i)/*,printf("%d %d\n",i,j)*/;
//puts("+++++++++++++++++");
bcc_find();
for(int i=0;i<=n;++i)bcc[i].clear();
bool flag=0;
for(int i=1;i<=n;++i)
{ //if(p_bcc[0]==p_bcc[i])flag=1;
bcc[p_bcc[i]].push_back(i);
}
for(int i=0;i<=n;++i)
cout<<i<<" "<<p_bcc[i]<<endl;
for(int i=0;i<=n;++i)
if(iscut[i])cout<<" c "<<i;cout<<endl;
memset(vis,0,sizeof(vis));
for(int i=1;i<=bcc_no;++i)
{ //if(!flag&&i==bcc_no)continue;
int z=bcc[i].size();
/** cout<<i<<" bcc";
for(int j=0;j<z;++j)
cout<<" "<<bcc[i][j];puts("");*/
if(!two_graph(bcc[i][0],i))
{// printf("%d yes\n",i);
for(int j=0;j<z;++j)
{if(p_bcc[bcc[i][j]]==i)
vis[bcc[i][j]]=1;
}
}
}
int ans=n;
for(int i=1;i<=n;++i)
if(vis[i])--ans;
printf("%d\n",ans);
}
return 0;
}
*/