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.
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 题意:N(2<N<100)各学校之间有单向的网络,每个学校得到一套软件后,可以通过单向网络向周边 的学校传输,问题1:初始至少需要向多少个学校发放软件,使得网络内所有的学校最终都能得到软件。 2,至少需要添加几条传输线路(边),使任意向一个学校发放软件后,经过若干次传送,网络内所有的 学校最终都能得到软件。 解法:强联通缩点,建立DAG之后,找到入度为0的点的个数就是1的答案。 对于2,我们考虑每个点的入度和出度个数n和m,则需要添加max(n,m)条边(具体略)#include<cstdio> #include<cstring> #include<algorithm> #include<vector> #include<string> #include<iostream> #include<queue> #include<cmath> #include<map> #include<stack> #include<bitset> using namespace std; #define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i ) #define REP( i , n ) for ( int i = 0 ; i < n ; ++ i ) #define CLEAR( a , x ) memset ( a , x , sizeof a ) typedef long long LL; typedef pair<int,int>pil; const int maxn=110; const int maxm=10100; struct node{ int u,v; int next; }e[maxm]; int head[maxn],cntE; int DFN[maxn],low[maxn]; int s[maxm],top,index,cnt; int belong[maxn],instack[maxn]; int in[maxn],out[maxn]; int n,m; void init() { top=cntE=0; index=cnt=0; CLEAR(DFN,0); CLEAR(head,-1); CLEAR(instack,0); } void addedge(int u,int v) { e[cntE].u=u;e[cntE].v=v; e[cntE].next=head[u]; head[u]=cntE++; } void Tarjan(int u) { DFN[u]=low[u]=++index; instack[u]=1; s[top++]=u; for(int i=head[u];i!=-1;i=e[i].next) { int v=e[i].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]) { cnt++; do{ v=s[--top]; belong[v]=cnt; instack[v]=0; }while(u!=v); } } void work() { REPF(i,1,n) if(!DFN[i]) Tarjan(i); CLEAR(in,0); CLEAR(out,0); REP(i,cntE) { int u=e[i].u,v=e[i].v; if(belong[u]!=belong[v]) { in[belong[v]]++; out[belong[u]]++; } } if(cnt==1) { printf("1\n0\n"); return ; } int d_1=0,d_2=0; REPF(i,1,cnt) { if(!in[i]) d_1++; if(!out[i]) d_2++; } printf("%d\n",d_1); printf("%d\n",max(d_1,d_2)); } int main() { int x; while(~scanf("%d",&n)) { init(); REPF(i,1,n) { while(~scanf("%d",&x)&&x) addedge(i,x); } work(); } return 0; }