【图论】Network of Schools

[POJ1236]Network of Schools
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 18969   Accepted: 7467

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

 
题目大意:一个有向图代表学校间的传输网络
     1.要将一文件发放到至少几所学校保证所有学校都能获得文件
     2.至少要添加几条有向边才能从任意学校发出文件使所有学校都可以获得
 
试题分析:缩点,第一问就可以直接得出是入度为0的强联通分量的个数。
        第二问就是MAX(入度为0,出度为0)的强联通分量个数。
     为什么呢?我们只需要将所有出度为0连向入度为0连一条边就好了。
     注意特判只有一个强联通分量的情况。
代码:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std;

inline int read(){
	int x=0,f=1;char c=getchar();
	for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
	for(;isdigit(c);c=getchar()) x=x*10+c-'0';
	return x*f;
}
const int MAXN=1001;
const int INF=999999;
int N,M;
vector<int> vec[MAXN];
int dfn[MAXN],low[MAXN],tar[MAXN];
int que[MAXN];
bool inq[MAXN];
int tmp,Col,tot;
int ind[MAXN],oud[MAXN];

void Tarjan(int x){
	que[++tmp]=x;
	++tot; dfn[x]=low[x]=tot;
	inq[x]=true;
	for(int i=0;i<vec[x].size();i++){
		int to=vec[x][i];
		if(!dfn[to]){
			Tarjan(to);
			low[x]=min(low[x],low[to]);
		}
		else if(inq[to]) low[x]=min(low[x],dfn[to]);
	}
	if(low[x]==dfn[x]){
		++Col;
		tar[x]=Col;
		inq[x]=false;
		while(que[tmp]!=x){
			int k=que[tmp];
			tar[k]=Col;
			inq[k]=false;
			tmp--;
		}
		tmp--;
	}
	return ;
}
int ans1,ans2;
int main(){
	N=read();
	for(int i=1;i<=N;i++){
		int v=read();
		while(v!=0){
			vec[i].push_back(v);
			v=read();
		}
	}
	for(int i=1;i<=N;i++) if(!dfn[i]) Tarjan(i);
	for(int i=1;i<=N;i++){
		for(int j=0;j<vec[i].size();j++){
			if(tar[i]!=tar[vec[i][j]]){
				ind[tar[vec[i][j]]]++;
				oud[tar[i]]++;
			}
		}
	}
	if(Col==1){
		puts("1
0");
		return 0;
	}
	for(int i=1;i<=Col;i++)
	    if(!ind[i]) ans1++;
	for(int i=1;i<=Col;i++)
	    if(!oud[i]) ans2++;
	printf("%d
%d
",ans1,max(ans1,ans2));
}

  

原文地址:https://www.cnblogs.com/wxjor/p/7283747.html