码迷,mamicode.com
首页 > 其他好文 > 详细

LA 3523 双连通分量+二分图判定

时间:2014-10-11 02:03:54      阅读:557      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   io   os   ar   for   

3523 - Knights of the Round Table

Time limit: 4.500 seconds

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 1bubuko.com,布布扣nbubuko.com,布布扣1000 <tex2html_verbatim_mark>and 1bubuko.com,布布扣mbubuko.com,布布扣1000000 <tex2html_verbatim_mark>. The number n <tex2html_verbatim_mark>is the number of knights. The next m <tex2html_verbatim_mark>lines describe which knight hates which knight. Each of these m <tex2html_verbatim_mark>lines contains two integers k1 <tex2html_verbatim_mark>and k2 <tex2html_verbatim_mark>, which means that knight number k1 <tex2html_verbatim_mark>and knight number k2 <tex2html_verbatim_mark>hate each other (the numbers k1 <tex2html_verbatim_mark>and k2 <tex2html_verbatim_mark>are between 1 and n <tex2html_verbatim_mark>).

The input is terminated by a block with n = m = 0 <tex2html_verbatim_mark>.

 

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


题目意思:
有n个骑士,m对骑士是敌人,骑士们多次在圆桌上开会(开会时不一定全部骑士都参加且至少3个骑士参加),假设x和y是敌人,那么一次开会中x和y不能相邻,开会的人数总是奇数,问多少个骑士每次都不能参加会议。

思路:
开会时相邻的不是敌人,那么把不是敌人的人连一条线,那么开会的人为一个奇双连通分量,只要双连通分量不是二分图,那么就是奇双连通分量,先用tarjan把所有的双连通分量求出来,然后判断每个双连通分量,若该双连通分量不是二分图,那么该双连通里的骑士都可以参加会议,总数n-可以参加会议的骑士的数目即为答案。


代码:
  1 #include <cstdio>
  2 #include <cstring>
  3 #include <algorithm>
  4 #include <iostream>
  5 #include <vector>
  6 #include <queue>
  7 #include <stack>
  8 using namespace std;
  9 
 10 #define N 1005
 11 
 12 struct Edge{
 13     int x, y;
 14     Edge(int a=0,int b=0):x(a),y(b){}
 15 };
 16 
 17 vector<int>ve[N], bcc[N];
 18 stack<Edge>st;
 19 int which_bcc[N], color[N], low[N], dfn[N], dfn_clock, cnt;
 20 int di[N][N], odd[N];
 21 int n, m;
 22 
 23 void init(){
 24     for(int i=0;i<=n;i++){
 25         ve[i].clear();bcc[i].clear();
 26     }
 27     memset(which_bcc,0,sizeof(which_bcc));
 28     memset(low,0,sizeof(low));
 29     memset(dfn,0,sizeof(dfn));
 30     memset(di,0,sizeof(di));
 31     memset(odd,0,sizeof(odd));
 32 }
 33 
 34 void tarjan(int u,int fa){           //求双连通分量 
 35     int i, j, k, v;
 36     low[u]=dfn[u]=dfn_clock++;
 37     for(i=0;i<ve[u].size();i++){
 38         v=ve[u][i];
 39         if(v==fa) continue;
 40         if(!dfn[v]){
 41             st.push(Edge(u,v));
 42             tarjan(v,u);
 43             low[u]=min(low[u],low[v]);
 44             if(low[v]>=dfn[u]){
 45                 cnt++;
 46                 while(1){
 47                     Edge e=st.top();st.pop();
 48                     if(which_bcc[e.x]!=cnt){
 49                         bcc[cnt].push_back(e.x);which_bcc[e.x]=cnt;
 50                     }
 51                     if(which_bcc[e.y]!=cnt){
 52                         bcc[cnt].push_back(e.y);which_bcc[e.y]=cnt;
 53                     }
 54                     if(e.x==u&&e.y==v) break;
 55                 }
 56             }
 57         }
 58         else low[u]=min(low[u],dfn[v]);
 59     }
 60 }
 61 
 62 int is2(int u,int _bcc){           //判断二分图 
 63     int i, j, k, v;
 64     for(i=0;i<ve[u].size();i++){
 65         v=ve[u][i];
 66         if(which_bcc[v]!=_bcc) continue;
 67         if(color[v]==color[u]) return 0;
 68         if(!color[v]){
 69             color[v]=3-color[u];
 70           if(!is2(v,_bcc)) return 0;
 71         }
 72         
 73     }
 74     return 1;
 75 }
 76 
 77 main()
 78 {
 79     int i, j, k, x, y;
 80     while(scanf("%d %d",&n,&m)&&(n+m)){
 81         init();
 82         while(m--){
 83             scanf("%d %d",&x,&y);
 84             di[x][y]=di[y][x]=1;
 85         }
 86         for(i=1;i<=n;i++){
 87             for(j=i+1;j<=n;j++){
 88                 if(!di[i][j]){
 89                     ve[i].push_back(j);ve[j].push_back(i);
 90                 }
 91             }
 92         }
 93         cnt=0;dfn_clock=1;
 94         for(i=1;i<=n;i++){
 95             while(!st.empty()) st.pop();
 96             if(!dfn[i]) 
 97             tarjan(i,-1);
 98         }
 99         for(i=1;i<=cnt;i++){
100             for(j=0;j<bcc[i].size();j++) which_bcc[bcc[i][j]]=i;
101             memset(color,0,sizeof(color));
102             color[bcc[i][0]]=1;
103             if(!is2(bcc[i][0],i)){
104                 for(j=0;j<bcc[i].size();j++){
105                     odd[bcc[i][j]]=1;
106                 }
107             }
108         }
109         int ans=n;
110         for(i=1;i<=n;i++) {
111             if(odd[i])
112             ans--;
113         }
114         printf("%d\n",ans);
115     }
116 } 

 

LA 3523 双连通分量+二分图判定

标签:des   style   blog   http   color   io   os   ar   for   

原文地址:http://www.cnblogs.com/qq1012662902/p/4018140.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!