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 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的表中,那么B不一定在A的表中。

现在的任务就是,给出所有学校及他们维护的表,问1、如果所有学校都要被传送到,那么需要几份软件备份;2、如果只用一份软件备份,那么需要添加几条边?

分析:

问题1:找入度为0的点的个数

问题2:找入度为0的个数与出度为0的个数的最大值

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<vector>
#include<algorithm>
#define N 110 using namespace std; vector<vector<int> >G; int low[N], dfn[N], di[N], maps[N][N];
int Stack[N], n, top, Time, cnt;
bool Instack[N]; void Init()
{
G.clear();
G.resize(n + );
memset(low, , sizeof(low));
memset(dfn, , sizeof(dfn));
memset(Stack, , sizeof(Stack));
memset(di, , sizeof(di));
memset(Instack, false, sizeof(Instack));
Time = top = cnt = ;
} void Tarjan(int u)
{
int i, v, len;
low[u] = dfn[u] = ++Time;
Stack[top++] = u;//进栈
Instack[u] = true;
len = G[u].size();
for(i = ; i < len ; i++)//遍历入栈节点的边
{
v = G[u][i];
if(!dfn[v])//点v未被访问过则则对其进行Tarjan
{
Tarjan(v);
low[u] = min(low[u], low[v]);// 回溯的时候改变当前节点的low值
}
else if(Instack[v])//如果新搜索到的节点已经被搜索过而且现在在栈中
low[u] = min(low[u], dfn[v]);//更新当前节点的low值,这里的意思是两个节点之间有一条可达边,而前面节点已经在栈中,那么后面的节点就可能和前面的节点在一个联通分量中
}
if(dfn[u] == low[u])//最终退回来的时候 low == dfn , 没有节点能将根节点更新,那 low == dfn 的节点必然就是根节点
{
do
{
v = Stack[--top];//出栈
Instack[v] = false;
di[v] = cnt;//表示点v在第cnt个强联通分量里
}
while(v != u); //一直出栈到此节点, 这些元素是一个强联通分量
cnt++;//联通分量的个数
}
}//Tarjan的记过得到di数组记录每个节点分别属于哪个强联通分量 void Solve()
{
int i, j, In[N], Out[N], in = , out = ;
memset(In, , sizeof(In));
memset(Out, , sizeof(Out));
for(i = ; i <= n ; i++)
if(!low[i])
Tarjan(i);
for(i = ; i <= n ; i++)// 遍历每条边,找到缩点之后的边
{
for(j = ; j <= n ; j++)
{
if(i == j)
continue;
if(maps[i][j] && di[i] != di[j])//如果i,j两点有边且不属于一个联通分量的边
{
Out[di[i]]++;//缩点后出度+1
In[di[j]]++;//缩点后入读+1
}
}
}
for(i = ; i < cnt ; i++)
{
if(!In[i])
in++;
if(!Out[i])
out++;
}//in表示入度为0的点的个数,out表示出度为0的点的个数
if(cnt == )//注意当联通分量只有一个时,不应该加边
printf("1\n0\n");
else
printf("%d\n%d\n", in, max(in, out));
} int main()
{
int i, a;
while(~scanf("%d", &n))
{
Init();
for(i = ; i <= n ; i++)
{
while(scanf("%d", &a), a)
{
G[i].push_back(a);
maps[i][a] = ;
}
}
Solve();
}
return ;
}

poj 1236 Network of Schools(连通图入度,出度为0)的更多相关文章

  1. POJ 1236 Network of Schools 连通图缩点

    题目大意:有向图连通图,第一问求至少需要多少个软件才能传输到所有学校,第二问求至少需要增加多少条路使其成为强连通图 题目思路:利用Tarjan算法经行缩点,第一问就是求缩点后入度为0的点的个数(特殊情 ...

  2. Poj 1236 Network of Schools (Tarjan)

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

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

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

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

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

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

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

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

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

  7. [tarjan] poj 1236 Network of Schools

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

  8. 【强连通分量缩点】poj 1236 Network of Schools

    poj.org/problem?id=1236 [题意] 给定一个有向图,求: (1)至少要选几个顶点,才能做到从这些顶点出发,可以到达全部顶点 (2)至少要加多少条边,才能使得从任何一个顶点出发,都 ...

  9. poj 1236 Network of Schools(连通图)

    题目链接:http://poj.org/problem?id=1236 题目大意:有一些学校,学校之间可以进行收发邮件,给出学校的相互关系,问:1.至少 要向这些学校发送多少份才能使所有的学校都能获得 ...

随机推荐

  1. CFF前端沙龙总结

    一. -OOCSS + Sass ——大漠 1. OOCSS 结构<=>皮肤 分离 容器<=>内容 分离 2. Sass 工具.处理器 SCSS(CSS风格)<=> ...

  2. linux/unix网络编程之epoll

    转载自 Linux epoll模型 ,这篇文章讲的非常详细! 定义: epoll是Linux内核为处理大批句柄而作改进的poll,是Linux下多路复用IO接口select/poll的增强版本,它能显 ...

  3. 1709. Penguin-Avia(并查集)

    1709 简单题 并查集找下就行 #include <iostream> #include<cstdio> #include<cstring> #include&l ...

  4. LA 3263 (平面图的欧拉定理) That Nice Euler Circuit

    题意: 平面上有n个端点的一笔画,最后一个端点与第一个端点重合,即所给图案是闭合曲线.求这些线段将平面分成多少部分. 分析: 平面图中欧拉定理:设平面的顶点数.边数和面数分别为V.E和F.则 V+F- ...

  5. UVa 1638 (递推) Pole Arrangement

    很遗憾,这么好的一道题,自己没想出来,也许太心急了吧. 题意: 有长度为1.2.3...n的n个杆子排成一行.问从左到右看能看到l个杆子,从右往左看能看到r个杆子,有多少种排列方法. 分析: 设状态d ...

  6. js array 数组删除元素

    /* * 方法:Array.remove(dx) * 功能:根据元素位置值删除数组元素. * 参数:元素值 * 返回:在原数组上修改数组 */ Array.prototype.baoremove = ...

  7. UVA 11374 Airport Express(最短路)

    最短路. 把题目抽象一下:已知一张图,边上的权值表示长度.现在又有一些边,只能从其中选一条加入原图,使起点->终点的距离最小. 当加上一条边a->b,如果这条边更新了最短路,那么起点st- ...

  8. LeetCode: Single Number I && II

    I title: Given an array of integers, every element appears twice except for one. Find that single on ...

  9. DrawDib函数组的使用

    Microsoft的针对与设备无关位图(DIB位图),在其WIN32 SDK的Multimedia中提供了一组绘制DIB位图的高性能函数组──DrawDib函数组.DrawDib函数组是一组不依赖于图 ...

  10. zoj 1119 /poj 1523 SPF

    题目描述:考虑图8.9中的两个网络,假定网络中的数据只在有线路直接连接的2个结点之间以点对点的方式传输.一个结点出现故障,比如图(a)所示的网络中结点3出现故障,将会阻止其他某些结点之间的通信.结点1 ...