您的位置:首页 > 其它

POJ 1236 Network of Schools

2015-08-05 11:23 375 查看
Network of Schools
Time Limit: 1000MS Memory Limit: 10000K
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

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <stack>
#define maxn 110
using namespace std;
vector <int> mapp[maxn] ;
vector <int> anss[maxn] ;
stack <int> S ;
int low[maxn] , dfn[maxn] ;
int belong[maxn] ;
int indegree[maxn], outdegree[maxn] ;
bool instack[maxn] ;
int time , bcnt ;
int in , out ;
int n ;
void tarjan(int num ) {
int temp ;
dfn[num] = low[num] = ++time ;              //深搜访问次序
instack[num] = true ;                       //标记是否在栈中
S.push(num) ;                               //入栈
for( int i = 0 ; i < mapp[num].size() ; ++i ){
temp = mapp[num][i] ;
if( !dfn[temp]  ) {                     //未访问,深搜
tarjan(temp) ;
low[num] = min(low[num], low[temp]) ;//求访问点当前最小序号与被访问点(强连通子图)的最小序号的较小值(画图意会)
}
else if( instack[temp] ){                //若被访问的点在栈中,则求访问点最早序号与被访问点序号的较小值(画图意会)
low[num] = min(low[num],dfn[temp] ) ;
}
}
if(low[num] == dfn[num] ) {                   //找到一个强连通子图
++bcnt ;                                  //记录个数
do {                                      //do while();  用它先执行后判断的特性
temp = S.top() ;
S.pop() ;
instack[temp] = false ;                //标记出栈
belong[temp] = bcnt ;                   //顶点属于第几个强连通图
//anss[bcnt].push_back(temp) ;            // 保存连通图结果
}
while(temp!=num) ;
}
}
void solve() {
time = bcnt = 0 ;
memset(dfn,0,sizeof(dfn)) ;
memset(belong,0,sizeof(belong)) ;
for( int i = 1 ; i <= n ; ++i) {
if( !dfn[i] ) {
tarjan(i) ;
}
}
if( bcnt == 1) {                            //bcnt == 1->强连通图
printf("1\n0\n");
return ;
}
memset(indegree,0,sizeof(indegree)) ;
memset(outdegree,0,sizeof(outdegree)) ;
for(int i = 1 ; i <= n ; ++i ) {              //缩点 ,求入度与出度
for( int j = 0 ; j < mapp[i].size() ; ++j) {
int temp = mapp[i][j] ;
if(belong[i] != belong[temp]  ) {
indegree[belong[temp]]++ ;outdegree[belong[i]]++ ;
}
}
}
for( int i = 1 ; i <= bcnt ; ++i) {         //找入度、出度为0的点
if( !indegree[i] ) ++in ;
if( !outdegree[i] ) ++out ;
}
printf("%d\n%d\n",in,max(in,out)) ;
//    for(int i = 1 ; i <= bcnt ;++i) {          //获得强连通结果
//        for( int j = 0 ; j < anss[i].size() ; ++j ) {
//            cout << anss[i][j] ;
//        }
//        cout << endl ;
//    }

}
int main() {
while(~scanf("%d",&n)) {
in = out = 0 ;
for( int i = 0 ; i < n ; ++i ) {
mapp[i].clear() ;
//anss[i].clear() ;
}
int init ;
for( int i = 1 ; i <= n ; ++i ) {
while(scanf("%d",&init),init) {
mapp[i].push_back(init) ;
}
}
solve() ;
}

return 0 ;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: