Poj 2942 round-table samurai

Source: Internet
Author: User

Search for the connected component. In the deep priority search process, a stack is used to store all the nodes that pass through, judge the cut point, mark the node at the top of the current stack when the cut point is reached, and return to the stack, until the current node is stopped and the current cut point is marked. The marked node is in the same dual-connected component.

Search for odd circles by cross-dyeing. In the dual-connected component of a node greater than 2, there must be a circle of all nodes passing through the connected component; if this circle is an odd circle, then all vertices in the connected component meet the condition. If this circle is an even ring, if it contains an odd circle, there must be another odd circle passing through all the remaining points. Therefore, as long as there is a odd circle in a double-connected component, all the points in the double-connected component are in a strange circle. In the question, the warrior can sit in a circle. Based on this nature, you only need to find the odd circle in the double Unicom component to determine whether the double Unicom component meets the conditions. Cross-staining is to repeatedly exchange two different colors in the DFS process to dye the point. If the child nodes of the current node in a DFS are the same color as the current node, the odd circle is found.

// Poj 2942 round-table warrior, dual-connection # include <stdio. h> # include <string. h ># include <vector> const int maxn = 1005; using namespace STD; struct edge {int S, T; int next; int vis ;} edge [maxn * 2]; int head [maxn]; int e = 0; void add (int s, int t) {edge [e]. S = s; edge [e]. T = T; edge [e]. vis = 0; edge [e]. next = head [s]; head [s] = e ++;} bool map [maxn] [maxn]; int btype, time, TOT; int dfn [maxn], low [maxn], belong [maxn], odd [maxn]; bool mark [maxn]; int st [maxn * ma XN], top, Col [maxn]; int n, m; bool find (INT s) {int T, I; for (I = head [s]; I! =-1; I = edge [I]. next) {T = edge [I]. t; If (MARK [T]) {If (COL [T] =-1) {Col [T] =! Col [s]; return find (t);} else if (COL [T] = Col [s]) return 1 ;}} return 0 ;} void color (INT s) {int I; memset (mark, 0, sizeof (Mark); do {I = sT [top --]; Mark [edge [I]. s] = 1; Mark [edge [I]. t] = 1;} while (edge [I]. s! = S); memset (COL,-1, sizeof (COL); Col [s] = 0; If (find (s) {for (I = 1; I <= N; I ++) {If (MARK [I]) odd [I] = 1 ;}} inline int min (int A, int B) {return a <B? A: B;} void DFS (INT s) {int I, T; dfn [s] = low [s] = ++ time; for (I = head [s]; i! =-1; I = edge [I]. next) {If (edge [I]. vis) continue; edge [I]. vis = edge [I ^ 1]. vis = 1; t = edge [I]. t; ST [++ top] = I; If (! Dfn [T]) {DFS (t); low [s] = min (low [s], low [T]); if (low [T]> = dfn [s]) color (s);} else low [s] = min (low [s], dfn [T]);} inline void SCC () {int I; time = 0; Top = 0; memset (dfn, 0, sizeof (dfn); memset (odd, 0, sizeof (ODD); for (I = 1; I <= N; I ++) if (! Dfn [I]) DFS (I);} inline int solve () {int res = 0, I; SCC (); for (I = 1; I <= N; I ++) if (! Odd [I]) RES ++; return res;} void read_data () {int I, j, a, B; for (I = 1; I <= N; I ++) for (j = 1; j <= N; j ++) map [I] [J] = 1; for (I = 0; I <m; I ++) {scanf ("% d", & A, & B); map [a] [B] = map [B] [a] = 0 ;} memset (Head,-1, sizeof (head); E = 0; for (I = 1; I <= N; I ++) for (j = I + 1; j <= N; j ++) {If (Map [I] [J]) {Add (I, j); add (J, I );}}} int main () {While (scanf ("% d", & N, & M), N | M) {read_data (); printf ("% d \ n", solve ();} return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.