UVALive-3523 Knights of the Round Table (双连通分量+二分图匹配)

题目大意:有n个骑士要在圆桌上开会,但是相互憎恶的两个骑士不能相邻,现在已知骑士们之间的憎恶关系,问有几个骑士一定不能参加会议。参会骑士至少有3个且有奇数个。

题目分析:在可以相邻的骑士之间连一条无向边,构成一张图G。则问题变成了有几个节点不在奇圈(有奇数个节点的圈)内,并且一个点在圈内最多出现一次。如果G不连通,应该对每一个分量分别求解。奇圈上的点一定在同一个双连通分量内,要找出所有的双连通分量。但是能构成二分图的双连通分量中一定没有奇圈,不能构成二分图的双连通分量中一定含有奇圈,并且分量中所有的点都在奇圈上,所有还要判断每一个双连通分量能不能构成二分图。

其实就是一道模板题。。。

代码如下:

# include<iostream>
# include<cstdio>
# include<vector>
# include<stack>
# include<cstring>
# include<algorithm>
using namespace std;

const int maxn=1005;
struct Edge
{
    int u,v;
    Edge(int _u,int _v):u(_u),v(_v){}
};
stack<Edge>S;
vector<int>G[maxn],bcc[maxn];
int bcc_cnt,dfs_cnt,low[maxn],pre[maxn],odd[maxn],color[maxn],bccno[maxn],iscut[maxn],A[maxn][maxn];

int dfs(int u,int fa)
{
    int lowu=pre[u]=++dfs_cnt;
    int child=0;
    for(int i=0;i<G[u].size();++i){
        int v=G[u][i];
        if(!pre[v]){
            S.push(Edge(u,v));
            ++child;
            int lowv=dfs(v,u);
            lowu=min(lowu,lowv);
            if(lowv>=pre[u]){
                iscut[u]=1;
                bcc[++bcc_cnt].clear();
                while(1)
                {
                    Edge x=S.top();
                    S.pop();
                    if(x.u!=bcc_cnt){
                        bcc[bcc_cnt].push_back(x.u);
                        bccno[x.u]=bcc_cnt;
                    }
                    if(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(Edge(u,v));
            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_cnt=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()
{
    int n,m,a,b;
    while(scanf("%d%d",&n,&m)&&(n+m))
    {
        memset(A,0,sizeof(A));
        for(int i=0;i<n;++i)  G[i].clear();
        while(m--){
            scanf("%d%d",&a,&b);
            --a,--b;
            A[a][b]=A[b][a]=1;
        }
        for(int i=0;i<n;++i)
            for(int j=i+1;j<n;++j)
                if(!A[i][j])
                    G[i].push_back(j),G[j].push_back(i);

        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;
}

  

时间: 2024-08-07 17:02:43

UVALive-3523 Knights of the Round Table (双连通分量+二分图匹配)的相关文章

UVALive 3523 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

UVALive - 3523 Knights of the Round Table(无向图的双连通分量)

题目大意:有n个骑士经常举行圆桌会议,每次圆桌会议至少要有3个骑士参加(且每次参加的骑士数量是奇数个),且所有互相憎恨的骑士不能坐在圆桌旁的相邻位置,问有多少个骑士不可能参加任何一个会议 解题思路:以骑士为点建立无向图G.如果两个骑士可以相邻(即他们并不互相憎恨),即可连一条边. 则题目就转化为求不在任何一个简单奇圈上的结点个数 首先,圈就是一个双连通的分量,所以第一件事就是将所有的双连通分量求出来,接着再判定这个双连通分量是不是奇圈 奇圈的判定就是用二分图染色判定,如果某个圈能被二分图染色,那

LA 3523 双连通分量+二分图判定

3523 - Knights of the Round Table Time limit: 4.500 seconds 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 surp

POJ 2942 Knights of the Round Table 黑白着色+点双连通分量

题目来源:POJ 2942 Knights of the Round Table 题意:统计多个个骑士不能參加随意一场会议 每场会议必须至少三个人 排成一个圈 而且相邻的人不能有矛盾 题目给出若干个条件表示2个人直接有矛盾 思路:求补图  能够坐在一起 就是能够相邻的人建一条边 然后假设在一个奇圈上的都是满足的 那些不再不论什么一个奇圈的就是不满足 求出全部奇圈上的点 总数减去它就是答案 首先有2个定理 1.一个点双连通分量是二分图 你就没有奇圈 假设有奇圈  那就不是二分图  充分必要条件 所

【POJ 2942】Knights of the Round Table(双联通分量+染色判奇环)

[POJ 2942]Knights of the Round Table(双联通分量+染色判奇环) Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 11661   Accepted: 3824 Description Being a knight is a very attractive career: searching for the Holy Grail, saving damsels in distress, an

POJ2942 Knights of the Round Table[点双连通分量|二分图染色|补图]

Knights of the Round Table Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 12439   Accepted: 4126 Description Being a knight is a very attractive career: searching for the Holy Grail, saving damsels in distress, and drinking with the oth

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

UvaLive3523 Knights of the Round Table 参考了Kuangbin巨的题解. /* POJ 2942 Knights of the Round Table 亚瑟王要在圆桌上召开骑士会议,为了不引发骑士之间的冲突, 并且能够让会议的议题有令人满意的结果,每次开会前都必须对出席会议的骑士有如下要求: 1. 相互憎恨的两个骑士不能坐在直接相邻的2个位置: 2. 出席会议的骑士数必须是奇数,这是为了让投票表决议题时都能有结果. 注意:1.所给出的憎恨关系一定是双向的,不

POJ 2942 - Knights of the Round Table(双连通图 Tarjan + 二分判定)

POJ 2942 - Knights of the Round Table(双连通图 Tarjan + 二分判定) ACM 题目地址: POJ 2942 - Knights of the Round Table 题意: 有N个骑士,给出某些骑士之间的仇恨关系,骑士们开会时会围坐在一个圆桌旁.一次会议能够顺利举行,要满足两个条件: 任意相互憎恨的两个骑士不能相邻 开会人数为大于2的奇数 若某个骑士任何会议都不能参加,那么就必须将他踢出,给出骑士之间的仇恨关系,问最少需要踢出多少个骑士? 分析: 把

POJ 2942 Knights of the Round Table (点-双连通分量 + 交叉法染色判二分图)

POJ 2942 Knights of the Round Table 链接:http://poj.org/problem?id=2942 题意:亚瑟王要在圆桌上召开骑士会议,为了不引发骑士之间的冲突,并且能够让会议的议题有令人满意的结果,每次开会前都必须对出席会议的骑士有如下要求: 1. 相互憎恨的两个骑士不能坐在直接相邻的2个位置: 2. 出席会议的骑士数必须是奇数,这是为了让投票表决议题时都能有结果. 如果出现有某些骑士无法出席所有会议(例如这个骑士憎恨所有的其他骑士),则亚瑟王为了世界和