POJ 1236 Network of schools (strong connected components)

Source: Internet
Author: User

POJ 1236 Network of schools

Topic links

Test instructions: Test instructions is essentially, given a graph, ask two questions
1, from which several vertices start, can walk all a little
2, at least a few sides, making the diagram strong connectivity

Ideas:

#include <cstdio> #include <cstring> #include <vector> #include <stack>using namespace std; const int N = 105;int n;vector<int> g[n];int pre[n], sccno[n], dfn[n], Dfs_clock, sccn;stack<int> S;void dfs_s CC (int u) {pre[u] = dfn[u] = ++dfs_clock; S.push (U); for (int i = 0; i < g[u].size (); i++) {int v = g[u][i];if (!pre[v]) {DFS_SCC (v);d fn[u] = min (Dfn[u], dfn[v]); } else if (!sccno[v]) dfn[u] = min (Dfn[u], pre[v]);} if (dfn[u] = = Pre[u]) {sccn++;while (1) {int x = S.top (); S.pop (); sccno[x] = sccn;if (x = = u) break;}}} void Find_scc () {SCCN = Dfs_clock = 0;memset (pre, 0, sizeof (PRE)), memset (sccno, 0, sizeof (SCCNO)); for (int i = 1; I <= N i++) if (!pre[i]) DFS_SCC (i);} int In[n], out[n];int main () {while (~SCANF ("%d", &n)) {int v;for (int i = 1; I <= N; i++) g[i].clear (); int cnt = N ; for (int u = 1; u <= n; u++) {while (~SCANF ("%d", &v) && v) g[u].push_back (v);} FIND_SCC (); memset (in, 0, sizeof (in)), memset (out, 0, sizeof), and for (int u =1; U <= N; u++) {for (int j = 0; J < G[u].size (); j + +) {int v = g[u][j];if (sccno[u]! = Sccno[v]) {in[sccno[v]]++;out[sccno[u]]++; }}}int ins = 0, outs = 0;for (int i = 1; I <= SCCN; i++) {if (!in[i]) ins++;if (!out[i]) outs++;} int ans1 = ins, ans2 = max (ins, outs), if (SCCN = = 1) ans2 = 0;printf ("%d\n%d\n", ans1, Ans2);} return 0;}


POJ 1236 Network of schools (strong connected components)

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.