这道题是一道关于点双连通分量的题,不过在求出点双连通分量时,要判定是否分量中每个点都属于一个奇圈。求奇圈可以通过二分图判定的方法,对节点进行染色,如果遇到染相同颜色的点,则说明找到一个奇圈。此外,要解决这道题还需要知道一个性质:如果一个点双连通分量中有一个奇圈,那么所有的点都会包含在奇圈中(但不一定是同一个奇圈)。这个性质好像网上没有证明,所以就直接用,不管为什么了。如果大家发现好的证明,或知道哪里有证明,希望大家告诉我。
题意比较直白,所以就不多说了,求个补图,再求点双连通分量就好了。不过要注意,求点双连通分量与求割点的差异,判断割点要分两种情况,而求点双连通分量则只有一种情况。因为不论搜索树的根节点u是否为割点,都会属于至少一个点双连通分量(如果u是割点,则可能属于好几个点双连通分量,如果u不是割点,则只属于一个点双连通分量)。
参考博文:http://blog.sina.com.cn/s/blog_64675f540100s4nv.html,http://blog.csdn.net/lenleaves/article/details/8977900。
代码(C++):
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define MAXp 1009
#define MAXe 1000009
using namespace std;
//#define LOCAL
struct Edge{
int v;
int next;
} edge[MAXe*2];
int head[MAXp],dfn[MAXp],low[MAXp],ts,c,stk[MAXp],r,s,in[MAXp],cnt,color[MAXp],n;
bool map[MAXp][MAXp],flag[MAXp];;
void addEdge(int u,int v)
{
edge[c].v=v;
edge[c].next=head[u];
head[u]=c;
c++;
}
bool paint(int u,int y,int pre)
{
int i,v;
color[u]=y;
for(i=head[u];i!=-1;i=edge[i].next)
{
v=edge[i].v;
if(in[v]==1&&v!=pre)
{
if(color[v]==-1)
{
if(paint(v,color[u]^1,u)) return true;
}else if(color[u]==color[v]) return true;
}
}
return false;
}
void dfs(int u,int pre)
{
int i,j,v;
dfn[u]=low[u]=++ts;
stk[++s]=u;
for(i=head[u];i!=-1;i=edge[i].next)
{
v=edge[i].v;
if(!dfn[v])
{
dfs(v,u);
low[u]=min(low[u],low[v]);
if(dfn[u]<=low[v]) //求点双连通分量时,不必按照u是否为搜索的根分类讨论
{
memset(in,0,sizeof(in));
in[u]=1;
while(stk[s]!=v)
{
in[stk[s]]=1;
s--;
}
in[v]=1;
s--;
memset(color,-1,sizeof(color));
if(paint(u,1,-1))
{
for(j=1;j<=n;j++) if(in[j]==1) flag[j]=true;
}
}
}else if(v!=pre) low[u]=min(low[u],dfn[v]);
}
}
int main(int argc, char *argv[])
{
#ifdef LOCAL
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
int m,u,v,i,j,ans;
while(scanf("%d %d",&n,&m)&&n+m!=0)
{
memset(map,false,sizeof(map));
for(i=0;i<m;i++)
{
scanf("%d %d",&u,&v);
map[u][v]=map[v][u]=true;
}
//求补图
c=0;
memset(head,-1,sizeof(head));
for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
if(map[i][j]==false)
{
addEdge(i,j);
addEdge(j,i);
}
}
}
ts=0;
s=-1;
memset(dfn,0,sizeof(dfn));
memset(flag,false,sizeof(flag));
for(i=1;i<=n;i++)
{
if(!dfn[i]) dfs(i,-1);
}
ans=0;
for(i=1;i<=n;i++)
{
if(flag[i]==false) ans++;
}
printf("%d\n",ans);
}
system("PAUSE");
return EXIT_SUCCESS;
}
题目( http://poj.org/problem?id=2942):
Knights of the Round Table
Time Limit: 7000MS | Memory Limit: 65536K | |
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.