Poj 2942 knights of the Round Table (odd circle + point dual Unicom)

Source: Internet
Author: User

Some knights hate each other in the round table of King Arthur, so that they cannot sit together during the meeting. That is, some people are not allowed to attend the meeting. If a knight fails to attend all meetings, he will be evicted. It is now known that those knights hate each other, and the minimum number of knights to be evicted is required. (The number of participants must be greater than or equal to 3 and is an odd number)
Question: When creating a graph, two people who do not hate each other are connected to one edge. For any server guard, if he wants to attend a meeting, the people around him cannot hate each other. The participants in each meeting (not necessarily the whole knights, only the number of people> = 3 and an odd number) are considered as the dual-link component of a vertex, so each vertex has at least two adjacent points. That is, the odd circle must exist in the dual-China Unicom component. As long as there is an odd circle in the double-link component of a vertex, all vertices in this component can appear in the odd circle. Such a property can be used in odd circles,A graph is a binary graph. If the graph does not have any odd circle. Then, we only need to determine whether an image is a bipartite graph. We can determine whether the image has a odd circle and use alternating coloring.

# Include <iostream> using namespace STD; # define n 1005 # define min (a, B) (a <B? A: B) int n, m; int size, ID, SCC; int color [N], head [N]; int low [N], dfn [N], block [N]; int stack [N], temp [N], top, CNT; // temp is a temporary stack, storage point dual-connectivity branch bool map [N] [N], expell [N], instack [N]; // expell [I] = true indicates that I need to be evicted struct {int V, next;} edge [N * 1000]; bool odd_cycle (INT U, int CLR) // determine whether there is an odd circle {color [u] = CLR; For (INT I = head [u]; I; I = edge [I]. next) {int v = edge [I]. v; If (Block [v] = SCC) {If (color [v] & Color [v] = color [u]) return true; If (! Color [v] & odd_cycle (v,-CLR) return true ;}return false;} void Tarjan (int u, int father) {int V, T; stack [++ top] = u; instack [u] = true; dfn [u] = low [u] = ++ ID; For (INT I = head [u]; i; I = edge [I]. next) {v = edge [I]. v; If (V = Father) continue; If (! Dfn [v]) {Tarjan (v, U); low [u] = min (low [u], low [v]); if (low [v]> = dfn [u]) // U is the cut point {SCC ++; do {T = stack [top --]; instack [T] = false; block [T] = SCC; temp [++ CNT] = T; // records all vertices in temp at the same time as the stack is output, that is, use temp to store all vertices in the connected branch} while (T! = V); // do not take the u out of the stack, because a cut point may belong to different double Unicom branches temp [++ CNT] = u; // cut point U belongs to different dual-Unicom branches, so it must also belong to tempmemset (color, 0, sizeof (color )); // set all colors to 0 if (CNT> = 3 & odd_cycle (u, 1) // when there is an odd circle in temp, then everyone in temp can leave {While (CNT! = 0) expell [temp [CNT --] = false;} else CNT = 0;} else if (instack [v]) low [u] = min (low [u], dfn [v]) ;}} void initial () {Top = CNT = 0; size = id = SCC = 0; memset (MAP, 0, sizeof (MAP); For (INT I = 1; I <= N; I ++) {expell [I] = true; instack [I] = false; dfn [I] = low [I] = block [I] = head [I] = 0 ;}} void add (INT U, int V) {size ++; edge [size]. V = V; edge [size]. next = head [u]; head [u] = size;} int main () {int u, V, I, j; while (scanf ("% d", & N, & M) & (m + n) {initial (); while (M --) {scanf ("% d", & U, & V ); map [u] [v] = map [v] [u] = true;} for (I = 1; I <= N; I ++) for (j = I + 1; j <= N; j ++) if (! Map [I] [J]) add (I, j), add (J, I); for (I = 1; I <= N; I ++) if (! Dfn [I]) Tarjan (I,-1); int res = 0; for (I = 1; I <= N; I ++) if (expell [I]) res ++; printf ("% d \ n", Res);} 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.