Knights of the Round Table 补图, 双连通分量,奇圈,二分图,染色(交叉染色),(tarjan)

Problem 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 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <algorithm>
  5 #include <stack>
  6 #include <queue>
  7 #define MAX 1005
  8 using namespace std;
  9 struct node
 10 {
 11   int to,next;
 12 };
 13 struct Eage
 14 {
 15     int start,end;
 16 };
 17   int N,M;
 18   bool cnt[MAX][MAX];
 19   node edge[MAX*100];
 20   int head[MAX];
 21   int index;
 22   int dfn[MAX],low[MAX];
 23   int time;
 24   stack <Eage> sta;
 25   int color[MAX];
 26   bool flag[MAX];
 27   queue <int> Q;
 28 
 29   bool mark[MAX];
 30 
 31   void addNode(int from,int to)
 32   {
 33       edge[index].to = to;
 34       edge[index].next = head[from];
 35       head[from] = index++;
 36       edge[index].to = from;
 37       edge[index].next = head[to];
 38       head[to] = index++;
 39   }
 40 
 41   void convert()
 42   {
 43      index = 0;
 44       memset(head,-1,sizeof(head));
 45       for(int i=1;i<=N;i++)
 46       {
 47           for(int j=i+1;j<=N;j++)
 48           {
 49               if(!cnt[i][j])
 50                   addNode(i,j);
 51           }
 52       }
 53   }
 54  bool toColor(int now)
 55     {
 56       while(!Q.empty())
 57           Q.pop();
 58       Q.push(now);
 59       while(!Q.empty())
 60       {
 61           int cur = Q.front();
 62           Q.pop();
 63           for(int i=head[cur];i!=-1;i=edge[i].next)
 64           {
 65               int to=edge[i].to;
 66               if(!flag[to])
 67                   continue;
 68              if(!color[to])
 69               {
 70                   color[to]=-color[cur];
 71                   Q.push(to);
 72               }
 73               else if(color[to] == color[cur])
 74                   return true;
 75          }
 76       }
 77       return false;
 78   }
 79 
 80   void tarjan(int cur,int fat)
 81     {
 82       dfn[cur]=low[cur]=++time;
 83       for(int i=head[cur];i!=-1;i=edge[i].next)
 84         {
 85           int to=edge[i].to;
 86           if(to!=fat && dfn[to]<dfn[cur])
 87           {
 88               if(!dfn[to])
 89               {
 90                   Eage eg;
 91                   eg.start=cur;
 92                  eg.end=to;
 93                  sta.push(eg);
 94                  tarjan(to,cur);
 95                  low[cur]=min(low[cur],low[to]);
 96                  if(dfn[cur]<=low[to])//存在连通分量 出栈标记;
 97                  {
 98                      memset(flag,0,sizeof(flag));
 99                      while(!sta.empty())
100                      {
101                          Eage s=sta.top();
102                            sta.pop();
103                          if(s.start == eg.start && s.end == eg.end)
104                              break;
105                          flag[s.start]=true;
106                          flag[s.end]=true;
107                      }
108                      memset(color,0,sizeof(color));
109                      color[cur]=1;
110                      if(toColor(cur))
111                      {
112                          mark[cur]=true;
113                          for(int j=1;j<=N;j++)
114                              if(flag[j])
115                                  mark[j]=true;
116                      }
117                  }
118              }
119              else
120                  low[cur]=min(low[cur],dfn[to]);
121          }
122      }
123  }
124 
125  void solve()
126     {
127      memset(dfn,0,sizeof(dfn));
128      time = 0;
129      for(int i=1;i<=N;i++)
130          if(!dfn[i])
131              tarjan(i,-1);
132  }
133 
134  int main()
135    {
136      while(scanf("%d%d",&N,&M))
137      {
138              if(!N && !M)
139              break;
140          memset(cnt,0,sizeof(cnt));
141          memset(mark,0,sizeof(mark));
142          for(int i=1;i<=M;i++)
143          {
144            int s,e;
145              scanf("%d%d",&s,&e);
146              cnt[s][e]=cnt[e][s]=true;
147          }
148         convert();
149          solve();
150          int ret = 0;
151          for(int i=1;i<=N;i++)
152             if(mark[i])
153                 ret ++;
154         printf("%d\n",N-ret);
155    }
156     return 0;
157  }
View Code

 

posted @ 2013-10-16 17:00  persistent codeants  阅读(246)  评论(0编辑  收藏  举报