POJ2942 Knights of the Round Table(点双联通分量+二分图染色+补图)

题目链接
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

题目大意:
N个骑士要在圆桌上开会,现在有M对骑士是相互讨厌的,开会的时候相互讨厌的骑士不能相邻而坐。规定至少人数为3且为奇数的圆桌才能开会。现在询问我们至少有多少个骑士不能出席任何会议。

解题思路:
emm 一开始以为是求割点, 实际上错的太离谱了 。 貌似很多带限制边的问题都是利用补图来操作的qaq 。
我们建立出一个相互不讨厌的骑士关系无向图,求出所有点双连通分量后,每个分量里面的人是可以坐在一个桌子的,现在还需要判断是否为奇环。如果为奇环,说明他们可以坐在一起开会,不必离开。最终统计答案即可。

#include <iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<stack>
#include<algorithm>
using namespace std;
#define maxn 1005
struct Edge
{
    int u,v;
};
int pre[maxn],iscut[maxn],bccno[maxn],dfs_clock,bcc_cnt;//pre[]表示开始时间 bccno[]表示某点所在集合号 bcc_cnt表示编号
vector<int>G[maxn],bcc[maxn];
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++)//遍历与u点相连接的边
    {
        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);//用后代的low函数更新自己
            if(lowv>=pre[u])//存在子节点连不回此点之前的点,此点是割点--定理
            {
                iscut[u]=true;//标记为割点
                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)//访问过v 而且v在u之前
        {
            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);
}
int odd[maxn],color[maxn];
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 A[maxn][maxn];
int main()
{
    int kase=0,n,m;
    while(scanf("%d%d",&n,&m)==2&&n)
    {
        for(int i=0;i<n;i++) G[i].clear();
        memset(A,0,sizeof(A));
        for(int i=0;i<m;i++)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            u--;v--;
            A[u][v]=A[v][u]=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;//u是这个连通分量的第一个点
            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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
资源包主要包含以下内容: ASP项目源码:每个资源包中都包含完整的ASP项目源码,这些源码采用了经典的ASP技术开发,结构清晰、注释详细,帮助用户轻松理解整个项目的逻辑和实现方式。通过这些源码,用户可以学习到ASP的基本语法、服务器端脚本编写方法、数据库操作、用户权限管理等关键技术。 数据库设计文件:为了方便用户更好地理解系统的后台逻辑,每个项目中都附带了完整的数据库设计文件。这些文件通常包括数据库结构图、数据表设计文档,以及示例数据SQL脚本。用户可以通过这些文件快速搭建项目所需的数据库环境,并了解各个数据表之间的关系和作用。 详细的开发文档:每个资源包都附有详细的开发文档,文档内容包括项目背景介绍、功能模块说明、系统流程图、用户界面设计以及关键代码解析等。这些文档为用户提供了深入的学习材料,使得即便是从零开始的开发者也能逐步掌握项目开发的全过程。 项目演示与使用指南:为帮助用户更好地理解和使用这些ASP项目,每个资源包中都包含项目的演示文件和使用指南。演示文件通常以视频或图文形式展示项目的主要功能和操作流程,使用指南则详细说明了如何配置开发环境、部署项目以及常见问题的解决方法。 毕业设计参考:对于正在准备毕业设计的学生来说,这些资源包是绝佳的参考材料。每个项目不仅功能完善、结构清晰,还符合常见的毕业设计要求和标准。通过这些项目,学生可以学习到如何从零开始构建一个完整的Web系统,并积累丰富的项目经验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值