Posted on 2012-04-30 13:12
lenohoo 阅读(123)
评论(0) 编辑 收藏 引用
Network of Schools
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
Source
adjacnet Matric
#include<cstdio>
#include<cstring>
#include<iostream>
#include<stack>
using namespace std;
#define re(i,n) for(int i=0;i<n;i++)
#define clr(a,b) memset(a,b,sizeof(a))
const int MAXN = 101;
int N,idx,cnt,tmp;
int g[MAXN][MAXN],in[MAXN],out[MAXN];
int dfn[MAXN],low[MAXN],belong[MAXN];
stack<int> Q;
bool instack[MAXN];
int tarjan(int v){
dfn[v]=low[v]=idx++;
Q.push(v);
instack[v]=true;
re(u,N) if(g[v][u])
if(dfn[u]==-1) { tarjan(u);low[v]=min(low[v],low[u]); }
else if(instack[u]) low[v]=min(low[v],dfn[u]);
if(low[v]==dfn[v]) {
do { tmp=Q.top();Q.pop();belong[tmp]=cnt;instack[tmp]=false; }while(tmp!=v);
cnt++;
}
}
int main(){
int j;
idx=cnt=0;
scanf("%d",&N);
clr(g,0);clr(in,0);clr(out,0);clr(dfn,-1);clr(instack,0);
re(i,N) while(scanf("%d",&j) && j) g[i][j-1]=1;
re(i,N) if(dfn[i]==-1) tarjan(i);
re(i,N) re(j,N) if(g[i][j] && belong[i]!=belong[j]) out[belong[i]]++,in[belong[j]]++;
int t1=0,t2=0;
re(i,cnt){
if(!in[i]) t1++;
if(!out[i]) t2++;
}
if(cnt==1) printf("1\n0\n");
else printf("%d\n%d\n",t1,max(t1,t2));
return 0;
}