• poj2942 Knights of the Round Table


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

    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

    题意:
    一个国家有N(1<=n<=1000)个圆桌骑士,该国有一种会议叫圆桌会议,即由奇数个(>1)骑士围成圆桌开会,已知M对禁止关系(a,b)表示编号为ab的两名圆桌骑士不能相邻坐,询问该国有多少名骑士无论如何安排座位都可能参加任何一场会议。
    分析:白书上的经典题目,要求的就是有多少个骑士不在任意一个奇环上,而一个环不正是一个双连通分量吗?我们求出所有的双连通分量,再判断这个双连通分量有没有奇环,怎么判断有没有奇环呢?二分图!二分图就是没有奇环的图,我们用染色的方式判断这是不是一个二分图,来看双连通分量上有没有奇环,但是就有一个问题了,我知道双连通分量上有一个奇环,但是我不知道上面的某一个点是不是在奇环上,这要怎么办呢?
      可以证明,上面的每一个点都一定在奇环内,假设点x不在奇环内,u,v是两个奇环上的点,因为在一个双连通分量上,所以u,v之间必然有两条不同的路径,而且组成了奇环,所以一条路径是奇数,一条路径是偶数,那么可以很容易知道,x与u,v组成的环中一定有奇数路径的.
        最后套用bcc的模板就好了.
    复习了一下二分图的概念,双连通分量如果不是二分图,那么所有的点都在奇环上......
    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    #include <cmath>
    
    using namespace std;
    
    const int maxn = 1010,maxm = 1e6+5;
    
    int head[maxn],to[maxm * 2],nextt[maxm * 2],tot = 1;
    int n,m,a[maxn][maxn],num,ans,dfs_clock,top,pre[maxn],low[maxn],iscut[maxn],belong[maxn],col[maxn];
    bool flag[maxn];
    
    struct node
    {
        int u,v;
    }e[maxm * 2];
    
    void add(int x,int y)
    {
        to[tot] = y;
        nextt[tot] = head[x];
        head[x] = tot++;
    }
    
    bool color(int u,int id)
    {
        for (int i = head[u];i;i = nextt[i])
        {
            int v = to[i];
            if (belong[v] != id)
            continue;
            if (col[v] == col[u])
            return false;
            if (!col[v])
            {
                col[v] = 3 - col[u];
                if (!color(v,id))
                return false;
            }
        }
        return true;
    }
    
    void dfs(int u,int fa)
    {
        pre[u] = low[u] = ++dfs_clock;
        int child = 0;
        for (int i = head[u];i;i = nextt[i])
        {
            int v = to[i];
            if (!pre[v])
            {
                node temp;
                temp.u = u;
                temp.v = v;
                e[++top] = temp;
                child++;
                dfs(v,u);
                low[u] = min(low[u],low[v]);
                if (low[v] >= pre[u])
                {
                    iscut[u] = 1;
                    num++;
                    while (1)
                    {
                        int tu = e[top].u,tv = e[top--].v;
                        if (belong[tu] != num)
                        belong[tu] = num;
                        if (belong[tv] != num)
                        belong[tv] = num;
                        if (tu == u && tv == v)
                        break;
                    }
                    col[u] = 1;
                    if (!color(u,num))
                    for (int i = 1; i <= n; i++)
                    if (belong[i] == num)
                    flag[i] = 1;
                    col[u] = 0;
                }
            }
            else
            if (pre[v] < pre[u] && v != fa)
            {
                node temp;
                temp.u = u;
                temp.v = v;
                e[++top] = temp;
                low[u] = min(low[u],pre[v]);
            }
        }
        if (child == 1 && fa == -1)
        iscut[u] = 0;
    }
    
    void bcc()
    {
        for (int i = 1; i <= n; i++)
        if (!pre[i])
        dfs(i,-1);
    }
    
    int main()
    {
        while (scanf("%d%d",&n,&m) == 2 && (n || m))
        {
            memset(head,0,sizeof(head));
            memset(a,0,sizeof(a));
            memset(flag,false,sizeof(flag));
            memset(pre,0,sizeof(pre));
            memset(low,0,sizeof(low));
            memset(iscut,0,sizeof(iscut));
            memset(col,0,sizeof(col));
            memset(belong,0,sizeof(belong));
            tot = 1;
            ans = 0;
            dfs_clock = 0;
            top = 0;
            num = 0;
            for (int i = 1; i <= m; i++)
            {
                int u,v;
                scanf("%d%d",&u,&v);
                a[u][v] = a[v][u] = 1;
            }
            for (int i = 1; i <= n; i++)
            for (int j = i + 1; j <= n; j++)
            if (!a[i][j])
            { 
            add(i,j);
            add(j,i);
            }
            bcc();
            for (int i = 1; i <= n; i++)
            if (!flag[i])
            ans++;
            printf("%d
    ",ans);
        }
    
        return 0;
    }
     
  • 相关阅读:
    表的相关内容
    数据类型
    mysql入门练习
    mysql入门
    协程
    多进程
    装饰器
    网络编程
    心路历程
    gensim的使用
  • 原文地址:https://www.cnblogs.com/zbtrs/p/7525501.html
Copyright © 2020-2023  润新知