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