[POJ 1236] [IOI 1996] Network of schools

Source: Internet
Author: User
Tags in degrees

Description

A number of schools is connected to a computer network. Agreements has been developed among those Schools:each School maintains a list of schools to which it distributes Softwa Re (the "Receiving schools"). Note that if B was in the distribution list of school A, then a does not necessarily appear in the list of school B
You is to write a program this computes the minimal number of schools that must receive a copy of the new software in Ord Er for the software to reach all schools in the network according to the agreement (Subtask A). As a further task, we want to ensure is sending the copy of new software to an arbitrary school, this software would r Each of the schools in the network. To achieve this goal we are having to extend the lists of receivers by new members. Compute the minimal number of extensions that has to is made so this whatever school we send the new software to, it'll Reach all other schools (Subtask B). One extension means introducing one new member into the list of receivers of one school.

Input

The first line contains a integer n:the number of schools in the network (2 <= N <= 100). The schools is identified by the first N positive integers. Each of the next N lines describes a list of receivers. The line i+1 contains the identifiers of the receivers of school I. Each list is ends with a 0. An empty list contains a 0 alone in the line.

Output

Your program should write, lines to the standard output. The first line should contain one positive integer:the solution of subtask A. The second line should contain the solution of subtask B.

Sample Input

52 4 3 04 5 0001 0

Sample Output

12

Source

field=source&key=ioi+1996 ">IOI 1996


Topic: Given a map, how many points must be at least, talent from these points to reach all points; at least how many sides to join, talent from a random point of departure to all points

First of all to introduce a theorem: in the DAG, for all the entry is not 0 points, there must be a degree of 0 points can be reached (because the point from the degree of 0 is backwards, you will be able to go to the point of not 0 of the degree)

So the question can be tarjan to find out how many points in the degree of 0, and that's the answer to the first question.

The answer to the second question is the minimum value of the point with an entry level of 0 and a point with a degree of 0. proved to be more difficult. Slightly.

For this problem, because only the requirements of the degree and the degree of 0 points, it is only necessary in the Tarjan process to record each point belonging to which strong connected components. Then the statistical output can be

#include <iostream> #include <stdio.h> #include <string.h> #define Maxe 500#define MAXV 3000using namespace Std;int n;struct edge{int u,v,next;} Edges[maxv];int head[maxe],ncount=0;int Dfn[maxe],low[maxe],index=0;int belong[maxe],tot=0; The strong connected component of the belong[i]=i point, the total number of tot= strongly connected components bool Instack[maxe];int Stack[maxe*4],top=0;bool map[maxe][maxe];int inDegree[ maxe],outdegree[maxe],inzero=0,outzero=0; In degrees.    Out of int max (int a,int b) {if (a>b) return A; return b;}    int min (int a,int b) {if (a<b) return A; return b;}    void Addedge (int u,int V) {edges[++ncount].u=u;    Edges[ncount].v=v;    Edges[ncount].next=head[u]; Head[u]=ncount;}    void Tarjan (int u) {dfn[u]=low[u]=++index; Stack[++top]=u;    The point into the stack instack[u]=true;        for (int p=head[u];p!=-1;p=edges[p].next) {int v=edges[p].v;            if (!dfn[v]) {Tarjan (v);        Low[u]=min (Low[u],low[v]);        } else if (Instack[v]) {low[u]=min (low[u],dfn[v]);}} int v;        if (Dfn[u]==low[u]) {tot++;            do {v=stack[top--];            Belong[v]=tot;        Instack[v]=false;    } while (U!=V);    }}int Main () {int to;    cin>>n;    memset (head,-1,sizeof (head));            for (int i=1;i<=n;i++) {while (1) {cin>>to;            if (to==0) break;            Addedge (i,to);        Map[i][to]=true;    }} for (int i=1;i<=n;i++) if (!dfn[i]) Tarjan (i);            for (int i=1;i<=n;i++) for (int j=1;j<=n;j++) {if (Map[i][j]&&belong[i]!=belong[j])                {indegree[belong[j]]++;            outdegree[belong[i]]++;        }} for (int i=1;i<=tot;i++) {if (!indegree[i]) inzero++;    if (!outdegree[i]) outzero++;    } if (tot==1) cout<<1<<endl<<0<<endl;    else Cout<<inzero<<endl<<max (Inzero,outzero) <<endl; Return 0;} 



[POJ 1236] [IOI 1996] Network of schools

Related Article

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.