Network of schools
Time limit:1000 ms 

Memory limit:10000 K 
Total submissions:11441 

Accepted:4554 
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 "processing ing 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.
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 shocould write two lines to the standard output. The first line shoshould contain one positive integer: the solution of subtask A. The second line shoshould contain the solution of subtask B.
Sample Input
52 4 3 04 5 0001 0
Sample output
12
Question meaning:
The number of schools is N, and the N rows below are the ones that I can reach. Question 1: The minimum number of schools can be reached. 2. The minimum number of connections between schools can be reached by any school.
Ideas:
Use the Trajan algorithm to reduce the number of schools, and then calculate the number of blocks with 0 and the number of blocks with 0. A is the answer 1, max (, b) that is, answer 2 (when the number of blocks is 1, answer 2 is 0 ).
The first one is easy to think of. The second question is due to the first sentence of the question "A number of schools are connected to a computer network. ", that is to say, if the path between schools is twoway, then all schools are connected together. If you can reach all other schools from any school, the outbound block with 0 must have a path pointing to the block with 0 in all other schools, it is obvious that the maximum value can be obtained in a and B.
Code:
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <iostream> 5 #include <vector> 6 #include <queue> 7 #include <cmath> 8 #include <stack> 9 using namespace std;10 11 #define N 10512 13 int low[N], dfn[N], dfn_clock;14 int instack[N];15 stack<int>st;16 vector<int>ve[N];17 int n;18 int ans;19 int in[N], chu[N];20 int suo[N];21 22 void tarjan(int u){23 int i, j, v;24 dfn[u]=low[u]=dfn_clock++;25 st.push(u);//instack[u]=1;26 for(i=0;i<ve[u].size();i++){27 v=ve[u][i];28 if(!dfn[v]){29 tarjan(v);30 low[u]=min(low[u],low[v]);31 }32 else if(instack[v])33 low[u]=min(low[u],dfn[v]);34 }35 if(low[u]==dfn[u]){36 ans++;37 38 while(1)39 {40 v=st.top();41 suo[v]=ans;42 st.pop();43 instack[v]=0;44 if(v==u) break;45 }46 }47 }48 49 main()50 {51 int i, j, k, x;52 while(scanf("%d",&n)==1){53 for(i=0;i<=n;i++) ve[i].clear(),instack[i]=1;54 memset(in,0,sizeof(in));55 memset(chu,0,sizeof(chu));56 while(!st.empty()) st.pop();57 for(i=1;i<=n;i++){58 while(scanf("%d",&x)){59 if(!x) break;60 ve[i].push_back(x);61 // chu[i]++;in[x]++;62 }63 }64 memset(dfn,0,sizeof(dfn));65 // memset(instack,0,sizeof(instack));66 dfn_clock=1;67 ans=0;68 for(i=1;i<=n;i++)69 if(!dfn[i])70 tarjan(i);71 // for(i=1;i<=n;i++) printf("%d ",suo[i]);72 // cout<<endl<<endl;73 for(i=1;i<=n;i++){74 for(j=0;j<ve[i].size();j++){75 x=ve[i][j];76 if(suo[x]!=suo[i]){77 chu[suo[i]]++;78 in[suo[x]]++;79 }80 }81 }82 int a=0, b=0;83 for(i=1;i<=ans;i++){84 if(!in[i]) a++;85 if(!chu[i]) b++;86 }87 // printf("%d\n",a);88 if(ans==1) printf("1\n0\n");89 else90 printf("%d\n%d\n",a,max(a,b));91 }92 }
Poj 1236 Tarjan contraction point + degree