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

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

解题思路:

强连通分量问题,题目分成两个子问题来求解

问题A 收到一份软件来使网络中的所有学校都能收到软件的学校的数量的最小值?

问题B 如果任意两个学校间能够互通,那么最少需要增加多少条边?

我们先求解强连通分量,强连通图内的电脑之间两两都是互通的,然后把所有强连通分量缩成结点,组成一张新的图

当某个结点的入度为0时,这个结点就必须要发送一份软件(因为收不到),统计有多少个入度为0的结点为0即可

问题B,我们求出出度为0的结点有多少个,取入度为0的顶点数和出度为0的顶点数的最大值即可

#include<iostream>
#include<algorithm>
#include<vector>
#include<string.h>
#include<stdio.h>
using namespace std;
const int MAXV = 220;
int V;
vector<int> G[MAXV];
vector<int> rG[MAXV];
vector<int> vs;
bool hasEdge[MAXV][MAXV];  //是否右边
bool bvis[MAXV];
int bcmp[MAXV];  //加入结点
int indegress[MAXV];  //入度顶点
int outdegress[MAXV]; //出度顶点

void add_edge(int from, int to) {   //加边操作
	G[from].push_back(to);
	rG[to].push_back(from);
}

void bDfs(int v) {   //正向dfs
	bvis[v] = true;
	for (int i = 0; i < G[v].size(); ++i) {
		if (!bvis[G[v][i]]) bDfs(G[v][i]);
	}
	vs.push_back(v);  
}

void rdfs(int v, int k) {
	bvis[v] = true;
	bcmp[v] = k;
	for (int i = 0; i < rG[v].size(); ++i) {
		if (!bvis[rG[v][i]])  rdfs(rG[v][i],k);   //求强连通分量
	}
}

int main() {
	memset(indegress, 0, sizeof(indegress));  //入度为0
	memset(outdegress, 0, sizeof(outdegress));  //出度为0
	memset(hasEdge, false, sizeof(hasEdge));
	scanf("%d", &V);   //读入V个结点
	int ap;
	vs.clear();
	for (int v = 1; v <= V; ++v) {
		while (scanf("%d", &ap)) {  //读入结点
			if (ap == 0) break;
			hasEdge[v][ap] = true;  
			add_edge(v, ap);  //建立边关系
		} 
	}
	memset(bvis, false, sizeof(bvis));
	for (int v = 1; v <= V; ++v) {
		if (!bvis[v]) {
			bDfs(v);
		}
	}
	memset(bvis, false, sizeof(bvis));
	int k = 0;
	for (int i = vs.size()-1; i >= 0; --i) {
		if (!bvis[vs[i]]) {
			rdfs(vs[i], k++);
		}
	}
	//缩点操作
	for (int i = 1; i <= V; ++i) {
		for (int j = 1; j <= V; ++j) {   
			if (hasEdge[i][j] && bcmp[i] != bcmp[j]) {  //它们之间有边且不在同一分量中,就把它们建起来
				outdegress[bcmp[i]]++;  //出度+1
				indegress[bcmp[j]]++;   //入度+1
			}
		}
	}
	//求强连通分量
	int  innodes = 0;
	int outnodes = 0;
	for (int i = 0; i < k; ++i) {
		if (indegress[i] == 0) innodes++;
		if (outdegress[i] == 0) outnodes++;
	}

	if(k == 1) printf("1\n0\n");
	else printf("%d\n%d\n", innodes,max(innodes,outnodes));  
	
	system("PAUSE");
	return 0;
}

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值