Network of Schools(强连通图,缩点,tarjan)思路详细分析

题目:Network of Schools

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个学校,学校与学校之间会传递信息,但是学校A向学校B传递信息,并不代表B会向A传递信息,第一个问题判断需要给几个学校信息,能够使所有的学校都得到信息,第二个问题,需要添加几条道路可使所有学校获得信息。

思路分析

首先需要将所有的点进行缩点(缩点使用tarjan算法缩点),将所有的强连通分量看成一个点,为什么要这么做呢?因为我们知道强连通分量内所有的点都可以连通,既然这样,我们就不需要在考虑其内部的点,只需要看是否有其余的点将信息传递给改强连通分量,例如假设a,b,c三个学校之间能够相互传递信息,那么我们就可以将其看成一个学校,所以在缩点之后,剩下的各个点,入度为0的点就表示没有人跟它信息,它被孤立了,我们需要单独给他信息。所以第一个问题,至少需要给几个学校信息,只需要知道入度为0的点强连通分量有几个,就需要给几个学校信息。因为入度为0,就无法从其他地方获得任何信息,第二个问题,需要添加几条道路可使所有学校获得信息。只有当所有的点入度和出度都不为0时整个图才会全部连通。所以只需要去出入度和出度为0的点和的最大值,即可。

缩点(tarjan)算法

整个代码只有tarjan这一块是模板,其他都是根据题意写。首先这个算法需要一些数组标记,low数组表示当前强连通分量的根,dfn数组表示时间,belong表示给将当前强连通分量做个标记。vis标记当前点是否访问过并在栈中,in入度,out出度.下面看代码。

#include<cstring>
#include<algorithm>
#include<cstdio>
#include<stack>
#include<iostream>
using namespace std;
int low[10000];//表示当前强连通分量的根
int dfn[10000];//时间
int vis[10000];//标记当前点是否访问过并在栈中
int belong[10000];//表示给将当前强连通分量做个标记
int in[10000];//in入度
int out[10000];//out出度
struct node
{
	int t;//为了方便计算保存了v数组的大小
	int v[100];//表示能够访问的各个学校
}s[10000];//结构体表示有向图。
stack<int>Q;
int n;
int index = 0;//时间标记。
int cnt = 0;//记录强连通分量的数目
void tarjan(int x)//递归不太容易理解
{
	dfn[x] = low[x] = ++index;
	vis[x] = 1;//标记当前节点
	Q.push(x);//入栈
	for (int k = 0; k < s[x].t; k++)
	{
		if (!dfn[s[x].v[k]])//为访问过
		{
			tarjan(s[x].v[k]);//进入递归
			low[x] = min(low[x], low[s[x].v[k]]);//这里可以与
		}
		else if (vis[s[x].v[k]] == 1)//已访问过并在栈中这就不用管了。
		{
			low[x] = min(low[x], dfn[s[x].v[k]]);
		}
	}
	if (low[x] == dfn[x])
	{
		int w;
		++cnt;//计数
		while (1)
		{
			w = Q.top();
			Q.pop();//将整个连通分量出栈
			vis[w] = 0;//恢复标记
			belong[w] = cnt;//标记连通分量
			if (w == x)
				break;
		}
	}
}
void solve()
{
	index = 0;
	cnt = 0;
	memset(dfn, 0, sizeof(dfn));
	memset(low, 0, sizeof(low));
	memset(vis, 0, sizeof(vis));
	memset(in, 0, sizeof(in));
	memset(out, 0, sizeof(out));
	while (!Q.empty())Q.pop();
	for (int i = 1; i <= n; i++)
	{
		if (dfn[i] == 0)
			tarjan(i);
	}
	for (int i = 1; i <= n; i++)
	{
		for (int k = 0; k < s[i].t; k++)
		{
			int m = s[i].v[k];
			if (belong[i] != belong[m])//若连接的两个点不属于同一个连通分量
			{
				out[belong[i]]++;
				in[belong[m]]++;
			}
		}
	}
	int ans1 = 0;
	int ans2 = 0;
	for (int i = 1; i <= cnt; i++)
	{
		if (in[i] == 0)ans1++;
		if (out[i] == 0)ans2++;
	}
	ans2 = max(ans1, ans2);
	if (cnt == 1)
		printf("1\n0\n");
	else
		printf("%d\n%d\n", ans1, ans2);
}
int main()
{
	while (cin >> n)
	{
	for (int i = 1; i <= n; i++)
	{
		int d = 0,l;
		while (cin >> l && l != 0)
		{
			s[i].v[d++] = l;
		}
		s[i].t = d;	
	}solve();
	}
	
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值