Uvalive 4262--trip Planning —————— "Tarjan to find the number of strong connected components"

Source: Internet
Author: User

Road NetworksTime limit:3000MS Memory Limit:0KB 64bit IO Format:%lld &%llu SubmitStatusPracticeuvalive 4262

Description


There is a road network comprised byM<tex2html_verbatim_mark> Roads andN<tex2html_verbatim_mark> cities. For convenience, we use{1, 2,...,N}<tex2html_verbatim_mark> to denote theN<tex2html_verbatim_mark> cities. Each road between citiesI<tex2html_verbatim_mark> andJ<tex2html_verbatim_mark>, where1I<tex2html_verbatim_mark>,JN<tex2html_verbatim_mark> andIJ<tex2html_verbatim_mark>, has both types:one type isBidirectional, which allows a citizen to drive a car from i <tex2html_verbatim_mark> to  J < tex2html_verbatim_mark>  (denoted By  i J <tex2html_verbatim_ mark> ) and From  J <tex2html_verbatim_mark> to  i <tex2html_verbatim_mark>  (denoted By  J I <tex2html_verbatim_mark> ). The other type Is unidirectional, which allows a citizen to drive a car following exactly one di Rection, either From  i <tex2html_verbatim_mark> to  J <tex2html_verbatim_mark> or from  J < Tex2html_verbatim_mark> to  i <tex2html_verbatim_mark> .

We say that cityJ<tex2html_verbatim_mark> isReachable from cityI<tex2html_verbatim_mark> if one can drive a car fromI<tex2html_verbatim_mark> toJ<tex2html_verbatim_mark>, visiting a sequence of citiesC1,C2,...,CK<tex2html_verbatim_mark> fork0<tex2html_verbatim_mark>, such thatIC1C2 ...CKJ<tex2html_verbatim_mark>. (Every city was always reachable from itself.) Aregion is a maximal set of cities So, the following  mutually reachable property holds:for both arbitrary cities  i <tex2html_verbatim_mark> and  J <tex2html_verbatim_mark > ,  i <tex2html_verbatim_mark> is reachable From  J <tex2html_verbatim_mark> and  J <tex2html_ Verbatim_mark> is also reachable From  i <tex2html_verbatim_mark>  . The adjective "maximal" means that if we include any and the given region, the mutually reachable property cann OT be retained. Given a road network, your task is to write a computer program to compute the number of regions in the road Network.


Technical specification

  1. We use {1, 2,..., n}<tex2html_verbatim_mark> to denote the n<tex2html_verbatim_mark > Cities.
  2. M2000<tex2html_verbatim_mark> is a non-negative integer
  3. N1000<tex2html_verbatim_mark> is a positive integer.
  4. If a road betweenI<tex2html_verbatim_mark> andJ<tex2html_verbatim_mark> is bidirectional, then we use the order pairs(I,J) <tex2html_verbatim_mark> and(J,I) <tex2html_verbatim_mark> to represent it. Otherwise, if a road between i<tex2html_verbatim_mark>and J<tex2html_verbatim_mark> is unidirectional From i<tex2html_verbatim_mark> to J<tex2html_verbatim_mark> (respectively, J<tex2html_verbatim_mark> to i<tex2html_verbatim_mark>), we use (  i<tex2html_verbatim_mark>, J<tex2html_verbatim_mark>) (respectively, ( J<tex2html_verbatim_mark>, i<tex2html_verbatim_mark>) to represent it .

Input

The input consists of a number of test cases. The first line of the input file contains a integer indicating the number of the test cases to follow. Each test case consists of a road network, which have the following format:the first line of all test case contains n umbers,  N <tex2html_verbatim_mark>and  M <tex2html_ verbatim_mark>  separated by a single space. The Next  M <tex2html_verbatim_mark> lines contain the description of   M <tex2html_verbatim_mark> roads such that one, line contains, cities Representing an order Pair  i ,   J ) <tex2html_verbatim_mark>  . Each of the line are represented by and positive numbers separated by a single space; The first number representing the former element in the order pair and the second number representing the latter element I n the order pair. A '   0 ' at The  M + 2) <tex2html_verbatim_mark> -th line of Each test case, except for the last Test case, indicates the end of this test case.

The next test case starts after the previous ending symbol '0'. Finally, a '-1' signals the end of the whole inputs.

Output

The output contains one line for each test case. Each line contains a integer, which is the number of the regions in the given road network.

Sample Input

2 3 21 21 30 3 31 22 33 1-1

Sample Output

3 1



The main topic: give you n points, M Bar has an edge. Ask you the number of SCC in this diagram.

Problem-solving ideas: To find strong connected components of the template problem, Tarjan algorithm water over.


/* Tarjan number of strongly connected components */#include <stdio.h> #include <algorithm> #include <string.h> #include <stack > #include <vector> #include <queue>using namespace std;const int maxn = 1E5+200;VECTOR&LT;INT&GT;G[MAXN] ; int pre[maxn],lowlink[maxn],sccno[maxn],dfs_clock,scc_cnt;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 FIND_SCC (int n) {dfs_clock = scc_cnt = 0; while (!    S.empty ()) S.pop ();    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 t,n,m;    scanf ("%d", &t);        while (t--) {scanf ("%d%d", &n,&m);        int A, B;            for (int i = 0; i < m; i++) {scanf ("%d%d", &a,&b);        G[a].push_back (b);        } scanf ("%d", &a);        FIND_SCC (n);        printf ("%d\n", scc_cnt);        for (int i = 0; I <= N; i++) {g[i].clear (); }} return 0;}

  

Uvalive 4262--trip Planning —————— "Tarjan to find the number of 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.