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. Linux改IP后务必重启网络服务

    [root@localhost ~]# vi /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0BOOTPROTO=noneONBOOT=yes ...

  2. java-基础练习题

    [程序1] 题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少? 1.程序分析: 兔子的规律为数列1,1 ...

  3. UVa 10129 Play On Words【欧拉道路 并查集 】

    题意:给出n个单词,问这n个单词能否首尾接龙,即能否构成欧拉道路 按照紫书上的思路:用并查集来做,取每一个单词的第一个字母,和最后一个字母进行并查集的操作 但这道题目是欧拉道路(下面摘自http:// ...

  4. 漫谈 polling 和 Websocket

    Http被设计成了一个单向的通信的协议,即客户端发起一个request,然后服务器回应一个response.这让服务器很为恼火:我特么才是老大,我居然不能给小弟发消息... 轮询 老大发火了,小弟们自 ...

  5. java分层架构概念

    转自:http://www.cnblogs.com/bdqnbenet/p/4924778.html service是业务层 DAO (Data Access Object) 数据访问 1.JAVA中 ...

  6. LwIP编译方法以及选项说明

    条件编译命令 作用说明 IP_SOF_BROADCAST   LWIP_IGMP  

  7. 两个android程序间的相互调用(apk互调)

    通常我们用到的只是activity之间的互相跳转和调用,很少会用到apk级别的互相调用. 往往在一些应用上会用到,比如一个支付系统,可能会有很多的一系列的程序调用到:彩票系统.订票系统.团购网……全部 ...

  8. Oracle行列互换 横表和纵表

    /* 在实际使用sql工作中总会碰到将某一列的值放到标题中显示.就是总说的行列转换或者互换. 比如有如下数据: ID NAME KECHENG CHENGJI -- ---------- ------ ...

  9. POJ 1195- Mobile phones(二维BIT)

    题意: 矩阵上的单点更新,范围求和 #include <map> #include <set> #include <list> #include <cmath ...

  10. 【LeetCode 99】Recover Binary Search Tree

    Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...