POJ 3180-the Cow Prom (graph theory-strong unicom Tarjan algorithm)

Source: Internet
Author: User

The main topic: There are n cattle in a piece, M single rope, there is m link relationship, ask how many groups inside any two cows can reach each other

The thinking of solving problems: the map of strongly connected component mode

The code is as follows:

#include <stdio.h>#include<vector>#include<map>#include<Set>#include<algorithm>using namespaceStd;typedefLong Longll;Const intN =10003; Vector<int>G[n], DQ;intLow[n], dfn[n], tot;BOOLMk[n];intN, m, ans;voidinit () {ans= Tot =0;    Dq.clear ();  for(intI=1; i<=n; ++i) {g[i].clear (); Low[i]= Dfn[i] =-1; Mk[i]=false; }}voidTarjan (intUintf) {Dfn[u]= Low[u] = + +tot;    Dq.push_back (U); Mk[u]=true;  for(inti =0; I<g[u].size (); ++i) {intv =G[u][i]; if(Dfn[v] = =-1) {Tarjan (V, u); Low[u]=min (Low[u], low[v]); }        Else if(Mk[v]) Low[u]=min (Low[u], dfn[v]); }    if(Dfn[u] = =Low[u]) {        ints; intK =0;  Do{s=Dq.back (); K++;            Dq.pop_back (); Mk[s]=false; }         while(U! =s); if(k >1) ans++; }}voidsolve () { for(intI=1; i<=n; ++i) {if(Dfn[i] = =-1) Tarjan (i,-1); } printf ("%d\n", ans);}intMain () { while(~SCANF ("%d%d", &n, &m)) {init ();  for(intI=1; i<=m; ++i) {intu, v; scanf ("%d%d", &u, &v);        G[u].push_back (v);    } solve (); }    return 0;}
View Code

POJ 3180-the Cow Prom (graph theory-strong link Tarjan algorithm)

Related Article

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.