POJ 1236-network of schools (graph theory-strong unicom Tarjan)

Source: Internet
Author: User

Title Link: http://poj.org/problem?id=1236

The main problem: N (2<n<100) schools have a one-way network, each school gets a set of software, can be transmitted through a one-way network to the surrounding schools.
Question 1: Initially, at least how many schools need to be distributed software, so that all schools in the network will eventually get the software.
Question 2: At least a few transmission lines (edges) need to be added, so that after any release of software to a school, after several transfers, all schools in the network will eventually get the software.

Problem-solving ideas: First use Tarjan to obtain all the strong unicom components, each strong unicom component as a point, so that you will get a directed acyclic map Dag, then for the problem one needs to find out how many of the Dag map on the number of points 0, for the problem two need to find in the Dag map of the point of 0 points, Then the number of points with the problem of 0 is the maximum, which is the answer to question two.

It is important to note that there is only one strong unicom component that needs to be given a special sentence.

The code is as follows:

#include <stdio.h>#include<vector>#include<string.h>#include<algorithm>using namespacestd;Const intN =103; Vector<int>Vec[n], STK;BOOLMp[n][n], in_stk[n];intLow[n], dfn[n], tot, COU_SCC;intBelong[n];intN;voidTarjan (intUintf) {Dfn[u]= Low[u] = tot + +; Stk.push_back (U), In_stk[u]=true;  for(intI=0; I < vec[u].size (); ++i) {intv =Vec[u][i]; if(Dfn[v] = =-1) {Tarjan (V, u); Low[u]=min (Low[u], low[v]); }        Else if(In_stk[v]) Low[u]=min (Low[u], dfn[v]); }    if(Low[u] = =Dfn[u]) {        ++COU_SCC;  while(1)        {            intv =Stk.back ();            Stk.pop_back (); IN_STK[V]=false; BELONG[V]=COU_SCC; if(U = =v) Break; }    }}intMain () { while(SCANF ("%d", &n)! =EOF) {memset (MP,false,sizeof(MP));  for(intI=1; i<=n; ++i) {vec[i].clear (); intC;  while(SCANF ("%d", &c), c) {vec[i].push_back (c); MP[I][C]=true; }} memset (Low,-1,sizeof(low)); memset (DFN,-1,sizeof(DFN)); Memset (Belong,-1,sizeof(belong)); memset (IN_STK,false,sizeof(IN_STK)); COU_SCC=0, tot =1;        Stk.clear ();  for(intI=1; i<=n; ++i) {if(Dfn[i] = =-1) Tarjan (i,-1); }        int inch[N], out[N]; memset (inch,0,sizeof(inch)); memset ( out,0,sizeof( out));  for(intI=1; i<=n; ++i) { for(intj=1; j<=n; ++j) {if(Mp[i][j] && belong[i]! =Belong[j]) {                    ++inch[Belong[j]]; ++ out[Belong[i]]; }            }        }        if(COU_SCC = =1) {printf ("1\n0\n"); Continue; }        intx =0, y =0;  for(intI=1; i<=cou_scc; ++i) {if(inch[I] = =0)                ++x; if( out[I] = =0)                ++y; } printf ("%d\n%d\n", X, max (x, y)); }}
View Code

POJ 1236-network of schools (graph theory-strong unicom Tarjan)

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.