[POJ 1236] Network of Schools | Tarjan缩点

Posted PushyTao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[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;
}
/**


**/

以上是关于[POJ 1236] Network of Schools | Tarjan缩点的主要内容,如果未能解决你的问题,请参考以下文章

poj1236Network of Schools Tarjan裸题

[POJ1236]Network of Schools(并查集+floyd,伪强连通分量)

POJ1236Network of Schools(强连通分量 + 缩点)

POJ 1236 Network of Schools —— (缩点的应用)

POJ 1236 Network of Schools(tarjan算法 + LCA)

poj1236 Network of Schools Tarjian算法