传送门

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

思路

  题意:

N(2<=N <=100)个学校之间有单向的网络,每个学校得到一套软件后,可以通过单向网络向周边的学校传输,

  • 1:初始至少需要向多少个学校发放软件,使得网络内所有的学校最终都能得到软件。
  • 2,至少需要添加几条传输线路(边),使任意向一个学校发放软件后,经过若干次传送,网络内所有的学校最终都能得到软件。

  分析:

首先找连通分量,然后看连通分量的入度为0点的总数,出度为0点的总数,那么问要向多少学校发放软件,就是入度为零的个数,这样才能保证所有点最终都能得到软件
第二问添加多少条边可以得到使整个图达到一个强连通分量,答案是入度为0的个数和出度为0的个数中最大的值。将这个图的所有子树找出来,然后将一棵子树的叶子结点(出度为0)连到另外一棵子树的根结点上(入度为0),这样将所有的叶子结点和根节点全部消掉之后,就可以得到一整个强连通分量,看最少多少条边,这样就是看叶子结点和根节点哪个多,即出度为0和入度为0哪个多

//入度数组indeg[]  出度数组outdeg[]
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 105;
struct Edge{
	int v,next;
}edge[maxn*maxn];
int head[maxn],dfn[maxn],low[maxn],st[maxn],inst[maxn],belong[maxn],indeg[maxn],outdeg[maxn];
int tot,top,in,out,scc_cnt,index,N;

void init()
{
	tot = top = scc_cnt = index = 0;
	in = out = 0;
	memset(head,-1,sizeof(head));	memset(inst,0,sizeof(inst));
	memset(dfn,0,sizeof(dfn));		memset(low,0,sizeof(low));
	memset(indeg,0,sizeof(indeg));	memset(outdeg,0,sizeof(outdeg));
}

void addedge(int u,int v)
{
	edge[tot] = (Edge){v,head[u]};
	head[u] = tot++;
}

void targin(int u)
{
	int v;
	dfn[u] = low[u] = ++index;
	st[++top] = u;
	inst[u] = 1;
	for (int i = head[u];i != -1;i = edge[i].next)
	{
		v = edge[i].v;
		if (!dfn[v])
		{
			targin(v);
			low[u] = min(low[u],low[v]);
		}
		else if (inst[v])
			low[u] = min(low[u],dfn[v]);
	}
	if (dfn[u] == low[u])
	{
		scc_cnt++;
		do
		{
			v = st[top--];
			inst[v] = 0;
			belong[v] = scc_cnt;
		}
		while (u != v);
	}
}

int main()
{
	while (~scanf("%d",&N))
	{
		int v;
		init();
		for (int i = 1;i <= N;i++)
			while (~scanf("%d",&v) && v)	addedge(i,v);
		for (int i = 1;i <= N;i++)	if (!dfn[i])	targin(i);
		for (int i = 1;i <= N;i++)
		for (int j = head[i]; j != -1;j = edge[j].next)
		{
			v = edge[j].v;
			if (belong[i] != belong[v])
			{
				indeg[belong[v]]++;
				outdeg[belong[i]]++;
			}
		}
		for (int i = 1;i <= scc_cnt;i++)
		{
			if (!indeg[i])	in++;
			if (!outdeg[i])	out++;
		}
		if (scc_cnt == 1)	printf("1\n0\n");
		else printf("%d\n%d\n",in,max(in,out));
	}
	return 0;
}

  

POJ 1236 Network of Schools(强连通分量/Tarjan缩点)的更多相关文章

  1. POJ 1236 Network Of Schools (强连通分量缩点求出度为0的和入度为0的分量个数)

    Network of Schools A number of schools are connected to a computer network. Agreements have been dev ...

  2. POJ 1236 Network of Schools (强连通分量缩点求度数)

    题意: 求一个有向图中: (1)要选几个点才能把的点走遍 (2)要添加多少条边使得整个图强联通 分析: 对于问题1, 我们只要求出缩点后的图有多少个入度为0的scc就好, 因为有入度的scc可以从其他 ...

  3. poj~1236 Network of Schools 强连通入门题

    一些学校连接到计算机网络.这些学校之间已经达成了协议: 每所学校都有一份分发软件的学校名单("接收学校"). 请注意,如果B在学校A的分发名单中,则A不一定出现在学校B的名单中您需 ...

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

    POJ 1236 Network of Schools(强连通 Tarjan+缩点) ACM 题目地址:POJ 1236 题意:  给定一张有向图,问最少选择几个点能遍历全图,以及最少加入�几条边使得 ...

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

    POJ 1236 Network of Schools 题目链接 题意:题意本质上就是,给定一个有向图,问两个问题 1.从哪几个顶点出发,能走全全部点 2.最少连几条边,使得图强连通 思路: #inc ...

  6. Poj 1236 Network of Schools (Tarjan)

    题目链接: Poj 1236 Network of Schools 题目描述: 有n个学校,学校之间有一些单向的用来发射无线电的线路,当一个学校得到网络可以通过线路向其他学校传输网络,1:至少分配几个 ...

  7. poj 1236 Network of Schools(又是强连通分量+缩点)

    http://poj.org/problem?id=1236 Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Su ...

  8. [tarjan] poj 1236 Network of Schools

    主题链接: http://poj.org/problem?id=1236 Network of Schools Time Limit: 1000MS   Memory Limit: 10000K To ...

  9. poj 1236 Network of Schools(tarjan+缩点)

    Network of Schools Description A number of schools are connected to a computer network. Agreements h ...

  10. POJ 1236 Network of Schools (Tarjan)

    Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 22745   Accepted: 89 ...

随机推荐

  1. 排序图解:js排序算法实现

    之前写过js实现数组去重, 今天继续研究数组: 排序算法实现. 排序是数据结构主要内容,并不限于语言主要在于思想:大学曾经用C语言研究过一段时间的排序实现, 这段时间有空用JS再将排序知识点熟悉一遍. ...

  2. 深入体验bash on windows,在windows上搭建原生的linux开发环境,酷!

    今年微软Build 2016大会最让开发人员兴奋的消息之一,就是在Windows上可以原生运行Linux bash,对开发人员来说,这是一个喜闻乐见的消息. 1 安装 你必须安装开发者预览版本,才能使 ...

  3. 【语言基础】c++ 备忘录

    1. C++ 整数类型范围 可以参照头文件limits.h定义的宏 #define INT_MAX       2147483647(32bit, 最大10位十进制) #define UINT_MAX ...

  4. JavaScript学习笔记- 正则表达式常用字符集及方法

    正则表达式修饰符(修饰符 可以在全局搜索中不区分大小写) i(ignoreCase)执行对大小写不敏感的匹配 g (global)     执行全局匹配(查找所有匹配而非在找到第一个匹配后停止) m( ...

  5. 3n+1b 备忘录方法

    题目详情 对任何一个自然数n,如果它是偶数,那么把它砍掉一半:如果它是奇数,那么把(3n+1)砍掉一半.这样一直反复砍下去,最后一定在某一步得到n=1.卡拉兹在1950年的世界数学家大会上公布了这个猜 ...

  6. ajax+FormData+javascript实现无刷新表单信息提交

    ajax+FormData+javascript实现无刷新表单信息提交 原理: dom收集表单信息,利用FormData快速收集表单信息  ,实例化表单数据对象 同时收集fm的表单域信息. var f ...

  7. PowerDesigner导出SQL时自动生成注释

    在powerBuilder中新建一个Physical Data Model,在其中新建一个用户表,信息如下图所示: 此时的SQL语句可从其中的Preview视图中得到,如下图所示: 这个时候生成的sq ...

  8. node.js-概念

    官方网站:http://nodejs.cn/ 1.Node 是一个服务器端 JavaScript 解释器,可是真的以为JavaScript不错的同学学习Node就能轻松拿下,那么你就错了,总结:水深不 ...

  9. js-格式化数字保留两位小数-带千分符

    很多时候发现有时候js会提示自带函数不能使用,所以自己找了很多资料实现了个 html <input type="text" class="input_text in ...

  10. js清除浏览器缓存的几种方法

    2014年9月24日 4692次浏览 关于浏览器缓存 浏览器缓存,有时候我们需要他,因为他可以提高网站性能和浏览器速度,提高网站性能.但是有时候我们又不得不清除缓存,因为缓存可能误事,出现一些错误的数 ...