HDU 1269 Maze Castle (strong Unicom component, Tarjan algorithm)

Source: Internet
Author: User

Problem Description:

To train Gardon's sense of direction, he built a large castle with N rooms (n<=10000) and M-Lanes (m<=100000), each of which is unidirectional, that is, if a passage is said to be connected to room A and room B, It is only possible to reach room B through this passage from room A, but it does not mean that it can be reached by Room B via it. Gardon need to ask you to write a procedure to confirm whether any two rooms are interconnected, namely: for arbitrary I and J, there is at least one path can be from room I to room J, there is a path can be from room J to room I.

Input:

The input contains more than one set of data, the first line of input has two numbers: N and M, and the next m row has two numbers a and B, indicating that a passage can come from room A to room B. The file ends with two 0.

Output:

For each set of data entered, if any two rooms are connected to each other, output "Yes", otherwise output "No".

Sample Input:

3 31 22 33 13 31 22 33 20 0
Sample Output:

Yes
No

#include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> #include <cmath > #include <vector> #include <queue> #include <stack> #include <set> #include <map># Define LL long longusing namespace Std;const int maxn = 10000 + 10;vector<int>g[maxn];int PRE[MAXN], LOWLINK[MAXN], SCCNO[MAXN], Dfs_clock, scc_cnt;//scc_cnt as the strong Unicom component counter, Sccno[i] represents the strong Unicom component number where node I resides. stack<int>s;void dfs (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]) {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 Tarjan (int n) {Dfs_clocK = scc_cnt = 0;    memset (sccno, 0, sizeof (SCCNO));    memset (pre, 0, sizeof (pre)); for (int i=1;i<=n;i++) if (!pre[i]) DFS (i);}    int main () {int n, m;        while (scanf ("%d%d", &n, &m)!=eof) {if (n = = 0 && m = = 0) break;        int u, v;        for (int i=0;i<=n;i++) g[i].clear ();            for (int i=0;i<m;i++) {scanf ("%d%d", &u, &v);        G[u].push_back (v);        } tarjan (n); //If the number of strong unicom components is 1, it proves that all points are strongly connected if (scc_cnt = = 1) printf ("yes\n");    else printf ("no\n"); } return 0;}


HDU 1269 Maze Castle (strong Unicom component, Tarjan algorithm)

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.