time Limit: 1000MS |
|
Memory Limit: 10000K |
Total Submissions: 16873 |
|
Accepted: 6672 |
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 Order for the Software-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 1996main
topic:
100 schools with one-way Internet connection to share Software. Given n points, the next n rows, the number of line I indicates I is connected to these points and ends this line with a zero.
Output requirements: first line output at least a few schools get the software, the rest of the school can get the software, the second line of output and then add a few one-way edge, you can make any school get the software, the rest of the school can get the Software.
Train of Thought: Tarjan run again, to shrink points, finally have a few degrees to zero point output is the first line of the answer, the second line needs a special sentence, if it can be shrunk to a point, then output 0, or the degree of 0 points and into the degree of 0 points to take large ...
1#include <iostream>2#include <cstring>3#include <cstdio>4#include <stack>5 #defineMAXN 10056 #defineMAXM 100057 using namespacestd;8 intn,m;9 structedge{Ten intu,v,next; one }e[maxm],ee[maxm]; a inthead[maxn],js,headd[maxn],jss,bianshu; - BOOLexist[maxn]; - intvisx,cur;//cur--the number of points to indent the intdfn[maxn],low[maxn],belong[maxn],chudu[maxn],rudu[maxn]; -stack<int>st; - voidAdd_edge (intUintV) { -e[++js].u=u;e[js].v=v; +e[js].next=head[u];head[u]=js; - } + voidAdd_edgee (intUintV) { aee[++jss].u=u;ee[jss].v=v; atee[jss].next=head[u];head[u]=jss; - } - voidTarjan (intU) { -dfn[u]=low[u]=++visx; -St.push (u); exist[u]=true; - for(intI=head[u];i;i=E[i].next) { in intv=e[i].v; - if(dfn[v]==0) { to Tarjan (v); +low[u]=min (low[v],low[u]); - } the Else if(exist[v]&&low[u]>dfn[v]) low[u]=dfn[v]; * } $ intj;Panax Notoginseng if(low[u]==Dfn[u]) { -++cur; the do{ +j=st.top (); st.pop (); aexist[j]=false; belong[j]=cur; the} while(j!=u); + } - } $ intMain () $ { -scanf"%d",&n); - for(intI=1; i<=n;i++){ the while(SCANF ("%d", &m) = =1&&m!=0){ -Add_edge (i,m); bianshu++;Wuyi } the } - for(intI=1; i<=n;i++) wu if(dfn[i]==0) - Tarjan (i); about for(intI=1; i<=js;i++){ $ intu=e[i].u,v=e[i].v; - if(belong[u]!=Belong[v]) { - Add_edgee (belong[u],belong[v]); -rudu[belong[v]]++;chudu[belong[u]]++; a } + } the intchu=0, ru=0; - for(intI=1; i<=cur;i++){ $ if(chudu[i]==0) chu++; the if(rudu[i]==0) ru++; the } theprintf"%d\n", ru); the if(cur==1) printf ("%d\n",0); - Elseprintf"%d\n", Max (ru,chu)); in the return 0; the}
POJ 1236 Network of schools