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
IOI 1996
Topic: Given a graph, how many points must be at least to get from these points to reach all points; To add at least how many edges to reach all points from any point
First of all to introduce a theorem: in the DAG, for all the points in the degree of not 0, 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 degrees)
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, proving to be more difficult, slightly.
For this problem, because only requirements of the degree and the degree of 0 points, it is only in the Tarjan process to record each point belongs to which strong connected components, and 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;}