POJ 2942 Tarjan双联通分量+二分图 解题报告

Knights of the Round Table

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" andno” 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

【解题报告】
来自:http://blog.sina.com.cn/s/blog_d760eb2e0101dod3.html
题意:一个国家有N(1<=n<=1000)个圆桌骑士,该国有一种会议叫圆桌会议,即由奇数个(>1)骑士围成圆桌开会,已知M对禁止关系(a,b)表示编号为ab的两名圆桌骑士不能相邻坐,询问该国有多少名骑士无论如何安排座位都可能参加任何一场会议。
题解:对于本题询问无论如何都不可能参加任何会议的骑士数量,而给出的是禁止相邻的列表,不太容易处理,所以我们做第一步转化,做出一个无向图,两名骑士之间存在边当且仅当这两名骑士可以相邻坐,所以一名骑士能参与圆桌会议的条件就是此图过这名骑士存在一个不重复经过一点的奇环,此处有一个结论,若存在一个奇环,则所有与此奇环存在公共边的环上的点都可选(此结论的证明不复杂,大家可自行脑补- -)。所以如果存在一个奇环,那么此与此奇环相关的点双联通分量都是可选的点,于是就产生了一个自然的思路,用tarjan算法依次求出原图的所有点双联通分量,然后在每个分量中判断是否存在奇环(可用二染色判定的方法),若有奇环则标记此分量中所有点为可选,答案就是所有不可选点的数量。

代码如下:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define N 1010
#define M 1000010

int n,m,i,j,k,l;
int head[N],next[M],list[M],tot,case_n;
int dfn[N],low[N],stack[N],stack_tot,_index,bfn[N],bfn_tot,color[N];
int aa[N][N],ans[N];

void add(int a,int b)
{
   tot++;
   list[tot]=b;
   next[tot]=head[a];
   head[a]=tot;
}
bool dfs(int x,int c)
{
   color[x]=c;
   for(int i=head[x];i;i=next[i])
   if(bfn[list[i]]==bfn_tot)
   {
       if(color[list[i]]==c) return false;
       else if(color[list[i]]!=(c^1)&&dfs(list[i],c^1)==false) return false;
   }
   return true;
}
void colour(int x)
{
   ans[x]=1;bfn[x]=-1;
   for(int i=head[x];i;i=next[i])
   if(bfn[list[i]]==bfn_tot) colour(list[i]);
   return;
}
int tarjan(int x,int fa)
{
   int i,j,k;
   dfn[x]=low[x]=++_index;
   stack[++stack_tot]=x;
   for(int i=head[x];i;i=next[i])
   if(i!=fa)
   {
       if(!dfn[list[i]])
       {
           low[x]=min(low[x],tarjan(list[i],i^1));
           if(low[list[i]]>=dfn[x])
           {
               bfn_tot++;
               while(stack[stack_tot+1]!=list[i])
               bfn[stack[stack_tot--]]=bfn_tot;
               bfn[stack[stack_tot]]=bfn_tot;
               bfn[x]=bfn_tot;
               if(!dfs(x,x<<1))colour(x);
           }
       }
       else low[x]=min(low[x],dfn[list[i]]);
   }
   return low[x];
}
int main()
{
   while(scanf("%d%d",&n,&m),n||m)
   {
        tot=1;_index=stack_tot=bfn_tot=0;
        memset(bfn,0,sizeof(bfn));
        memset(head,0,sizeof(head));
        memset(dfn,0,sizeof(dfn));
        memset(color,0,sizeof(color));
        memset(ans,0,sizeof(ans));
        case_n++;
        for(i=1;i<=m;i++)
        {
            scanf("%d%d",&j,&k);
            aa[j][k]=case_n;
            aa[k][j]=case_n;
        }
        for(i=1;i<=n;i++)
        for(j=i+1;j<=n;j++)
        if(aa[i][j]!=case_n)
        {
            add(i,j);
            add(j,i);
        }
        for(i=1;i<=n;i++)
        if(!dfn[i])tarjan(i,0);
        k=0;
        for(i=1;i<=n;i++)
        if(!ans[i]||!head[i])k++;
        printf("%d\n",k);
   }
   return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值