The main topic: N Knights often hold round tables, each round table at least 3 knights (and each of the number of knights is odd number), and all the hated knight can not sit next to the table adjacent position, ask how many knights can not participate in any meeting
Problem-solving ideas: The knight as the point of the establishment of the graph G. If two knights can be adjacent (that is, they do not hate each other), an edge is attached.
The title translates to the number of nodes that are not on any simple odd circle
First, the circle is a two-connected component, so the first thing is to find out all the two connected components, and then determine whether the double connected component is a singular circle
The determination of the odd circle is determined by the two-dimensional graph staining, if a circle can be stained by a binary graph, then this circle is not a singular circle, because the two-dimensional map dyeing, the point of staining is paired, so the number of points is even
#include <cstdio>#include <cstring>#include <algorithm>#include <vector>#include <queue>#include <stack>using namespace STD;#define N 1010structedge{intU, v; Edge () {} Edge (intUintV): U (U), V (v) {}};intPre[n], Iscut[n], bccno[n], Dfs_clock, bcc_cnt;intOdd[n], color[n]; vector<int>G[n], bcc[n]; Stack<Edge>SintDfsintUintFA) {intLowu = Pre[u] = ++dfs_clock;intChild =0; for(inti =0; I < g[u].size (); i++) {intv = g[u][i]; Edge e = Edge (U, v);if(!pre[v]) {S.push (e); child++;intLOWV = DFS (v, u); Lowu = min (LOWV, LOWU);if(LOWV >= Pre[u]) {Iscut[u] =true; 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] =0;returnLowu;}voidFIND_BCC (intN) {memset(Pre,0,sizeof(pre));memset(Iscut,0,sizeof(Iscut));memset(Bccno,0,sizeof(BCCNO)); Dfs_clock = bcc_cnt =0; for(inti =0; I < n; i++)if(!pre[i]) DFS (i,-1);}BOOLBipartite (intUintb) { for(inti =0; I < g[u].size (); i++) {intv = g[u][i];if(Bccno[v]! = b)Continue;if(Color[v] = = Color[u])return false;if(!color[v]) {Color[v] =3-Color[u];if(!bipartite (V, b))return false; } }return true;}intA[n][n];intMain () {intCAS =1, N, M; while(scanf("%d%d", &n, &m)! = EOF && n + m) { for(inti =0; I < n; i++) g[i].clear ();memsetA0,sizeof(A));intU, v; for(inti =0; I < m; i++) {scanf("%d%d", &u, &v); u--; v--; A[U][V] = A[v][u] =1; } for(intU =0; U < n; u++) for(intv = U +1; v < n; v++)if(! A[u][v]) {g[u].push_back (v); G[v].push_back (U); } FIND_BCC (n);memset(Odd,0,sizeof(odd)); for(inti =1; I <= bcc_cnt; i++) {memset(Color,0,sizeof(color)); for(intj =0; J < Bcc[i].size (); J + +) Bccno[bcc[i][j]] = i;intU = bcc[i][0]; Color[u] =1;if(!bipartite (U, i)) for(intj =0; J < Bcc[i].size (); J + +) {Odd[bcc[i][j]] =1; } }intans = n; for(inti =0; I < n; i++)if(Odd[i]) ans--;printf("%d\n", ans); }return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
UVALive-3523 Knights of the Round Table (dual connected components of undirected graphs)