HDU 3836 equivalent sets (strong connected component)

Source: Internet
Author: User

Topic Link: Click to open the link

Idea: Strongly connected component template problem, a strong connected component of the indentation into a point, to build a new picture. Then on the new diagram, ask for the degrees and degrees of each point.  Assuming that the 0,b of a vertex has a degree of 0, the answer is Max (A, b). You can think of this, the point of 0 is sure to practice the degree of 0 points.

See the code for details:

#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #include < string> #include <vector> #include <stack> #include <bitset> #include <cstdlib> #include < cmath> #include <set> #include <list> #include <deque> #include <map> #include <queue># Define MAX (a) > (b)? ( A):(B) #define MIN (a) < (b) ( A):(B)) using namespace Std;typedef long long ll;typedef long double ld;const ld EPS = 1e-9, PI = 3.14159265358979323846264 33832795;const int mod = 1000000000 + 7;const int INF = 0x3f3f3f3f;//& 0x7fffffffconst int seed = 131;const ll INF64 = ll (1e18); const int MAXN = 20000 + 10;int T,N,M,U,V,PRE[MAXN], LOWLINK[MAXN], SCCNO[MAXN], Dfs_clock, scc_cnt;vector<i    nt> g[maxn];stack<int> s;void dfs (int u) {pre[u] = lowlink[u] = ++dfs_clock;    S.push (U);    int len = G[u].size ();        for (int i = 0; i < len; i++) {int v = g[u][i]; if (!pre[v]) {DFS (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++; for (;;) {int x = S.top ();            S.pop ();            SCCNO[X] = scc_cnt;        if (x = = u) break;    }}}void FIND_SCC (int n) {for (int i = 1; I <= n; i++) {if (!pre[i]) DFS (i);    }}int IN0[MAXN], out0[maxn];void init (int n) {dfs_clock = scc_cnt = 0;        for (int i = 1; I <= n; i++) {g[i].clear ();    Sccno[i] = Pre[i] = 0;        }}int Main () {while (~scanf ("%d%d", &n,&m)) {init (n);            for (int i = 1; I <= m; i++) {scanf ("%d%d", &u,&v);        G[u].push_back (v);        } FIND_SCC (n);        if (scc_cnt = = 1) {printf ("0\n"); continue;}        for (int i = 1; I <= scc_cnt; i++) in0[i] = out0[i] = 1;          for (int u = 1; u <= n; u++) {int len = g[u].size ();  for (int j = 0; J < Len; J + +) {int v = g[u][j];            if (sccno[u]! = Sccno[v]) in0[sccno[v]] = out0[sccno[u]] = 0;        }} int a = 0, b = 0;            for (int i = 1; I <= scc_cnt; i++) {if (in0[i]) a++;        if (Out0[i]) b++;        } int ans = max (A, b);    printf ("%d\n", ans); } return 0;}


HDU 3836 equivalent sets (strong connected component)

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.