LA4287 -- tarjan, la4287 replacement
Question:
In mathematics, we often need to prove the equivalence of several propositions. For example, there are four propositions a, B, c, d. We prove↔B, then B↔C, finally c↔D. Note that each proof is bidirectional, so a total of 6 derivation is completed. Another method is a → B, then B → c, then c → d, and finally d → a, only four times. Now, your task is to prove that n propositions are all equivalent, and your friends have made m derivation for you (the content of each derivation is known ), how many times do you need to deduce the entire proof?
First, tarjan finds the strongly connected component, shrinks the point, and counts the inbound and outbound degrees of each point. If you set a node to 0 and B to 0, the answer is max (a, B ).
Code:
# Include <iostream> # include <cstdio> # include <cmath> # include <vector> # include <string. h> using namespace std; vector <int> g [20001]; int n, m, t, I, j, x, y, dfn [20001], dfs_clock, low [20001], in0 [20001], out0 [20001], c [20001], a, B, l, f [20001], cnt; void dfs (int u) {dfn [u] = low [u] = ++ dfs_clock; c [++ l] = u; for (int I = 0; I <g [u]. size (); ++ I) if (! Dfn [g [u] [I]) {dfs (g [u] [I]); low [u] = min (low [u], low [g [u] [I]);} else if (! F [g [u] [I]) low [u] = min (low [u], dfn [g [u] [I]); if (low [u] = dfn [u]) {cnt ++; while (c [l]! = U) f [c [l --] = cnt; f [c [l --] = cnt ;}} int main () {scanf ("% d ", & t); for (int u = 0; u <t; ++ u) {scanf ("% d", & n, & m ); for (I = 1; I <= n; ++ I) g [I]. clear (); for (I = 1; I <= m; ++ I) {scanf ("% d", & x, & y ); g [x]. push_back (y);} memset (dfn, 0, sizeof (dfn); memset (low, 0, sizeof (low); memset (in0, 0, sizeof (in0); memset (out0, 0, sizeof (out0); memset (f, 0, sizeof (f); memset (c, 0, sizeof (c); a = 0; B = 0; l = 0; cnt = 0; dfs_clock = 0; for (I = 1; I <= n; ++ I) If (! Dfn [I]) dfs (I); for (I = 1; I <= n; ++ I) for (j = 0; j <g [I]. size (); ++ j) if (f [g [I] [j]! = F [I]) {in0 [f [g [I] [j] ++; out0 [f [I] ++;} for (I = 1; I <= cnt; ++ I) {if (! In0 [I]) a ++; if (! Out0 [I]) B ++;} if (cnt = 1) printf ("0 \ n"); else printf ("% d \ n", max (, b);} return 0 ;}LA4287