[POJ 1236] Network of Schools | Tarjan缩点

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 ) (2 <= N <= 100) (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

题意:
有n个学校,每个学校可以给其它一些学校发送消息。
第一问:最少选择多少个学校投放消息,才能够使得所有的学校都收到消息
第二问:如果要使得在任意学校放置消息,其他学校都可以收到,至少需要添加多少条边
首先将图建立之后,进行缩点
缩点后找到每个强连通同分量的入度和出度
第一问是
入度为0的点
第二问是
max(入度为0的点,出度为0的点)数量的最大值
特殊情况:但是如果说只有一个强连通分量
就应该是{
1
0
}
假设以题中的样例为例:
缩点之前:
在这里插入图片描述

缩点之后:
(1,2,5是强连通的)
在这里插入图片描述
这里,要加入4->1,3->1两条边
对于第二问,不能让出度为0的店没法发送消息,也不能让入度为0的点没法接收消息,所以说就得二者取max

int cnt,head[107];
int n;
struct node {
	int u,v,nex;
} e[maxn];
void _Init() {
	cnt = 0;
	Clear(head,-1);
}
void add(int u,int v) {
	e[cnt].u = u;
	e[cnt].v = v;
	e[cnt].nex = head[u];
	head[u] = cnt ++;
}
stack<int> stk;
int cntSCC;
int low[107],dfn[107],dfc,pos[107];
bool instack[107];
int vis[107][107];
void Tarjan(int u) {
	dfn[u] = low[u] = ++ dfc;
	stk.push(u);
	instack[u] = 1;
	for(int i=head[u]; ~i; i=e[i].nex) {
		int to = e[i].v;
		if(!dfn[to]) {
			Tarjan(to);
			low[u] = min(low[u], low[to]);
		} else if(instack[to]) low[u] = min(low[u], dfn[to]);
	}
	if(low[u] == dfn[u]) {
		int tp;/// = stk.top();
		++ cntSCC;
		do {
			tp = stk.top();
			stk.pop();
			instack[tp] = 0;
			pos[tp] = cntSCC;
		} while(tp != u);
	}
}
int out[107],in[107];
int main() {
	n = read;
	_Init();
	for(int i=1; i<=n; i++) {
		int x = read;
		while(x) {
			add(i, x);
			x = read;
		}
	}
	for(int i=1; i<=n; i++) {
		if(!dfn[i]) Tarjan(i);
	}
	for(int i=1; i<=n; i++) {
		int u = i;
		for(int j=head[u]; ~j; j=e[j].nex) {
			int to = e[j].v;
			if(pos[u] != pos[to] && !vis[u][to]) {
				out[pos[u]] ++;
				in[pos[to]] ++;
			}
		}
	}
	/**
	for(int i=1; i<=n; i++) {
		printf("%d %d %d\n",i,out[i],in[i]);
	}
	**/
	int cntout = 0,cntin = 0;
	for(int i=1; i<=cntSCC; i++) {
		if(!out[i]) cntout ++;
		if(!in[i]) cntin ++;
	}
	if(cntSCC == 1) puts("1\n0");
	else {
		cout << cntin << endl << max(cntout,cntin) << endl;
	}
	return 0;
}
/**


**/
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值