POJ 1236 Network of Schools 强连通图

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

图论题目,需要解决问题:

1 使用Tarjan算法求子强连通图

2 标识顶点属于哪个子强连通图

3 计算各个子强连通图的零入度数和零出度数

图论中高级内容了,是有点难度的,不细心一点肯定会出错的。

这次本博主认真注解好几乎每个语句,希望大家可以follow我的程序。

#include <cstdio>
#include <stack>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;

const int MAX_N = 101;//最大的顶点数
vector<int> graAdj[MAX_N];//vector表示邻接表法
int visNo[MAX_N];//记录深搜各个顶点的访问顺序标号
int lowLink[MAX_N];//连通图的最低标识号,记录好是否递归到已经访问过的顶点了,如果是,那么就以最低的顶点访问顺序标号为,这样可以统一子连通图的标号。通过判断当前最低连通图的标识号和访问顺序号是否一致来判断是否找到了一个子强连通图
int dfsNo;//记录深搜总的访问号
int connectNo;//当前的子强连通图的标号,最终为所有子强连通图的数量
int markNo[MAX_N];//markNo[v]代表顶点v属于子强连通图markNo[v],其值就为子强连通图
int in[MAX_N], out[MAX_N];//分别记录一个子强连通图的入度数和出度数
stack<int> stk;//深搜顶点入栈,找到子强连通图时候出栈,直到当前顶点数,所有点都属于同一个子强连通图

//深搜查找子强连通图,并记录好顶点属于哪个子强连通图
void getStrongConnected(int u)
{
	visNo[u] = lowLink[u] = ++dfsNo;//第一次进入当前顶点的时候的初值
	int n = (int)graAdj[u].size();
	stk.push(u);
	for (int i = 0; i < n; i++)//遍历当前顶点的所有连接点
	{
		int v = graAdj[u][i];
		if (!visNo[v])//没有访问过的时候
		{
			getStrongConnected(v);//递归
			lowLink[u] = min(lowLink[u], lowLink[v]);//记录最低序号
		}
		//已经访问过,但是还在栈里面,即还没有记录该顶点属于哪个强连通图
		else if (!markNo[v]) lowLink[u] = min(lowLink[u], lowLink[v]);
	}
	if (visNo[u] == lowLink[u])//当前访问顺序号等于最低标号,
	{//那么就是找到了一个子强连通图
		++connectNo;//每次要增加全局的连通标号
		int v;
		do
		{
			v = stk.top(); stk.pop();
			markNo[v] = connectNo;//顶点对用强连通图号
		} while (u != v);
	}
}

void Tarjan(int n)
{
	//前期清零工作
	dfsNo = 0, connectNo = 0;
	fill(visNo, visNo+n+1, 0);
	fill(lowLink, lowLink+n+1, 0);
	fill(markNo, markNo+n+1, 0);
	while (!stk.empty()) stk.pop();

	for (int u = 1; u <= n; u++)
	{
		//某些顶点也许是分离的,就是图的顶点有不相连的,故此要遍历所有顶点
		if (!visNo[u]) getStrongConnected(u);
	}
}

int main()
{
	int N, u, v;
	scanf("%d", &N);
	for (u = 1; u <= N; u++)
	{
		scanf("%d", &v);
		while (v)//为零表示结束
		{
			graAdj[u].push_back(v);//使用vector建立一个邻接表
			scanf("%d", &v);
		}
	}
	Tarjan(N);//计算子强连通图的个数,并表出各个顶点属于哪个子强连通图

	for (u = 1; u <= N; u++)
	{//遍历所有顶点,然后遍历顶点的邻接边,相当于遍历所有边
		for (int i = 0; i < (int)graAdj[u].size(); i++)
		{
			int v = graAdj[u][i];
			if (markNo[u] != markNo[v])//不是属于同一个子强连通图
			{//分别增加该强连通图的入度和出度
				out[markNo[u]]++;
				in[markNo[v]]++;
			}
		}
	}

	int zeroIn = 0, zeroOut = 0;
	for (int i = 1; i <= connectNo; i++)
	{
		if (in[i] == 0) zeroIn++;
		if (out[i] == 0) zeroOut++;
	}
	//入度为零则需要放置一个软件拷贝
	printf("%d\n", zeroIn);
	//变为一个强连通图,分2个情况:1 本身是一个强连通图;2 零入度或出度最大值
	printf("%d\n", connectNo == 1? 0 : max(zeroIn, zeroOut));

	return 0;
}
时间: 2024-10-10 09:13:48

POJ 1236 Network of Schools 强连通图的相关文章

poj 1236 Network of Schools 【强连通图】

题目:poj 1236 Network of Schools 类似题目hdoj 2767 3836 /*******以下kuang大神的解释,写的很好就不解释了*************************/ 强连通分量缩点求入度为0的个数和出度为0的分量个数 题目大意:N(2<N<100)各学校之间有单向的网络,每个学校得到一套软件后,可以通过单向网络向周边的学校传输,问题1:初始至少需要向多少个学校发放软件,使得网络内所有的学校最终都能得到软件.2,至少需要添加几条传输线路(边),使任

POJ 1236 Network of Schools(强连通 Tarjan+缩点)

POJ 1236 Network of Schools(强连通 Tarjan+缩点) ACM 题目地址:POJ 1236 题意: 给定一张有向图,问最少选择几个点能遍历全图,以及最少加入?几条边使得有向图成为一个强连通图. 分析: 跟HDU 2767 Proving Equivalences(题解)一样的题目,只是多了个问题,事实上转化成DAG后就不难考虑了,事实上仅仅要选择入度为0的点即可了. 代码: /* * Author: illuz <iilluzen[at]gmail.com> *

Poj 1236 Network of Schools (Tarjan)

题目链接: Poj 1236 Network of Schools 题目描述: 有n个学校,学校之间有一些单向的用来发射无线电的线路,当一个学校得到网络可以通过线路向其他学校传输网络,1:至少分配几个网络才能够让所有学校被网络覆盖?2:至少要加几条线路就能做到在任意一个学校安装网络都可以覆盖全部学校? 解题思路: 先用Tarjan对强连通分量进行缩点,然后对缩点以后的图进行处理,统计图中节点出度为零的有多少,入度为零的有多少个? 因为入度为零的点不能由其他的点到达,在每个入度为零的节点安装网络可

POJ 1236 Network of Schools(强连通分量)

POJ 1236 Network of Schools 链接:http://poj.org/problem?id=1236 题意:有一些学校连接到一个计算机网络.这些学校之间达成了一个协议:每个学校维护着一个学校列表,它向学校列表中的学校发布软件.注意,如果学校B 在学校A 的列表中,则A 不一定在B 的列表中. 任务A:计算为使得每个学校都能通过网络收到软件,你至少需要准备多少份软件拷贝. 任务B:考虑一个更长远的任务,想确保给任意一个学校发放一个新的软件拷贝,该软件拷贝能发布到网络中的每个学

[tarjan] poj 1236 Network of Schools

题目链接: http://poj.org/problem?id=1236 Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 11433   Accepted: 4551 Description A number of schools are connected to a computer network. Agreements have been developed among thos

poj 1236 Network of Schools(连通图入度,出度为0)

http://poj.org/problem?id=1236 Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 13046   Accepted: 5215 Description A number of schools are connected to a computer network. Agreements have been developed among those scho

POJ 1236——Network of Schools——————【加边形成强连通图】

Network of Schools Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 1236 Description A number of schools are connected to a computer network. Agreements have been developed among those schools: e

poj 1236 Network of Schools(tarjan+缩点)

Network of Schools 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

POJ 1236 Network of Schools(强连通分量)

题目地址:POJ 1236 这个题的大意是求最少往多少点发送消息可以使任意一个点都能收到消息和最少增加多少条边可以使图为连通图.对于第一个问题,可以求入度为0的强连通块的块数,因为只有入度为0的强连通块是无法从外界接受信息的,而只要有一个入度的话,那整个连通块就都可以接收到信息.第二个问题则是求入度为0的强连通块与出度为0的强连通块的个数的最大值. 代码如下: #include <iostream> #include <cstdio> #include <string>