poj2942Knights of the Round Table

Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
13110572105      Log Out
Mail:2(1)
Login Log      Archive
Language:
Knights of the Round Table
Time Limit: 7000MS Memory Limit: 65536K
Total Submissions: 9766 Accepted: 3170

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

[Submit]   [Go Back]   [Status]    [Discuss]

Home Page  Go Back  To top

 

给出一个无向图,然后计算出该图的补图,然后再计算补图中不再任一奇圈内的点的个数。

首先要求出所有的点的双连通分量,然后判断每一个双连通分量中是否存在奇圈,若有,则该双连通分量中的任意一点均在某一个奇圈上。

求解双连通分量的方法:

设 rv[ ] 表示每个点在DFS时被访问的次序,low[ ] 表示节点 u 及其子孙所连接到点中 rv[ ] 最小的值。

首先需要一个辅助栈来存放每一个双连通分量的边。


若边( u , v )仅符合下面两种情况的任意一种则将边压入栈中:

        1,v 尚未被访问。

        2,rv[ v ] <= low[u] 且 v 正在等待回溯。


若当 u 的 子节点 v 回溯完成时有 low[v] >= rv[u],则说明 u 是一个割点,此时要将栈中的在(u , v )及在其后面压入的边弹出,这些边就组成了一个点的双连通分量。


判断奇圈可以用BFS染色法。可见判断二分图的方法

 

书上学习二分图的一个例题。

代码:

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <stack>
#include <vector>
using namespace std;
const int maxe=1010;
const int maxv=10e5+10;
struct Edge
{
    int u;
    int v;
};
int n,m,odd[maxv],color[maxv];
int a[maxe][maxe],pre[maxe],bccno[maxe],dfs_clock,bcc_cnt;
bool iscut[maxe];
vector<int> g[maxe],bcc[maxe];
stack<Edge> s;
int dfs(int u,int fa)
{
    int lowu=pre[u]=++dfs_clock;
    int child=0;
    for(int i=0; i<g[u].size(); i++)
    {
        int v=g[u][i];
        Edge e=(Edge)
        {
            u,v
        };
        if(!pre[v])
        {
            s.push(e);
            child++;
            int lowv=dfs(v,u);
            lowu=min(lowu,lowv);
            if(lowv>=pre[u])
            {
                iscut[u]=1;
                bcc_cnt++;
                bcc[bcc_cnt].clear();
                for(;;)
                {
                    Edge x=s.top();
                    s.pop();
                    if(bccno[x.u]!=bcc_cnt)
                    {
                        bcc[bcc_cnt].push_back(x.u);
                        bccno[x.u]=bcc_cnt;
                    }
                    if(bccno[x.v]!=bcc_cnt)
                    {
                        bcc[bcc_cnt].push_back(x.v);
                        bccno[x.v]=bcc_cnt;
                    }
                    if(x.u==u&&x.v==v)
                        break;
                }
            }
        }
        else if(pre[v]<pre[u]&&v!=fa)
        {
            s.push(e);
            lowu=min(lowu,pre[v]);
        }
    }
    if(fa<0&&child==1)
        iscut[u]=0;
    return lowu;
}
void find_bcc(int n)
{
    memset(pre,0,sizeof(pre));
    memset(iscut,0,sizeof(iscut));
    memset(bccno,0,sizeof(bccno));
    dfs_clock=bcc_cnt=0;
    for(int i=0; i<n; i++)
        if(!pre[i])
            dfs(i,-1);
}
bool bipartite(int u,int b)
{
    for(int i=0; i<g[u].size(); i++)
    {
        int v=g[u][i];
        if(bccno[v]!=b)
            continue;
        if(color[v]==color[u])
            return false;
        if(!color[v])
        {
            color[v]=3-color[u];
            if(!bipartite(v,b))
                return false;
        }
    }
    return true;
}
int main()
{
    while(scanf("%d %d",&n,&m)&&(n||m))
    {
        for(int i=0; i<n; i++)
            g[i].clear();
        memset(a,0,sizeof(a));
        for(int i=0; i<m; i++)
        {
            int ita,itb;
            scanf("%d%d",&ita,&itb);
            ita--;
            itb--;
            a[ita][itb]=a[itb][ita]=1;
        }
        for(int u=0; u<n; u++)
            for(int v=u+1; v<n; v++)
                if(!a[u][v])
                {
                    g[u].push_back(v);
                    g[v].push_back(u);
                }
        find_bcc(n);
        memset(odd,0,sizeof(odd));
        for(int i=1; i<=bcc_cnt; i++)
        {
            memset(color,0,sizeof(color));
            for(int j=0; j<bcc[i].size(); j++)
                bccno[bcc[i][j]]=i;
            int u=bcc[i][0];
            color[u]=1;
            if(!bipartite(u,i))
                for(int j=0; j<bcc[i].size(); j++)
                    odd[bcc[i][j]]=1;
        }
        int ans=n;
        for(int i=0; i<n; i++)
            if(odd[i])
                ans--;
        printf("%d\n",ans);
    }
    return 0;
}



 


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值