Usaco Network of school strong connectivity components

Source: Internet
Author: User

This question means there is a graph, each vertex can send the software to the vertex connected to it, now ask 1, at least send to a few vertices to meet all the vertices receive the software, 2: If you want to make this graph strong connected graph, at least add a few edges. The special case is that the given graph is a strongly connected graph, the answer is 1, 0. In general, we first calculate the strong connected component of this graph to a point and then the number of points with a degree of 0 and a point of 0, the answer is the number of points in the degree 0, the answer is the maximum between them two. The code is as follows:

/*id:m1500293 lang:c++ prog:schlnet*/#include<cstdio>#include<cstring>#include<algorithm>#include<vector>using namespacestd;Const intMax_v = -;structscc{intV//number of vertices of the graphvector<int> G[max_v];//Original Diagramvector<int> Rg[max_v];//graph of opposite edgesvector<int> vs;//Post-post traverse vertex list    BOOLUSED[MAX_V];//access token    intCMP[MAX_V];//topological ordering of the strongly connected components    voidinit () { for(intI=0; i<=v; i++) G[i].clear (), rg[i].clear (); }    voidAdd_edge (int  from,intTo ) {g[ from].push_back (to); Rg[to].push_back ( from); }    voidDfsintv) {Used[v]=true;  for(intI=0; I<g[v].size (); i++)            if(!Used[g[v][i]]) DFS (g[v][i]);    Vs.push_back (v); }    voidRdfsintVintk) {Used[v]=true; CMP[V]=K;  for(intI=0; I<rg[v].size (); i++)            if(!Used[rg[v][i]]) Rdfs (Rg[v][i], k); }    intSCC () {memset (used,0,sizeof(used));        Vs.clear ();  for(intv=1; v<=v; v++)            if(!Used[v]) Dfs (v); memset (Used,0,sizeof(used)); intK =1;  for(intI=vs.size ()-1; i>=0; i--)            if(!used[vs[i]]) Rdfs (Vs[i], k++); returnK-1; }}SS;intNum//number of strongly connected componentsint inch[ the], out[ the];intMain () {Freopen ("schlnet.in","R", stdin); Freopen ("Schlnet.out","W", stdout); intN; scanf ("%d", &N); Ss. V=N;    Ss.init ();  for(intI=1; i<=n; i++)    {        intT; scanf ("%d", &t);  while(t! =0) {Ss.add_edge (I, t); scanf ("%d", &t); }    }//printf ("%d\n", SS.SCC ());num =SS.SCC (); if(num = =1) {printf ("1\n0\n"); return 0; }     for(intu=1; u<=n; u++)//U->v         for(intj=0; J<ss. G[u].size (); J + +)        {            intv =SS.            G[U][J]; intUu=ss.cmp[u], vv=Ss.cmp[v]; if(UU! =vv) {inch[vv]++;  out[uu]++; }        }    intin_0_num=0, out_0_num=0;  for(intI=1; i<=num; i++)    {        if(!inch[i]) in_0_num++; if(! out[i]) out_0_num++; } printf ("%d\n%d\n", In_0_num, Max (In_0_num, out_0_num)); return 0;}

Usaco Network of school strong connectivity 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.