点的双联通+二分图的判定(poj2942)

Knights of the Round Table
Time Limit: 7000MS   Memory Limit: 65536K
Total Submissions: 10804   Accepted: 3553

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:

  • 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 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
题意:一群骑士要召开圆桌会议,坐在圆桌上的人都有两个邻居
满足两个要求:每个人与身边的邻居都不能有矛盾,且围坐的人数必须是奇数
题目给出m对矛盾关系,问有多少人一定是不能参加会议的
分析:
1.首先做补图,把题中给出的边舍去,把没有给出的边建图
2.然后在图中找奇数环,求点的双连通块
3.在双连通块中找奇数点的环,如果一个联通块中存在一个奇数环,那么该连通块中的每个人都有可能成为一个奇数环中的一员,此时把可能成为一员的人标记
4.找奇数环实际上就是判断该联通块是否是二分图,即用黑白交叉染色法判断,如果某条边的两个端点有相同的颜色,就存在了奇数环
程序:
 #include"stdio.h"
#include"string.h"
#include"stdlib.h"
#include"algorithm"
#include"queue"
#include"math.h"
#include"iostream"
#include"vector"
#define M 1009
#define inf 0x3f3f3f3f
#define eps 1e-9
#define PI acos(-1.0)
#include"map"
#include"vector"
#include"set"
#include"string"
#include"stack"
#define LL __int64
using namespace std;
struct node
{
int u,v,next;
}edge[M*M*];
int t,head[M],dfn[M],low[M],cut[M],belong[M*M*],use[M],color[M],exist[M];
int indx,num;
int G[M][M];
stack<int>q;
vector<int>bian[M];
void init()
{
t=;
memset(head,-,sizeof(head));
}
void add(int u,int v)
{
edge[t].u=u;
edge[t].v=v;
edge[t].next=head[u];
head[u]=t++;
}
int dfs(int u)//交叉染色判断二分图
{
for(int i=;i<(int)bian[u].size();i++)
{
int v=bian[u][i];
if(color[v]==-)
{
color[v]=color[u]^;
int tt=dfs(v);
if(tt)
return ;
}
else if(color[v]==color[u])
return ;
}
return ;
}
void tarjan(int u,int fa,int id)
{
dfn[u]=low[u]=++indx;
for(int i=head[u];~i;i=edge[i].next)
{
int v=edge[i].v;
if(i==(id^))continue;
if(!dfn[v])
{
q.push(i);//把边入栈
tarjan(v,u,i);
low[u]=min(low[u],low[v]);
if(dfn[u]<=low[v])//求割点
{
cut[u]++;
int x;
num++;
int cnt=;
map<int,int>mp;
do//当搜到一个割点u的时候把栈中边出栈,所有出栈的边都属于一个点双连通块
{
x=q.top();
q.pop();
belong[x]=belong[x^]=num;
bian[edge[x].u].push_back(edge[x].v);//建立连通块联通图
bian[edge[x].v].push_back(edge[x].u);
if(!mp[edge[x].u])//离散化点
{
mp[edge[x].u]=;
use[cnt++]=edge[x].u;
}
if(!mp[edge[x].v])
{
mp[edge[x].v]=;
use[cnt++]=edge[x].v;
} }while(i!=x);
color[use[]]=;
if(dfs(use[]))//如果存在奇数环,把点标记
{
for(int i=;i<cnt;i++)
exist[use[i]]=;
}
for(int i=;i<cnt;i++)//清理初始化边和color标记
{
bian[use[i]].clear();
color[use[i]]=-;
}
}
}
else if(low[u]>dfn[v])
{
low[u]=dfn[v];
q.push(i);//把边入栈
}
}
if(fa<)
cut[u]--;
}
int slove(int n)
{
for(int i=;i<=n;i++)
bian[i].clear();
indx=num=;
memset(cut,,sizeof(cut));
memset(dfn,,sizeof(dfn));
memset(use,,sizeof(use));
memset(color,-,sizeof(color));
memset(exist,,sizeof(exist));
for(int i=;i<=n;i++)
{
if(!dfn[i])
tarjan(i,-,-);
}
int ans=;
for(int i=;i<=n;i++)
{
if(!exist[i])
ans++;
}
return ans;
}
int main()
{
int n,m;
while(scanf("%d%d",&n,&m),m||n)
{
init();
memset(G,,sizeof(G));
for(int i=;i<=m;i++)
{
int a,b;
scanf("%d%d",&a,&b);
G[a][b]=G[b][a]=;
}
for(int i=;i<=n;i++)
{
for(int j=i+;j<=n;j++)
{
if(!G[i][j])
{
add(i,j);
add(j,i);
}
}
}
int ans=slove(n);
printf("%d\n",ans);
}
return ;
}
 
上一篇:强大的CImage类


下一篇:C# DateTime 日期加1天 减一天 加一月 减一月 等方法