Poj1236-network of schools (Tarjan + indent)

Source: Internet
Author: User

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)

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.