Topic links
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.
Ideas: for the directed graph, first of all to find a few strong connected components, then each strong connected component contraction point, the formation of a DAG, the first sentence at the beginning of the question that the diagram is connected. Then, if you want to traverse the entire graph, just look for a few points with a 0 degree of entry and exit, and the number of edges to add depends on the degree of entry and exit of all points.
Code:
#include <iostream> #include <cstdio> #include <cstring> #include <vector> #include <stack > #include <algorithm>using namespace std;const int maxn = 105;vector<int> g[maxn];stack<int> s; int PRE[MAXN], LOWLINK[MAXN], SCCNO[MAXN], Dfs_clock, Scc_cnt;int N, In[maxn], out[maxn];void tarjan (int u) {Pre[u] = Lowlink[u] = ++dfs_clock; S.push (U); for (int i = 0; i < g[u].size (); i++) {int v = g[u][i]; if (!pre[v]) {Tarjan (v); Lowlink[u] = min (Lowlink[u], lowlink[v]); } else if (!sccno[v]) lowlink[u] = min (Lowlink[u], pre[v]); } if (lowlink[u] = = Pre[u]) {scc_cnt++; int x =-1; while (x! = u) {x = S.top (); S.pop (); SCCNO[X] = scc_cnt; }}}void FIND_SCC () {memset (pre, 0, sizeof (pre)); memset (sccno, 0, sizeof (SCCNO)); memset (lowlink, 0, sizeof (lowlink)); Dfs_clock = scc_cnt = 0; for (int i = 1; I <= N; i++) if (!pre[i]) Tarjan (i);} int main () {while (scanf ("%d", &n)! = EOF) {int u; for (int i = 1; I <= n; i++) {g[i].clear (); while (scanf ("%d", &u) && u) g[i].push_back (u); } FIND_SCC (); if (scc_cnt = = 1) {printf ("1\n0\n"); Continue } else {memset (in, 0, sizeof (in)); memset (out, 0, sizeof (out)); for (int u = 1, u <= n; u++) {for (int i = 0; i < g[u].size (); i++) {int v = g[u ][i]; if (sccno[u]! = Sccno[v]) {in[sccno[v]]++; out[sccno[u]]++; }}} int a = 0, b = 0; for (int i = 1; I <= scc_cnt; i++) {if (in[i] = = 0) a++; if (out[i]== 0) b++; } intAns = max (A, b); printf ("%d\n%d\n", A, ans); }} return 0;}
Poj1236-network of schools (Tarjan + indent)