Http://poj.org/problem? Id = 2942
All kinds of teasing ....
There are translation white books; I read the white books and the online standard course and learned .. Orz.
The strongly connected component is to first locate the cut point, and then use a stack to maintain the sub-tree before finding the cut point. Finally, if this is a cut point, the sub-tree will be strongly connected, then this question is about the odd circle, so we can perform black and white dyeing to determine whether it is a strange circle. You can accumulate all points of strongly connected components that are not in the odd circle.
# Include <cstdio> # include <cstring> # include <cmath> # include <string> # include <iostream> # include <algorithm> # include <queue> # include <set> # include <stack> # include <vector> using namespace STD; # define rep (I, n) for (INT I = 0; I <(n); ++ I) # define for1 (I, A, n) for (INT I = (a); I <= (n); ++ I) # define for2 (I, A, n) for (INT I = (); I <(n); ++ I) # define for3 (I, A, n) for (INT I = (a); I> = (n); -- I) # define for4 (I, A, n) for (INT I = (a); I> (N); -- I) # define CC (I, A) memset (I, A, sizeof (I) # define read (A) A = getint () # define print (a) printf ("% d", a) # define dbg (x) cout <(# X) <"=" <(X) <Endl # define printarr2 (a, B, c) for1 (_, 1, B) {for1 (__, 1, C) cout <A [_] [_]; cout <Endl ;}# define printarr1 (a, B) for1 (_, 1, B) cout <A [_] <'\ T'; cout <endlinline const int getint () {int r = 0, k = 1; char c = getchar (); for (; C <'0' | C> '9'; C = Getchar () if (C = '-') k =-1; for (; C> = '0' & C <= '9 '; C = getchar () r = r * 10 + C-'0'; return K * r;} inline const int max (const Int & A, const Int & B) {return A> B? A: B;} inline const int min (const Int & A, const Int & B) {return a <B? A: B;} const int n = 1005, M = 1000005; int ihead [N], n, m, CNT, ll [N], FF [N], MP [N] [N], TOT, s [m <1], top, vis [N], OK [N], Col [N]; struct ed {int from, to, next;} e [m <1]; void add (int u, int v) {e [++ CNT]. next = ihead [u]; ihead [u] = CNT; E [CNT]. to = V; E [CNT]. from = u; E [++ CNT]. next = ihead [v]; ihead [v] = CNT; E [CNT]. to = u; E [CNT]. from = V;} bool Ifind (int u) {int V; For (INT I = ihead [u]; I; I = E [I]. next) if (vis [V = E [I]. to]) {If (COL [v] =-1) {Col [v] =! Col [u]; return Ifind (V);} else if (COL [v] = Col [u]) return true;} return false;} void color (int x) {int y, u = E [X]. from; CC (VIS, 0); CC (COL,-1); Col [u] = 0; do {Y = s [top --]; vis [E [Y]. from] = vis [E [Y]. to] = 1;} while (y! = X); If (Ifind (u) for1 (I, 1, n) if (vis [I]) OK [I] = 1;} void Tarjan (INT U, int FA) {FF [u] = ll [u] = ++ tot; For (INT I = ihead [u]; I; I = E [I]. next) if (Fa! = E [I]. To) {int v = E [I]. To; If (! FF [v]) {s [++ top] = I; // pay attention to the incoming stack .. Do not add stacks above .. Tarjan (v, U); If (LL [v]> = FF [u]) color (I); LL [u] = min (LL [u], ll [v]);} else if (LL [u]> FF [v]) s [++ top] = I, ll [u] = FF [v]; // pay attention to inbound stack ..}} Int main () {While (1) {read (n); read (m); int ans = 0; If (n = 0 & M = 0) break; CC (MP, 0); CC (ihead, 0); CC (LL, 0); CC (FF, 0); CC (OK, 0 ); top = CNT = tot = 0; rep (I, m) {int u = getint (), V = getint (); MP [u] [v] = MP [v] [u] = 1;} for1 (I, 1, n) for1 (J, I + 1, n) if (! MP [I] [J]) add (I, j); for1 (I, 1, n) if (! FF [I]) Tarjan (I,-1); for1 (I, 1, n) if (! OK [I]) ++ ans; printf ("% d \ n", ANS);} return 0 ;}
Description
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 far 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:
- The knights shoshould be seated such that two knights who hate each other shoshould not be neighbors at the table. (Merlin has a list that says who hates whom .) the knights are sitting around und a roundtable, thus every knight has exactly two neighbors.
- An odd number of knights shocould 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 .)
Merlin will let the Knights sit down only if these two rules are satisfied, otherwise he cancels the meeting. (if only one knight shows up, then the meeting is canceled as well, as one person cannot sit around a table .) merlin realized that this means that there can be knights who cannot be part of any seating arrangements that respect these rules, and these knights will never be able to sit at the Round Table (one such case is if a knight hates every other knight, but there are missing other possible reasons ). if a knight cannot sit at the round table, then he cannot be a member of the Knights of the Round Table and must be expelled from the order. these knights have to be transferred to a less-prestigious order, such as the Knights of the square table, the Knights of the octagonal table, or the knights of the banana-shaped table. to help Merlin, you have to write a program that will determine the number of knights that must be expelled.
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.
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 51 41 52 53 44 50 0
Sample output
2
Hint
Huge input file, 'scanf' recommended to avoid TLE.
Source
Central Europe 2005
[Poj] 2942 knights of the Round Table (dual-Connected Component)