poj 2942 Knights of the Round Table(边双连通分量)

来源:互联网 发布:csgo处理器优化 编辑:程序博客网 时间:2024/05/18 01:47

Knights of the Round Table
Time Limit: 7000MS Memory Limit: 65536KTotal Submissions: 10346 Accepted: 3403

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 51 41 52 53 44 50 0

Sample Output

2

Hint

Huge input file, 'scanf' recommended to avoid TLE. 

Source

Central Europe 2005

题意:

一群武士 某些武士之间互相仇视 如果在一起会发生斗争事件

参加圆桌会议的条件:

1)圆桌上任意任意两个相邻的武士不能互相仇视

2)同一个圆桌上的武士数量必须是奇数


思路:

将能坐在一起的武士分为一组  将所有的武士分组 得到双连通分量

然后 判断双连通分量中是否存在奇圈  如果存在 则这一组武士都能参与回忆

奇圈的判断方法  用交叉染色判断是否是二分图 二分图中肯定没有奇圈

#include <cstdio>#include <iostream>#include <cstring>#include <cmath>#include <algorithm>#include <string.h>#include <string>#include <vector>#include <queue>#define MEM(a,x) memset(a,x,sizeof a)#define eps 1e-8#define MOD 10009#define MAXN 1010#define MAXM 1000010#define INF 99999999#define ll __int64#define bug cout<<"here"<<endl#define fread freopen("ceshi.txt","r",stdin)#define fwrite freopen("out.txt","w",stdout)using namespace std;void read(int &x){    char ch;    x=0;    while(ch=getchar(),ch!=' '&&ch!='\n')    {        x=x*10+ch-'0';    }}int n,m,tol,cnt,top;int head[MAXN],dfn[MAXN],low[MAXN],vis[MAXN],col[MAXN],mark[MAXN],stack[MAXN],odd[MAXN];int mp[MAXN][MAXN];struct Edge{    int from,to,next,vis;}edge[2*MAXM];void addedge(int u,int v){    edge[tol].to=v; edge[tol].from=u; edge[tol].next=head[u];    edge[tol].vis=0; head[u]=tol++;    edge[tol].to=u; edge[tol].from=v; edge[tol].next=head[v];    edge[tol].vis=0; head[v]=tol++;}int find(int u){    for(int i=head[u];i!=-1;i=edge[i].next)    {        int v=edge[i].to;        if(mark[v])        {            if(col[v]==-1)            {                col[v]=!col[u];                return find(v);            }            else if(col[v]==col[u])  return 1;        }    }    return 0;}void color(int u){    MEM(mark,0);    int i;    do    {        i=stack[top--];        mark[edge[i].from]=1;        mark[edge[i].to]=1;    }while(edge[i].from!=u);    MEM(col,-1);    col[u]=0;    if(find(u))    {        for(int j=1;j<=n;j++)        {            if(mark[j])                odd[j]=1;        }    }}void dfs(int u){    dfn[u]=low[u]=++cnt;    for(int i=head[u];i!=-1;i=edge[i].next)    {        int v=edge[i].to;        if(edge[i].vis)  continue;        edge[i].vis=edge[i^1].vis=1;        stack[++top]=i;        if(!dfn[v])        {            dfs(v);            low[u]=min(low[u],low[v]);            if(low[v]>=dfn[u])  color(u);        }        else        {            low[u]=min(low[u],dfn[v]);        }    }}int main(){//    fread;    while(scanf("%d%d",&n,&m)!=EOF)    {        if(n==0&&m==0)  break;        MEM(mp,0);        for(int i=0;i<m;i++)        {            int u,v;            scanf("%d%d",&u,&v);            mp[u][v]=mp[v][u]=1;        }        tol=0;        MEM(head,-1);        for(int i=1;i<=n;i++)        {            for(int j=i+1;j<=n;j++)            {                if(mp[i][j]==0)                    addedge(i,j);            }        }        MEM(dfn,0);        MEM(odd,0);        cnt=0; top=0;        for(int i=1;i<=n;i++)        {            if(!dfn[i])                dfs(i);        }        int ans=0;        for(int i=1;i<=n;i++)        {            if(!odd[i])                ans++;        }        printf("%d\n",ans);    }    return 0;}




0 0
原创粉丝点击