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

题解:

题目第一句话说所有的电脑都是相互连接的,所以不用担心出现森林的情况。

第一步用tarjan进行缩点,重新构图得到一棵树,然后统计树上各点的入度以及出度。

记in为入读为0的点,记out为出度为0的点。

那么我们很显然的要给所有入度为0的点全都分配一套软件,因为他们无法从别的地方得到软件。

所以第一问的答案就是in。

考虑第二问,如果out > in 的话,我们从所有的出度为0的点开始连一条边到入度为0的点去,这样的话,入读为0的点一定会被覆盖,而且有的点甚至有多余1条入边,这无所谓。所以我们只需要添加out条边就可以了。

如果in > out的话,同理。

因此我们添加的边的条数应该为max(in,out)


代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <set>
using namespace std;
const int MAXN = 107;
int head[MAXN];
int cnt;
int DFN[MAXN];
int LOW[MAXN];
int visit[MAXN];
int scc[MAXN];
int belong[MAXN];
int stk[MAXN];

int sp;
int index;
int sccnum;
struct edge{ 
    int v; 
    int next; 
    int cost; 
}Es[10007];  
void init(){ 
    sp = sccnum = index = cnt = 0; 
    memset(head,-1,sizeof(head)); 
    memset(visit,0,sizeof(visit)); 
    memset(belong,0,sizeof(belong)); 
}
inline void add_edge(int i,int j,int cost){   
    Es[cnt].v = j; 
    Es[cnt].cost = cost; 
    Es[cnt].next = head[i]; 
    head[i] = cnt++; 
}   
void tarjan(int u){
	DFN[u] = LOW[u] = ++index;
	visit[u] = 1;
	stk[sp++] = u; 
	for(int e = head[u];e != -1;e = Es[e].next){
		int v = Es[e].v;
		if(!DFN[v]){
			tarjan(v);
			LOW[u] = min(LOW[u],LOW[v]);
		}
		else if(visit[v]){
			LOW[u] = min(LOW[u],DFN[v]);
		}
	}
	if(DFN[u] <= LOW[u]){
		//
		sccnum++;
		int top;
		do{
			top = stk[--sp];
			belong[top] = sccnum;
			visit[top] = 0;
		}while(top != u);
	}
}

int main() {
	init();
	int N;scanf("%d",&N);
	for(int i = 1;i <= N;i++){
	 	int num;
	 	while(scanf("%d",&num) && num){
	 		add_edge(i,num,1);
		}
	}

	for(int i = 1;i <= N;i++){
		if(!DFN[i])
			tarjan(i);
	}
	set<int> st1,st2;
	for(int i = 1;i <= N;i++){
		for(int e = head[i];e != -1;e = Es[e].next){
			int v = Es[e].v;
			if(belong[i] == belong[v]) continue;
			st1.insert(belong[i]);
			st2.insert(belong[v]);
		}
	}
	if(sccnum == 1){
		puts("1\n0");
		return 0;
	}
	cout<<sccnum - st2.size()<<endl;
	cout<<max(sccnum - st1.size(),sccnum - st2.size())<<endl;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值