P2746 [USACO5.3] Campus Network of schools//Poj1236:network of schools

Source: Internet
Author: User

P2746 [USACO5.3] Campus Network of schools//Poj1236:network of schools title description

Some schools connect to a computer network. The schools have agreed that each school will distribute software (known as "school acceptance") to some other schools. Note Even if B is in the distribution list of school A, a is not necessarily in the list of B schools.

You have to write a program calculation, according to the Protocol, in order to allow all schools in the network to use the new software, you must accept the minimum number of new software copies of the school (subtask A). Further, we want to make sure that by sending new software to any school, the software is distributed to all schools on the network. In order to complete this task, we may have to expand the receiving school list to join the new members. The calculation requires a minimum of several extensions, so that no matter which school we send new software to, it will reach all the rest of the school (subtask B). An extension is the introduction of a new member in a school's receiving school list.

Input/output format

Input format:

The first line of the input file includes an integer n: the number of schools in the network (2 <= N <= 100). The school is identified with the first N positive integers.

Each row in the next N row represents a Receive school list (distribution list). Line i+1 includes an identifier for school I's receiving school. Each list ends with 0. An empty list is represented by only one 0.

Output format:

Your program should output two lines in the output file.

The first line should include a positive integer: The solution of subtask A.

The second line should include the solution of sub-task B.

Input/Output sample Input Sample # #:Copy
52 4 3 04 5 0001 0
Sample # # of output:Copy
12
Description

The title translation comes from Nocow.

Usaco Training Section 5.3

Problem Solving Report: The main topic: given a graph, ask 1. At least select a few points, you can reach all the points, 2. At least a few sides, so that the entire graph is strong unicom (that is, from any vertex, you can reach any vertex)a useful theorem: All the points in the non-circular graph are not 0, it can be up to a point of 0 of a certain degree. (because there is no ring, so from any degree is not 0 points back, it is bound to end in a point of 0 points) thinking: Tarjan, the number of points to find the degree of 0, that is, 1 of the answer; To add a few sides on the Dag to make the Dag become strongly connected, question 2 is the answer to how much, The method of adding edges: To add an edge for each point with a degree of 0, to add an edge for each point with a degree of 0, to assume that there are N degrees 0 points, m out of a 0 point, Max (M,n) is the solution of the second problem (proving difficult, slightly)
#include <iostream>#include<queue>#include<cstdio>#include<cstring>#include<algorithm>#include<stack>#include<vector>#defineN 2000000using namespacestd;void inch(int&x) {RegisterCharC=getchar (); x=0;intf=1;  while(!isdigit (c)) {if(c=='-') f=-1; c=GetChar ();}  while(IsDigit (c)) {x=x*Ten+c-'0'; c=GetChar ();} X*=F;}intAns1,ans2,n,tot,head[n];structnode{intTo,next;} E[n];intDfn[n],low[n],cnt,item,belong[n],rd[n],cd[n];stack<int>S;BOOLVis[n];voidTarjan (intu) {Dfn[u]=low[u]=++item; Vis[u]=1;    S.push (U);  for(intI=head[u];i;i=E[i].next) {        intv=e[i].to; if(!Dfn[v])            {Tarjan (v); Low[u]=min (low[u],low[v]); }Else if(Vis[v]) low[u]=min (low[u],dfn[v]); }if(low[u]==Dfn[u]) {        intv=u;++CNT;  Do{v=S.top ();            S.pop (); VIS[V]=0; belong[v]=CNT; } while(v!=u); }}voidAddintUintv) {e[++tot].to=v,e[tot].next=head[u],head[u]=tot;}intMain () {inch(n);  for(intX,i=1; i<=n;i++){         while(1){            inch(x); if(!x) Break;        Add (i,x); }    } for(intI=1; i<=n;i++)        if(!Dfn[i]) Tarjan (i);  for(intI=1; i<=n;i++){         for(intj=head[i];j;j=E[j].next) {            intv=e[j].to; if(belong[i]!=Belong[v]) {Rd[belong[v]]++; Cd[belong[i]]++; }        }    } for(intI=1; i<=cnt;i++){        if(!rd[i]) + +ans1; if(!cd[i]) + +Ans2; }ans2=Max (ANS1,ANS2); if(cnt==1) ans2=0; printf ("%d\n%d", ANS1,ANS2); return 0;}

Rokua P2812 is a reinforced version of this problem, you can drop a

P2746 [USACO5.3] Campus Network of schools//Poj1236:network of schools

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.