POJ 1236 Network of schools (strong connectivity tarjan+ indent)
ACM
Title Address: POJ 1236
Test Instructions :
Given an undirected graph, ask at least a few points to traverse the full map, and add at least a few edges to make the graph a strongly connected graph.
Analysis :
With Hdu 2767 proving equivalences (the problem) the same topic, but a lot of problems, in fact, converted into a DAG is not difficult to consider, in fact, as long as the choice of 0 points on the line.
Code :
/** author:illuz <iilluzen[at]gmail.com>* file:1236.cpp* Create date:2014-07-30 15:13:12* Descrip Ton:tarjan */#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; #include <vector> #include <stack> #define REPF (I,A,B) for (int i= (a); i<= (b); i++) typedef long LONG ll;const int N = 105;vector<int> g[n];stack<int> s;int low[n], dfn[n], sccno[n], Tclock, SCC Cnt;int Id[n], od[n];int N, rd;void tarjan (int u) {low[u] = dfn[u] = ++tclock; S.push (u); int sz = G[u].size (); Repf (i, 0, sz-1) {int v = g[u][i];if (!dfn[v]) {Tarjan (v); Low[u] = min (Low[u], low[v]);} else if (!sccno[v]) {Low[u] = min (Low[u], dfn[v]);}} if (low[u] = = Dfn[u]) {Scccnt++;int v = -1;while (v! = u) {v = s.top (); S.pop (); sccno[v] = scccnt;}}} void Read () {REPF (i, 1, n) {g[i].clear (); while (scanf ("%d", &rd) && Rd) {g[i].push_back (rd);}}} void Find_scc () {tclock = scccnt = 0;memset (DFN, 0, sizeof (DFN)); MEMSET (low, 0, sizeof (low)), memset (sccno, 0, sizeof (SCCNO)), REPF (i, 1, n) {if (!dfn[i]) {Tarjan (i);}}} void Solve () {if (scccnt = = 1) {printf ("1\n0\n"); return;} memset (ID, 0, sizeof (ID)), memset (OD, 0, sizeof (OD)), REPF (U, 1, n) {int sz = g[u].size (), REPF (i, 0, sz-1) {int v = g[u] [I];if (sccno[u]! = Sccno[v]) {id[sccno[v]]++;od[sccno[u]]++;}}} int idnum = 0, Odnum = 0;REPF (i, 1, scccnt) {idnum + = (id[i] = = 0); Odnum + = (od[i] = = 0);} printf ("%d\n%d\n", Idnum, Max (Idnum, Odnum));} int main () {while (~SCANF ("%d", &n)) {read (); FIND_SCC (); Solve ();} return 0;}