Network of Schools
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 10910 | Accepted: 4346 |
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
解决方案:wa了几遍,一直没想明白第二个问,后来看了discuss,才知道一个规律,要添加的最少边数为max(in,out)即出度为0的个数和入度为0的个数两者的最大值,连的时候,要保证每个出度为0的店或每个入度为0的点都连上,这样就能保证整图成一个强联通图。
code:#include<iostream> #include<cstdio> #include<vector> #include<stack> #include<cstring> #define MMAX 103 using namespace std; int low[MMAX],dfn[MMAX],instack[MMAX],fa[MMAX],in[MMAX],out[MMAX],ii[MMAX],oo[MMAX]; int cnt; vector<int>Map[MMAX]; stack<int>S; int N,index; void init() { index=0; cnt=0; for(int i=0; i<=N; i++) { Map[i].clear(); dfn[i]=0; low[i]=0; in[i]=0; out[i]=0; fa[i]=i; oo[i]=0; ii[i]=0; } memset(instack,0,sizeof(instack)); while(!S.empty())S.pop(); } void tarjan(int u) { low[u]=dfn[u]=index++; instack[u]=1; S.push(u); int len=Map[u].size(); for(int i=0; i<len; i++) { int v=Map[u][i]; if(!instack[v]) { tarjan(v); low[u]=min(low[v],low[u]); } else if(instack[v]==1) { low[u]=min(low[v],low[u]); } } if(low[u]==dfn[u]) { while(!S.empty()) { int temp=S.top(); S.pop(); fa[temp]=u; instack[temp]=2; if(temp==u) break; } cnt++; } } int main() { while(~scanf("%d",&N)) { init(); for(int i=1; i<=N; i++) { while(1) { int a; scanf("%d",&a); if(a==0) break; Map[i].push_back(a); } } for(int i=1; i<=N; i++) { if(!instack[i]) { tarjan(i); } } for(int i=1; i<=N; i++) { int len=Map[i].size(); for(int j=0; j<len; j++) { int v=Map[i][j]; if(fa[v]!=fa[i]) { in[fa[v]]++,out[fa[i]]++; } } } int _in=0,_out=0; for(int i=1; i<=N; i++) { if(!in[fa[i]]) { ii[fa[i]]=1; } if(!out[fa[i]]) { oo[fa[i]]=1; } } for(int i=1;i<=N;i++){ _in+=ii[i]; _out+=oo[i]; } if(cnt==1) cout<<_in<<"\n"<<'0'<<endl; else cout<<_in<<"\n"<<max(_in,_out)<<endl; } return 0; }