poj 2942 Knights of the Round Table (点双连通分量求解)

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

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

Hint

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

Source



题意:(贴的别人的啦)

瑟王要在圆桌上召开骑士会议,为了不引发骑士之间的冲突,并且能够让会议的议题有令人满意的结果,每次开会前都必须对出席会议的骑士有如下要求:
1、  相互憎恨的两个骑士不能坐在直接相邻的2个位置;
2、  出席会议的骑士数必须是奇数,这是为了让投票表决议题时都能有结果。
 
如果出现有某些骑士无法出席所有会议(例如这个骑士憎恨所有的其他骑士),则亚瑟王为了世界和平会强制把他剔除出骑士团。
       现在给定准备去开会的骑士数n,再给出m对憎恨对(表示某2个骑士之间使互相憎恨的),问亚瑟王至少要剔除多少个骑士才能顺利召开会议?
 
注意:1、所给出的憎恨关系一定是双向的,不存在单向憎恨关系。
2、由于是圆桌会议,则每个出席的骑士身边必定刚好有2个骑士。即每个骑士的座位两边都必定各有一个骑士。
3、一个骑士无法开会,就是说至少有3个骑士才可能开会。


思路:

根据所给图构出补图,Tarjan求点双连通分量(用栈保存边),然后用交叉染色法判断是否存在奇圈,有一个结论是双连通分量如果存在一个奇圈,那么这个分量中所有的点都能在某一个奇圈中,这样就不必每个点都判断了。


代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define maxn 1005
#define MAXN 1000005
#define mod 1000000007
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 0.000001
typedef long long ll;
using namespace std;

int n,m,ans,cnt,tot,flag,sum;
int dfn[maxn],low[maxn];
int vis[maxn],app[maxn],ok[maxn];
int edge[maxn][maxn];
int lt[maxn][maxn],xx[maxn];
struct Node
{
    int u,v;
} cur,sta[MAXN];

void presolve()
{
    int i,j,t;
    for(i=1; i<=n; i++)
    {
        for(j=1; j<=n; j++)
        {
            edge[i][j]^=1;
        }
    }
}
void dfs(int u,int fa)
{
//   printf("%d->",u);
    int i,j,t,v;
    if(fa) sta[++cnt].u=u,sta[cnt].v=fa;
    low[u]=dfn[u]=++tot;
    for(i=1; i<=n; i++)
    {
        if(!edge[u][i]||u==i) continue ;
        if(vis[i])
        {
            if(i!=fa) low[u]=min(low[u],dfn[i]);
        }
        else
        {
            vis[i]=1;
            dfs(i,u);
            low[u]=min(low[u],low[i]);
            if(dfn[u]<=low[i])
            {
                sum++;
 //               printf("sum:%d\n",sum);
                cur=sta[cnt];
                while(!(cur.u==u&&cur.v==i||cur.u==i&&cur.v==u))
                {
                    lt[sum][cur.u]=lt[sum][cur.v]=1;
//                    printf("%d-%d ",cur.u,cur.v);
                    cnt--;
                    cur=sta[cnt];
                }
                cnt--;
                lt[sum][u]=lt[sum][i]=1;
 //               printf("%d-%d\n",u,i);
            }
        }
    }
}
void color(int u,int c,int d)
{
    if(flag) return ;
    int i,j,t;
    for(i=1; i<=u; i++)
    {
        if(!edge[u][i]||i==u||!app[i]) continue ;
        if(vis[i]!=-1)
        {
            if(vis[i]==c)
            {
                flag=1;
                return ;
            }
        }
        else
        {
            vis[i]=c^1;
            color(i,c^1,d);
        }
    }
}
void solve()
{
    int i,j,t,u,v;
    memset(vis,0,sizeof(vis));
    memset(lt,0,sizeof(lt));
    sum=0;
    for(i=1; i<=n; i++)
    {
        if(!vis[i])
        {
            tot=cnt=0;
            vis[i]=1;
            dfs(i,0);
        }
    }
    memset(ok,0,sizeof(ok));
    for(i=1; i<=sum; i++)
    {
        t=0;
        memset(app,0,sizeof(app));
        for(j=1; j<=n; j++)
        {
            if(lt[i][j])
            {
                t++,app[j]=1;
                xx[t]=j;
            }
        }
//        printf("i:%d t:%d\n",i,t);
        for(j=1; j<=t; j++)
        {
            flag=0;
            memset(vis,-1,sizeof(vis));
            vis[xx[j]]=1;
            color(xx[j],1,i);
            if(flag) break ;
        }
        if(flag)
        {
            for(j=1; j<=t; j++)
            {
                ok[xx[j]]=1;
            }
        }
    }
    ans=0;
    for(i=1; i<=n; i++)
    {
        ans+=ok[i];
//        printf("i:%d ok[i]:%d\n",i,ok[i]);
    }
    ans=n-ans;
}
int main()
{
    int i,j,t,u,v,w;
    while(scanf("%d%d",&n,&m),n|m)
    {
        memset(edge,0,sizeof(edge));
        for(i=1; i<=m; i++)
        {
            scanf("%d%d",&u,&v);
            edge[u][v]=edge[v][u]=1;
        }
        presolve();  // 求补图
        solve();
        printf("%d\n",ans);
    }
    return 0;
}
/*
5 8
3 1
3 5
5 5
4 2
5 3
3 3
4 3
3 4
ans: 1
*/





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值