zoukankan      html  css  js  c++  java
  • poj 2942 Knights of the Round Table 点双连通分量

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

    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

     
    算法:类似点双连通分量,不过可以不求点双连通分量。用tarjan算法把各个点双连通分量及其最近割点标记并用vector保存(割点多次保存,且割点不一定属于某双连通分量,但也同标记,所以非双连通分量),然后bipartite染色,因为点双连通分量存在奇圈等价于其非二分图,即无法交叉染色。另外,若点双连通分量存在奇圈,则该分量所有点都在奇圈上,可分三种情况证明如下:
    设u, v 在奇圈上,w在该圈外,由点双连通分量定义有,w有两条点不相交路径到达u:
    1.若恰有一条路径p1经过了圈上点t,记另一条为p2;   p1:w-t-u, p2 : w-u, 则图中(见上图)包含w点的至少有两个圈:(p2, p1), (p2, 奇圈-p1), 因为p1、奇圈-p1路径长度恰好一奇一偶,所以两个圈中恰有一个为奇圈;
    2.两条不相交路径不经过该圈;3.两条路径都经过该圈,这两种情况同理可证。
     
    另外,并非所有点都标记过,比如孤立点。这也是这种算法的好处之一,求奇圈不用管这些点,然后做减法即可。
     
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<string>
    #include<cmath>
    #include<vector>
    #include<cstdlib>
    #include<algorithm>
    
    using namespace std;
    
    #define LL long long
    #define ULL unsigned long long
    #define UINT unsigned int
    #define MAX_INT 0x7fffffff
    #define MAX_LL 0x7fffffffffffffff
    #define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
    #define MIN(X,Y) ((X) < (Y) ? (X) : (Y))
    #define MAXN 1111
    #define MAXM 2222222
    
    struct edge{
        int u, v, nxt;
        int re;
    }e[MAXM];
    int cc;
    int a[MAXN][MAXN], h[MAXN], ve[MAXM];
    int dfn[MAXN], low[MAXN], s[MAXM];
    int tsp, cnt, top;
    vector<int> bcc[MAXN];
    int bcc_cnt[MAXN], odd[MAXN];
    
    inline void add(int u, int v, int f){
        e[cc]=(edge){u, v, h[u], f+cc};
        h[u]=cc++;
    }
    
    void tarjan(int u){                 //有的点可能未标记,比如孤立点
        int v, i;
        dfn[u]=low[u]=++tsp;
        for(i=h[u]; i!=-1; i=e[i].nxt){
            v=e[i].v;
            if(!dfn[v]){
                ve[e[i].re]=1;
                s[top++]=i;
                tarjan(v);
                low[u]=MIN(low[u], low[v]);
                if(dfn[u]<=low[v]){
                    ++cnt;      bcc[cnt].clear();
                    while(1){                       //标记出的不一定是双连通分量,
                        int tt=s[--top];        edge x=e[tt];
                        if(bcc_cnt[x.u]!=cnt){
                            bcc[cnt].push_back(x.u);    bcc_cnt[x.u]=cnt;
                        }
                        if(bcc_cnt[x.v]!=cnt){
                            bcc[cnt].push_back(x.v);    bcc_cnt[x.v]=cnt;
                        }
                        if(tt==i) break;
                    }
                }
            }
            else if(!ve[i]){
                ve[e[i].re]=1;
                s[top++]=i;
                low[u]=MIN(low[u], dfn[v]);
            }
        }
    }
    
    /*              from LRJ...
    void tarjan(int u, int fa){
        low[u]=dfn[u]=++tsp;
        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();
                    while(1){
                        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]=1;
        return lowu;
    }
    
    */
    int color[MAXN], f;
    bool bipartite(int u, int col){
        color[u]=col;
        for(int i=h[u]; i!=-1; i=e[i].nxt){
            int v=e[i].v;
            if(f!=bcc_cnt[v]) continue;
            if(color[v]==col) return false;
            if(!color[v] && !bipartite(v, 3-col)) return false;
        }
        return true;
    }
    
    int solve(int n){
        int i,j;
        tsp=cnt=top=0;
        memset(dfn, 0, sizeof(dfn));
        memset(bcc_cnt, 0, sizeof(bcc_cnt));
        memset(ve, 0, sizeof(ve));
        for(i=0; i<n; i++) if(!dfn[i])
            tarjan(i);
    
        memset(odd, 0, sizeof(odd));
        for(i=1; i<=cnt; i++){
            memset(color, 0, sizeof(color));
            for(j=0; j<bcc[i].size(); j++) bcc_cnt[bcc[i][j]]=i;    //这里修改了割点的标记,导致割点会多次染色
            f=i;
            if(bcc[i].size() && !bipartite(bcc[i][0], 1)){
                for(j=0; j<bcc[i].size(); j++)
                    odd[bcc[i][j]]=1;
            }
        }
        int ans=n;//巧妙的计数方式,没有标号的点默认在ans中,且对割点去重
        for(i=0; i<n; i++) if(odd[i])
            ans--;
        return ans;
    }
    
    int main(){
        //freopen("C:\Users\Administrator\Desktop\in.txt","r",stdin);
        int n,m;
        while(scanf(" %d %d", &n, &m)==2 && (n||m)){
            int i,j,u,v;
            memset(a, 0, sizeof(a));
            for(i=0; i<m; i++){
                scanf(" %d %d", &u, &v);        u--; v--;
                a[u][v]=a[v][u]=1;
            }
            memset(h, -1, sizeof(h));       cc=0;
            for(i=0; i<n; i++)
                for(j=i+1; j<n; j++) if(!a[i][j]){
                    add(i, j, 1);      add(j, i, -1);
                }
            int ans=solve(n);
            printf("%d
    ", ans);
        }
        return 0;
    }
    View Code
     
    开始搞错题意,以为要求一方案使得到开会的骑士量最大,其实只要排除不能参加会议的骑士数就好。
     
  • 相关阅读:
    synthetic-load-generator 一个不错的opentracing trace && metrics && logs 生成工具
    记一次php.ini配置不合理造成系统加载偏慢问题
    Data-Prepper opendistro 开源的基于es 的trace 分析工具
    使用babel-standalone 让浏览器支持es6特性
    tempo grafana 团队开源的分布式追踪框架
    grafana/agent grafana 团队开源的兼容prometheus 的agent
    k6 集成goja 的部分集成说明
    spf13/afero 通用文件系统试用
    goja 支持es6的一种方法
    salesforce 跨组织数据可见性的方案
  • 原文地址:https://www.cnblogs.com/ramanujan/p/3301802.html
Copyright © 2011-2022 走看看