#include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <iostream > #include <algorithm> #include <vector> #include <map> #include <queue> #include <stack& Gt #include <string> #include <map> #include <set> #define EPS 1e-6 #define LL Long long using namespace std; const int MAXN = 10;//const int INF = 0x3f3f3f3f;int N, m;int a[maxn][maxn];//compute Point-double connected component, with stack s to retain the edge int pre[maxn in the current BCC ], ISCUT[MAXN], BCCNO[MAXN], Dfs_clock, bcc_cnt;vector<int> G[MAXN], bcc[maxn];struct Edge {int u, v; Edge (int u = 0, int v = 0): U (U), V (v) {}};stack<edge> s;int dfs (int u, int fa) {int Lowu = Pre[u] = ++dfs_clock;in T child = 0;for (int i = 0; i < g[u].size (); i++) {int v = g[u][i]; Edge e = Edge (U, v), if (!pre[v]) {S.push (e); Child++;int LOWV = Dfs (v, u); lowu = min (Lowu, LOWV); if (LOWV >= pre[u]) {Iscu T[u] = true;bcc_cnt++; Bcc[bcc_cnt].clear (); Attention! BCC numbering starts from 1 for (;;) {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;return Lowu;} void find_bcc (int n) {memset (pre, 0, sizeof (PRE)), memset (iscut, 0, sizeof (iscut)), memset (bccno, 0, sizeof (BCCNO));d Fs_ Clock = bcc_cnt = 0;for (int i = 0; i < n; i++) {if (!pre[i]) DFS (i,-1);}} DFS gives the binary graph black and white two coloring, color 1 for black, color 2 for white, 0 for no coloring int COLOR[MAXN]; Determine if the connected component of the node U is two graph bool bipartite (int u, int id) {for (int i = 0; i < g[u].size (); i++) {int v = g[u][i];if (bccno[v]! = ID) continue;if (color[v] = = Color[u]) return false;if (!color[v]) {Color[v] = 3-color[u];if (!bipartite (v, id)) return FA LSE;}} return true;} int odd[maxn];void init () {int u, v;memset (A, 0, sizeof (a)), memset (odd, 0, sizeof (odd)), while (m--) {scanf ("%d%d", &u, &V); u--; v--; A[U][V] = A[v][u] = 1;} for (int i = 0; i < n; i++) G[i].clear (), for (U = 0, u < n; u++) for (int v = u + 1; v < n; v++) if (! A[U][V]) G[u].push_back (v), g[v].push_back (U);} void Solve () {FIND_BCC (n); for (int i = 1; I <= bcc_cnt; i++) {memset (color, 0, sizeof (color)); for (int j = 0; J < bcc[ I].size (); J + +) Bccno[bcc[i][j]] = i;int U = bcc[i][0];color[u] = 1;if (!bipartite (U, i)) {for (int j = 0; J < Bcc[i].size (), j + +) O DD[BCC[I][J]] = 1;}} int ans = 0;for (int i = 0; i < n; i++) if (!odd[i]) ans++;cout << ans << endl;} int main () {//freopen ("Input.txt", "R", stdin), while (scanf ("%d%d", &n, &m) = = 2 && N) {init (); Solve ();} return 0;}
Test instructions: There are N Knights meeting, at least three people at a time and the number must be odd, the hatred of the people can not be adjacent to each other, give the Knights of mutual hatred, ask how many knights can not participate in any meeting.
Great White Book Classic examples, after writing the feeling of harvest is very big.
1. With the knight as the node to establish the graph, if the two knights do not hate, then in between them a side, then the title to seek not in any simple odd circle of the number of nodes.
2. All nodes on a simple circle are necessarily of the same dual-connected component, so you need to find all the two connected components first.
3. A very important conclusion!!!!!!!! For a two-connected component, if he is a binary graph, then there is no odd circle, and the opposite has the odd circle of the graph must not be a binary map, prove slightly.
4. Also a very important conclusion!!!!!!! If the node v belongs to a two-connected component that is not a binary graph, then v must belong to a singular circle. Now prove this conclusion, if the double connected component B is not a binary graph, according to the 3,b must contain a singular circle, then for the V that does not belong to this odd circle, according to the double connectivity, there must be two disjoint paths (except the starting point outside the public node), so that V can reach the two different points on the odd circle, set to V1 V2.
And since V1 and V2 are on a different odd circle, the two paths from V1 to V2 have a singular and odd length, so a singular circle through V can always be constructed.
5. Note that each cut is a plurality of double-connected components and may be marked multiple times.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
POJ 2942 Knights of the Round Table (dual-connected components of undirected graphs + binary graph determination)