Network of Schools POJ - 1236 (tarjan)

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所学校之间可以传输文件,但是文件传输线路是单向传输,文件可以传递传输,求最少需要发送几份文件能使得

所有学校接收到文件,如果发送一份文件,就可以将文件传递给所有学校,最少需要添加几条单向传输网线

思路:强连通块内的点任意两点可以相互到达,缩为一点,入度为0的点必须手动传输文件,通过与其他点相连,消除将入度为0和出度为0 的点,整个图连成一块,向任意一点传输文件都可以使得所有学校得到文件

代码:

扫描二维码关注公众号,回复: 2302037 查看本文章
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stack>
#include<vector>
using namespace std;
const int N = 105;
int LOW[N],DFN[N];
int id,index,vis[N];
int include[N],in[N],out[N];
vector<int>G[N];
stack<int>p;
int n;
void init(){
	id=index=0;
	memset(LOW,0,sizeof(LOW));
	memset(DFN,0,sizeof(DFN));
    memset(vis,0,sizeof(vis));
	memset(in,0,sizeof(in));
	memset(out,0,sizeof(out));
	for(int i=0;i<=n;i++)
	   G[i].clear();
}
void read(int x){
	int y;
	while(~scanf("%d",&y)&&y){
		G[x].push_back(y);
	}
}
void tarjan(int u){
	LOW[u]=DFN[u]=++index;
	vis[u]=1;
	p.push(u);
	for(int i=0;i<G[u].size();i++){
		int v=G[u][i];
		if(!DFN[v]){
			tarjan(v);
			LOW[u]=min(LOW[v],LOW[u]);
		}
		else if(vis[v]) LOW[u]=min(LOW[u],DFN[v]);
	}
	if(DFN[u]==LOW[u]){
		int v=u;
		include[v]=++id;
		do{
			v=p.top();
			p.pop();
			vis[v]=0;
			include[v]=id;
		}while(u!=v);
	}
}
int main(){
	while(~scanf("%d",&n)){
		init();
		for(int i=1;i<=n;i++)
			read(i);
		for(int i=1;i<=n;i++)
		   if(!DFN[i]) tarjan(i);
		for(int u=1;u<=n;u++){
			for(int i=0;i<G[u].size();i++){
				int v=G[u][i];
				if(include[v]!=include[u]){
					in[include[v]]++;
					out[include[u]]++;
				}
			}
		}
		int ans1=0,ans2=0;
		for(int i=1;i<=id;i++){
			ans1+= in[i] == 0;
			ans2+= out[i] == 0;
		}  
		if(id==1) printf("1\n0\n");
		else printf("%d\n%d\n",ans1,max(ans1,ans2));
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/islittlehappy/article/details/81088471