Strong Unicom block Tarjan algorithm

Source: Internet
Author: User


http://poj.org/problem?id=1236
First question: Several schools need software to pass, so that all schools have software
After using the Tarjan algorithm to find the strong Unicom component, each Unicom component is shrunk to a point, then the answer to question 1 is the number of points with a degree of 0.
Why? The point of entry is 0, certainly cannot pass the software to him through the other school, therefore he must have a copy of the software
Second question: Need to add several sides, in order to make the graph strong unicom
After shrinking, A is the number of points with all degrees 0, and B is the number of all points with a degree of 0, then the answer is Max (A, B)

1#include <stdio.h>2#include <string.h>3#include <vector>4#include <stack>5 using namespacestd;6 Const intN = -+Ten;7vector<int>G[n];8stack<int>St;9 BOOLVis[n];Ten intSccno[n],pre[n],lowlink[n],inch[N], out[n],dfs_clock,scc_cnt; One intMinConst int&a,Const int&b) A { -     returnA < b?a:b; - } the voidTarjan (intu) - { -Pre[u] = Lowlink[u] = + +Dfs_clock; - st.push (u); +      for(intI=0; I<g[u].size (); ++i) -     { +         intv =G[u][i]; A         if(!Pre[v]) at         { - Tarjan (v); -Lowlink[u] =min (lowlink[u],lowlink[v]); -         } -         Else if(!Sccno[v]) -Lowlink[u] =min (lowlink[u],pre[v]); in     } -     if(lowlink[u]==Pre[u]) to     { +scc_cnt++; -          for(;;) the         { *             intx =st.top (); St.pop (); $SCCNO[X] =scc_cnt;Panax Notoginseng             if(X==u) Break; -         } the     } + } A voidFIND_SCC (intN) the { +      for(intI=1; i<=n; ++i) -         if(!Pre[i]) $ Tarjan (i); $ } - intMain () - { the     intn,i,a,b; -scanf"%d",&n);Wuyi      for(a=1; a<=n; ++a) the     { -          while(SCANF ("%d",&b), b) Wu         { - G[a].push_back (b); AboutVIS[B] =true; $         }     -     } - FIND_SCC (n); -      for(i=1; i<=scc_cnt; ++i)inch[I] = out[I] =1; A      for(intu=1; u<=n; ++u) +          for(i=0; I<g[u].size (); + +i) the         { -             intv =G[u][i]; $             if(Sccno[u]! = Sccno[v])inch[Sccno[v]] = out[Sccno[u]] =0; the         } theA = b =0; the      for(i=1; i<=scc_cnt; ++i) the     { -         if(inch[i]) a++; in         if( out[i]) b++; the     }     the     intAns = a > B?a:b; About     if(scc_cnt = =1) ans =0; theprintf"%d\n%d\n", A,ans); the     return 0; the}

Strong Unicom block 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.