zoukankan      html  css  js  c++  java
  • Poj2942--Knights of the Round Table

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

    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

    这道题目是求点双连通分量,此题解题步骤:1,找点连通分量,2,染色找奇圈
    具体看代码:
          
      1 #include<cstdio>
      2 #include<cmath>
      3 #include<iostream>
      4 #include<algorithm>
      5 #include<cstring>
      6 #include<vector>
      7 #include<map>
      8 #include<list>
      9 #include<set>
     10 #include<queue>
     11 #include<stack>
     12 #include<deque>
     13 using namespace std;
     14 
     15 #define min(a,b) (a>b?b:a)
     16 #define max(a,b) (a>b?a:b)
     17 #define INF 9999999
     18 #define eps 1e-7
     19 #define Maxn 1001
     20 
     21 int low[Maxn];                  //最低深度优先数
     22 int dfn[Maxn],tempdfn;            //深度优先数
     23 int vis[Maxn];
     24 int A[Maxn][Maxn];                //存图
     25 int iscut[Maxn];
     26 int bccno[Maxn],bcc_cnt;        //标记点连通分量,连通分量的数目
     27 struct Edge
     28 {
     29     int u,v;
     30     Edge(int s,int t):u(s),v(t)
     31     {}
     32 };
     33 stack<Edge>s;                   //栈,存边
     34 int odd[Maxn];                    //判断是否是奇图
     35 int color[Maxn];                //染色
     36 int n,m;
     37 vector<int>G[Maxn],bcc[Maxn];    
     38 
     39 void dfs(int u,int father)
     40 {
     41     vis[u]=1;  
     42     low[u]=dfn[u]=++tempdfn;             //low-----1
     43     int child = 0;
     44     for(int j=0;j<G[u].size();j++)
     45     {
     46         int v=G[u][j];
     47         Edge e=Edge(u,v);
     48         if(!vis[v])
     49         {
     50             child++;
     51             s.push(e);
     52             dfs(v,u);
     53             low[u]=min(low[u],low[v]);    //low------2
     54             if(low[v]>=dfn[u])            //找到关节点,u为关节点
     55             {
     56                 iscut[u]=1;
     57                 bcc_cnt++;bcc[bcc_cnt].clear();
     58                 for(;;)
     59                 {
     60                     Edge t=s.top();s.pop();   //出栈直到t.u==u,t.v==v
     61                     if(bccno[t.u]!=bcc_cnt){
     62                         bcc[bcc_cnt].push_back(t.u);
     63                         bccno[t.u]=bcc_cnt;
     64                     }
     65                     if(bccno[t.v]!=bcc_cnt){
     66                         bcc[bcc_cnt].push_back(t.v);
     67                         bccno[t.v]=bcc_cnt;
     68                     }
     69                     if(t.u==u&&t.v==v)break;
     70                 }
     71             }
     72         }
     73         else if(dfn[v]<dfn[u]&&v!=father)
     74         {
     75             low[u]=min(low[u],dfn[v]);              //low-----3,以上low---1,low---2,用来求low[u]
     76         }
     77     }
     78     if(father<0&&child==1)iscut[u]=0;
     79 }
     80 
     81 void find_bcc(int n)
     82 {
     83     memset(vis,0,sizeof(vis));
     84     memset(low,0,sizeof(low));
     85     memset(dfn,0,sizeof(dfn));
     86     memset(iscut,0,sizeof(iscut));
     87     memset(bccno,0,sizeof(bccno));
     88     tempdfn=bcc_cnt=0;
     89     for(int i=0;i<n;i++)
     90     {
     91         if(!vis[i])
     92         {
     93             dfs(i,-1);
     94         }
     95     }
     96 }
     97 
     98 int bipartite(int u,int kind)
     99 {
    100     int i;
    101     for(i=0;i<G[u].size();i++)
    102     {
    103         int v=G[u][i];
    104         if(bccno[v]!=kind)continue;
    105         if(color[v]==color[u])return false;
    106         if(!color[v])
    107         {
    108             color[v]=3-color[u];
    109             if(!bipartite(v,kind))return false;
    110         }
    111     }
    112     return true;
    113 }
    114 
    115 int main()
    116 {
    117     int cas = 0;
    118     while(scanf("%d%d",&n,&m)!=EOF)
    119     {
    120         if(n==0)break;
    121         int i;
    122         for( i=0;i<n;i++)G[i].clear();
    123         memset(A,0,sizeof(A));
    124         for(i=0;i<m;i++)
    125         {
    126             int u,v;
    127             scanf("%d%d",&u,&v);
    128             u--,v--;
    129             A[u][v]=A[v][u]=1;
    130         }
    131         for(int u=0;u<n;u++)
    132         {
    133             for(int v=u+1;v<n;v++)
    134             {
    135                 if(!A[u][v])
    136                 {
    137                     G[u].push_back(v);
    138                     G[v].push_back(u);
    139                 }
    140             }
    141         }
    142         find_bcc(n);
    143         memset(odd,0,sizeof(odd));
    144     //    for(i=1;i<=bcc_cnt;i++)
    145     //    {
    146     //        printf("size:%d
    ",bcc[i].size());
    147     //    }
    148         for(i=1;i<=bcc_cnt;i++)
    149         {
    150             memset(color,0,sizeof(color));
    151             for(int j=0;j<bcc[i].size();j++)
    152             {
    153                 bccno[bcc[i][j]]=i;                   //对同一个连通分量中的点进行标记
    154         //        printf("%d
    ",bcc[i][j]);
    155             }
    156             int u=bcc[i][0];
    157             color[u]=1;
    158             if(!bipartite(u,i))                       //能够染色,返回false,有奇圈
    159             {
    160                 for(int j=0;j<bcc[i].size();j++)
    161                 {
    162                     odd[bcc[i][j]]=1;                 //需要保留下来的点
    163                 //    printf("%d
    ",bcc[i][j]);
    164                 }
    165             }
    166         }
    167         int ans=n;
    168         for(i=0;i<n;i++)
    169         {
    170             if(odd[i])ans--;
    171         }
    172         printf("%d
    ",ans);
    173     }
    174     return 0;
    175 }
    View Code
  • 相关阅读:
    JDK线程池原理之一:工作原理
    Hystrix Feign 特定状态码不熔断
    Hystrix熔断的方法级别(自定义commonKey)
    谨慎使用Exception
    FunctionalInterface~一个批量处理数据的类
    keycloak~账号密码认证和授权码认证
    keycloak~OIDC&OAuth2&自定义皮肤
    docker~添加hosts绑定的方法
    docker~产生的IP段与现有IP冲突问题
    高中数学知识要点及解题方法精粹[网摘]
  • 原文地址:https://www.cnblogs.com/zafuacm/p/3250513.html
Copyright © 2011-2022 走看看