Network of Schools
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 12068 | Accepted: 4804 |
Description
A number of schools are connected to a computer network. Agreements have been developed among those schools: each school maintains a list of schools to which it distributes software (the “receiving schools”). Note that if B is in the distribution list of school A, then A does not necessarily appear in the list of school B
You are to write a program that computes the minimal number of schools that must receive a copy of the new software in order for the software to reach all schools in the network according to the agreement (Subtask A). As a further task, we want to ensure that by sending the copy of new software to an arbitrary school, this software will reach all schools in the network. To achieve this goal we may have to extend the lists of receivers by new members. Compute the minimal number of extensions that have to be made so that whatever school we send the new software to, it will reach all other schools (Subtask B). One extension means introducing one new member into the list of receivers of one school.
You are to write a program that computes the minimal number of schools that must receive a copy of the new software in order for the software to reach all schools in the network according to the agreement (Subtask A). As a further task, we want to ensure that by sending the copy of new software to an arbitrary school, this software will reach all schools in the network. To achieve this goal we may have to extend the lists of receivers by new members. Compute the minimal number of extensions that have to be made so that whatever school we send the new software to, it will 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 an integer N: the number of schools in the network
(2 <= N <= 100). The schools are 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 ends with a 0. An empty list contains a 0 alone in
the line.
Output
Your
program should write two 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
5 2 4 3 0 4 5 0 0 0 1 0
Sample Output
1 2
Source
/** 求强联通份量 **/ #include<iostream> #include<cmath> #include<algorithm> #include<string.h> #include<stdio.h> using namespace std; #define N 101 struct Edge { int dist; Edge *next; }*GA[N],*GT[N],*G[N]; int used[N],path[N],part[N],mark[N][N],m,n; int in[N],out[N]; void addedge(Edge *T[],int i,int j) { Edge *L; L = new Edge; L->dist = j; L->next = T[i]; T[i] = L; } void DFSA(int s) { Edge *l; if(!used[s]) { used[s] = 1; for(l = GA[s]; l != NULL; l = l->next) DFSA(l->dist); path[0] ++; path[path[0]] = s; } } void DFST(int s) { Edge *l; if(!used[s]) { used[s] = 1; for(l = GT[s]; l != NULL; l = l->next) DFST(l->dist); part[s] = part[0]; } } void Kosaraju() { int i,j,k; Edge *L; memset(used,0,sizeof(used)); path[0] = part[0] = 0; for(i=1; i<=n; i++) DFSA(i); memset(used,0,sizeof(used)); for(i=n; i>=1; i--) { if(!used[path[i]]) { part[0]++; DFST(path[i]); } } memset(mark,0,sizeof(mark)); for(k=1; k<=n; k++) { for(L= GA[k],i = part[k]; L!=NULL; L=L->next) { j = part[L->dist]; if(i!=j && !mark[i][j]) { mark[i][j] = 1; addedge(G,i,j); } } } } int main() { #ifndef ONLINE_JUDGE freopen("in.txt","r",stdin); #endif // ONLINE_JUDGE int i,j,A,B; Edge *L; scanf("%d",&n); for(int i=1; i<=n; i++) { GA[i] = GT[i] = G[i] = NULL; } for(int i=1; i<=n; i++) { while(scanf("%d",&j) && j) { addedge(GA,i,j); addedge(GT,j,i); } } Kosaraju(); memset(in,0,sizeof(in)); memset(out,0,sizeof(out)); for(m = part[0],i = 1; i<=m; i++) { for(L = G[i]; L!=NULL; L=L->next) { out[i] ++; in[L->dist] ++; } } for(A=0,B=0,i=1; i<=m; i++) { if(!in[i]) A++; if(!out[i]) B++; } B=A>B?A:B; if(m == 1) B=0; printf("%d %d ",A,B); return 0; }