POJ 1236 Network of Schools 强连通分量+缩点

POJ 1236 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

IOI 1996
   

题目大意:N(2<N<100)各学校之间有单向的网络,每个学校得到一套软件后,可以通过单向网络向周边的学校传输,问题1:初始至少需要向多少个学校发放软件,使得网络内所有的学校最终都能得到软件。2,至少需要添加几条传输线路(边),使任意向一个学校发放软件后,经过若干次传送,网络内所有的学校最终都能得到软件。

具体算法:先用Korasaju Algorithm求出有向图所有的强连通分量,然后将所有的强连通分量缩成一个点(缩点),这样原来的有向图就缩成了一个DAG图(有向无环图);用2个数组分别记录新生成的DAG图中的每个顶点(包括原来的顶点和强连通分量的缩点)是否有出边和入边,最后遍历每个顶点,如果没有入边,则ans1++;如果没有出边,ans2++。最后所求即为ans1和max(ans1,ans2)。
#include  < iostream >
#include 
< vector >
using   namespace  std;

const   int  MAXN  =   101 ;
int  n,m,cnt;
bool  visit[MAXN];
int   set [MAXN],order[MAXN], in [MAXN], out [MAXN];
vector
<  vector < int >   >  adj;
vector
<  vector < int >   >  radj;

void  dfs( int  u) {
    visit[u]
=true;
    
int i,len=adj[u].size();
    
for(i=0;i<len;i++)
        
if(!visit[adj[u][i]])
            dfs(adj[u][i]);
    order[cnt
++]=u;
}

void  rdfs( int  u) {
    visit[u]
=true;
    
set[u]=cnt;
    
int i,len=radj[u].size();
    
for(i=0;i<len;i++)
        
if(!visit[radj[u][i]])
            rdfs(radj[u][i]);
}

void  korasaju() {
    
int i;
    memset(visit,
false,sizeof(visit));
    
for(cnt=0,i=1;i<=n;i++)
        
if(!visit[i])
            dfs(i);
    memset(visit,
false,sizeof(visit));
    
for(cnt=0,i=n-1;i>=0;i--)
        
if(!visit[order[i]])
            cnt
++,rdfs(order[i]);
}

int  main() {
    
int i,j;
    scanf(
"%d",&n);
    adj.assign(n
+1,vector<int>());
    radj.assign(n
+1,vector<int>());
    
for(i=1;i<=n;i++){
        
while(scanf("%d",&m),m){
            adj[i].push_back(m);
            radj[m].push_back(i);
        }

    }

    korasaju();
    memset(
in,1,sizeof(in));
    memset(
out,1,sizeof(out));
    
for(i=1;i<=n;i++)
        
for(j=0;j<adj[i].size();j++)
            
if(set[i]!=set[adj[i][j]]){
                
out[set[i]]=0;
                
in[set[adj[i][j]]]=0;
            }

    
int ans1=0,ans2=0;
    
for(i=1;i<=cnt;i++){
        
if(out[i]) ans2++;
        
if(in[i]) ans1++;
    }

    
if(cnt==1){
        printf(
"1\n");
        printf(
"0\n");
    }

    
else{
        printf(
"%d\n",ans1);
        printf(
"%d\n",max(ans1,ans2));
    }

    
return 0;
}

你可能感兴趣的:(POJ 1236 Network of Schools 强连通分量+缩点)