zoukankan      html  css  js  c++  java
  • poj 2942

    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

     
    点的双联通分量
    #include<iostream>
    #include<cstdio>
    #include<vector>
    #include<cstring>
    using namespace std;
    
    const int maxn = 1e3+10;
    
    const int maxm = 1e6+10;
    
    int n,m,low[maxn],dfn[maxn],bcc_cnt,size,bccno[maxn],tim,p,color[maxn],head[maxn];
    
    bool odd[maxn],link[maxn][maxn];
    
    struct edge{
        int v,nex;
    }e[maxm<<1];
    
    struct node{
        int u,v;
    }s[10010];
    
    void adde(int u,int v){
        e[size].v=v;e[size].nex=head[u];head[u]=size++;
    }
    
    vector<int> bcc[maxn];
    
    void dfs(int u,int fa){
        low[u]=dfn[u]=++tim;
        for(int i=head[u];~i;i=e[i].nex){
            int v=e[i].v;
            if(!dfn[v]){
                s[++p].u=u;s[p].v=v;
                dfs(v,u);
                low[u]=min(low[u],low[v]);
                if(low[v]>=dfn[u]) {
                    ++bcc_cnt;
                    bcc[bcc_cnt].clear();
                    while(p){
                        if(bccno[s[p].u]!=bcc_cnt) 
                        bcc[bcc_cnt].push_back(s[p].u),bccno[s[p].u]=bcc_cnt;
                        if(bccno[s[p].v]!=bcc_cnt) 
                        bcc[bcc_cnt].push_back(s[p].v),bccno[s[p].v]=bcc_cnt;
                        if(s[p].u==u&&s[p].v==v) break;
                        p--;
                    }
                    p--;
                }
            }else if(dfn[v]<dfn[u]&&v!=fa){
                s[++p].u=u;s[p].v=v;
                low[u]=min(low[u],dfn[v]);
            }
        }
    }
    
    void find_bcc(){
        memset(dfn,0,sizeof(dfn));
        memset(low,0,sizeof(low));
        memset(bccno,0,sizeof(bccno));//没加wa了好久
        tim=bcc_cnt=0;
        for(int i=1;i<=n;i++) 
        if(!dfn[i]) dfs(i,-1);
    }
    
    bool half(int u,int d){
        for(int i=head[u];~i;i=e[i].nex){
            int v=e[i].v;
            if(bccno[v]!=d) continue;
            if(color[u]==color[v]) return 0;
            if(!color[v]) {
                color[v]=3-color[u];
                if(!half(v,d)) return 0;
            }
        }
        return 1;
    }
    
    int main(){
        while(scanf("%d%d",&n,&m)&&n){
            p=0;
            size=0;
            memset(head,-1,sizeof(head));
            memset(link,0,sizeof(link));
            for(int i=1;i<=m;i++){
                int u,v;scanf("%d%d",&u,&v);link[u][v]=link[v][u]=1;
            }
            for(int u=1;u<=n;u++)
                for(int v=1;v<u;v++){
                    if(link[u][v]) continue;adde(u,v);adde(v,u);
                }
            find_bcc();
            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;
                color[bcc[i][0]]=1;
                if(!half(bcc[i][0],i)) for(int j=0;j<bcc[i].size();j++) 
                odd[bcc[i][j]]=1;
            }
            int ans=n;
            for(int i=1;i<=n;i++) if(odd[i]) ans--;
            printf("%d
    ",ans);
        }
        return 0;
    }
    View Code
  • 相关阅读:
    k8s中负载均衡器【ingress-nginx】部署
    利用procedure批量插入数据
    一次socket.error: [Errno 99] Cannot..报错排查
    k8s使用Job执行任务失败了怎么办
    采坑指南——k8s域名解析coredns问题排查过程
    如何批量删除k8s资源对象
    史上最全docker基础知识汇总
    docker镜像制作必备技能
    kubernetes垃圾回收器GarbageCollector源码分析(一)
    python标准库-日志logging
  • 原文地址:https://www.cnblogs.com/plysc/p/11447315.html
Copyright © 2011-2022 走看看