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:
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.)
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 .
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、建图不能存在自环(不然任一点的自环都能形成奇圈)
2、每次判断二分图时割顶都要包含在内,即割顶在包含它的所有双连通分量中都需要参与到判断中去。
1 #include <cstdio> 2 #include <iostream> 3 #include <algorithm> 4 #include <vector> 5 #include <set> 6 #include <map> 7 #include <string> 8 #include <cstring> 9 #include <stack> 10 #include <queue> 11 #include <cmath> 12 #include <ctime> 13 #include <bitset> 14 #include <utility> 15 #include <assert.h> 16 using namespace std; 17 #define rank rankk 18 #define mp make_pair 19 #define pb push_back 20 #define xo(a,b) ((b)&1?(a):0) 21 #define tm tmp 22 #pragma comment(linker, "/STACK:1024000000,1024000000") 23 //#define LL ll 24 typedef unsigned long long ull; 25 typedef pair<int,int> pii; 26 typedef long long ll; 27 typedef pair<ll,int> pli; 28 typedef pair<ll,ll> pll; 29 const int INF=0x3f3f3f3f; 30 const ll INFF=0x3f3f3f3f3f3f3f3fll; 31 const int MAX=4e5+10; 32 //const ll MAXN=2e8; 33 //const int MAX_N=MAX; 34 const ll MOD=1e9+7; 35 //const long double pi=acos(-1.0); 36 //const double eps=0.00000001; 37 ll gcd(ll a,ll b){return b?gcd(b,a%b):a;} 38 template<typename T>inline T abs(T a) {return a>0?a:-a;} 39 template<class T> inline 40 void read(T& num) { 41 bool start=false,neg=false; 42 char c; 43 num=0; 44 while((c=getchar())!=EOF) { 45 if(c=='-') start=neg=true; 46 else if(c>='0' && c<='9') { 47 start=true; 48 num=num*10+c-'0'; 49 } else if(start) break; 50 } 51 if(neg) num=-num; 52 } 53 inline ll powMM(ll a,ll b,ll M){ 54 ll ret=1; 55 a%=M; 56 // b%=M; 57 while (b){ 58 if (b&1) ret=ret*a%M; 59 b>>=1; 60 a=a*a%M; 61 } 62 return ret; 63 } 64 void open() 65 { 66 // freopen("1009.in","r",stdin); 67 freopen("out.txt","w",stdout); 68 } 69 70 const int maxn=1e5+5; 71 struct Edge{int u,v;}; 72 73 int pre[maxn],iscut[maxn],bccno[maxn],dfs_clock,bcc_cnt;//bccno记录每一点所属的BCC的编号 74 vector<int> G[maxn],bcc[maxn];//bcc数组存储某一BCC中所有的点 75 stack<Edge>S; 76 int dfs(int u,int fa) 77 { 78 int lowu=pre[u]=++dfs_clock; 79 int child=0; 80 for(int i=0;i<G[u].size();i++) 81 { 82 int v=G[u][i]; 83 Edge e=(Edge){u,v}; 84 if(!pre[v])//未访问过v 85 { 86 S.push(e);child++; 87 int lowv=dfs(v,u); 88 lowu=min(lowu,lowv);//用后代的low函数更新自己 89 if(lowv>=pre[u]) 90 { 91 iscut[u]=true;//是割顶 92 bcc_cnt++;//双连通分量个数++ 93 bcc[bcc_cnt].clear();//注意!bcc从1开始编号 94 for(;;) 95 { 96 Edge x=S.top();S.pop(); 97 if(bccno[x.u]!=bcc_cnt){bcc[bcc_cnt].push_back(x.u);bccno[x.u]=bcc_cnt;} 98 if(bccno[x.v]!=bcc_cnt){bcc[bcc_cnt].push_back(x.v);bccno[x.v]=bcc_cnt;} 99 if(x.u==u&&x.v==v)break;//到了当前割顶的边 100 } 101 } 102 } 103 else if(pre[v]<pre[u]&&v!=fa) 104 { 105 S.push(e);lowu=min(lowu,pre[v]);//用反向边更新自己 106 } 107 } 108 if(fa<0&&child==1)iscut[u]=0; 109 return lowu; 110 } 111 void find_bcc(int n) 112 { 113 //调用结束后S保证为空 所以不用清空 114 memset(pre,0,sizeof(pre)); 115 memset(iscut,0,sizeof(iscut)); 116 memset(bccno,0,sizeof(bccno)); 117 dfs_clock=bcc_cnt=0; 118 for(int i=1;i<=n;i++) 119 if(!pre[i])dfs(i,-1); 120 } 121 int color[maxn]; 122 bool bipartite(int u,int bel) 123 { 124 for(int i=0;i<G[u].size();i++) 125 { 126 int v=G[u][i];if(bccno[v]!=bel)continue; 127 if(color[v]==color[u])return false; 128 if(!color[v]) 129 { 130 color[v]=3-color[u]; 131 if(!bipartite(v,bel))return false; 132 } 133 } 134 return true; 135 } 136 137 int n,m,x,y,an; 138 bool vi[1005][1005],odd[1005]; 139 int main() 140 { 141 while(scanf("%d%d",&n,&m)&&n) 142 { 143 for(int i=1;i<=n;i++)G[i].clear(); 144 memset(vi,false,sizeof(vi)); 145 memset(odd,false,sizeof(odd)); 146 for(int i=1;i<=m;i++) 147 { 148 scanf("%d%d",&x,&y); 149 vi[x][y]=vi[y][x]=true; 150 } 151 for(int i=1;i<=n;i++) 152 for(int j=1;j<=n;j++) 153 { 154 if(i==j)continue; 155 if(!vi[i][j])G[i].pb(j); 156 } 157 find_bcc(n); 158 for(int i=1;i<=bcc_cnt;i++) 159 { 160 for(int j=0;j<bcc[i].size();j++) 161 bccno[bcc[i][j]]=i; 162 int u=bcc[i][0]; 163 memset(color,0,sizeof(color)); 164 color[u]=1; 165 if(!bipartite(u,i)) 166 for(int j=0;j<bcc[i].size();j++) 167 odd[bcc[i][j]]=true; 168 } 169 an=n; 170 for(int i=1;i<=n;i++) 171 if(odd[i])--an; 172 printf("%d ",an); 173 } 174 return 0; 175 } 176 /* 177 3 4 178 1 1 0 0 179 0 1 1 0 180 1 0 0 1 181 182 1 4 183 0 0 0 1 184 */