poj1236 Network of Schools(强连通-缩点)

Network of Schools

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 20000/10000K (Java/Other)
Total Submission(s) : 2   Accepted Submission(s) : 1
Problem 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
/*
题目大意:输入一个N,从1到N表示出度,输入每个 i 有m个点可以到达;求选最少的点能到达所有的点,以及找加最少的的边数使整个图变成强连通图
关键点:最少的点就是缩点后的入度,而最少的加最少的边,是缩点后入度为0的与出度为0 的中取最大值;具体证明不清楚
心得:第一次写好不知道什么原因一直出错,有重写了一次,还好能输出测试数据,wa了一次,突然发现wa了好多次之后a掉是多么激动
Time:2014-8-16 23:39
*/
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
const int MAXN=10000010;
const int MAX=110;
struct Edge{
	int to;
	int next;
}edge[MAXN];
int N;
int head[MAX];
int dfn[MAX],low[MAX],stack[MAX],belong[MAX];
bool instack[MAX];
int in[MAX],out[MAX];
int edgeNum,cnt,index,Top; 
void Add(int a,int b){
	edge[edgeNum].to=b;
	edge[edgeNum].next=head[a];
	head[a]=edgeNum++;
}
void Init(){
	Top=edgeNum=index=cnt=0;
	memset(head,-1,sizeof(head));
	memset(in,0,sizeof(in));
	memset(out,0,sizeof(out)); 
	memset(belong,0,sizeof(belong));
	memset(dfn,0,sizeof(dfn));
	memset(low,0,sizeof(low));
	memset(instack,0,sizeof(instack));
	memset(stack,0,sizeof(stack));
	for(int i=1;i<=N;i++){
		int M;
		while(scanf("%d",&M),M){
			Add(i,M);
		}
	}
}
void Tarjan(int u){
	dfn[u]=low[u]=++index;
	stack[++Top]=u;
	instack[u]=true;
	for(int i=head[u];~i;i=edge[i].next){
		int v=edge[i].to;
		if(dfn[v]==0){
			Tarjan(v);
			low[u]=min(low[u],low[v]);
		}
		else if(instack[v]){
			low[u]=min(low[u],dfn[v]);
		}
	}
	int v;
	if(low[u]==dfn[u]){
		cnt++;
		do
		{
			v=stack[Top--];
			instack[v]=false;	
			belong[v]=cnt;	
		}while(u!=v);
	}
}
void solve(){
	while(scanf("%d",&N)!=EOF){
		Init();
		for(int i=1;i<=N;i++){
			if(dfn[i]==0)
			Tarjan(i);
		}
		for(int i=1;i<=N;i++){
			for(int j=head[i];~j;j=edge[j].next){
				int v=edge[j].to;
				if(belong[i]!=belong[v]){
					in[belong[v]]++;
					out[belong[i]]++;
				}
			}
		}
		int nIn,nOut;nIn=nOut=0;
		for(int i=1;i<=cnt;i++){
			if(in[i]==0)nIn++;
			if(out[i]==0)nOut++;
		}
		printf("%d\n",nIn);
		if(cnt==1)
		printf("0\n");
		else{
		printf("%d\n",max(nIn,nOut));
		}
	}
}
int main(){
	solve();
return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值