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. bzoj1064

    很巧妙的题 首先有几种情况 1. 有环 2.两点间有多条路径 3.其他 3.显然最简单,最小是3,最大是每个弱联通块中最长链 2.显然,两点间两条路径的差是答案的倍数 1.出现环,那答案一定是其约数, ...

  2. bzoj1185

    一遇到数学题和计算几何题我就要调半天…… 玛雅,我真是太弱了…… 基本思路很简单,先上凸包,然后矩形与凸包一边重合,然后旋转卡壳即可 然而我没怎么写过计算几何题,一开始写的各种囧,后来看了hzwer的 ...

  3. bzoj3931: [CQOI2015]网络吞吐量

    将最短路图找出来,跑maxflow即可.有注意到数据范围.然后输出的时候%dWA了三次QAQ... #include<cstdio> #include<cstring> #in ...

  4. MYSQL自动备份策略的选择

    目前流行几种备份方式: 1.逻辑备份:使用mysql自带的mysqldump工具进行备份.备份成sql文件形式.优点:最大好处是能够与正在运行的mysql自动协同工作,在运行期间可以确保备份是当时的点 ...

  5. Darwin Streaming Server用vs2005编译运行过程

    原创. 一:编译 Darwin6.0.3版本是最新版本,也提供了.dsw文件.但是使用vs2005和vc6是编译不过的.所以,采用Darwin5.5.5版本.使用vc6打开WinNTSupport文件 ...

  6. 【转】Windows环境下Android Studio v1.0安装教程

    原文网址:http://ask.android-studio.org/?/article/9 http://android-studio.org/index.php/docs/experience/1 ...

  7. Delphi Waring 的信息

    Display PreferencesWarning messages (Delphi)Go Up to Delphi Compiler Directives (List) Index TypeSwi ...

  8. #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)

    #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)宏的运行机理:1. ( (TYPE *)0 ) 将零转型为TY ...

  9. Binary Tree Level Order Traversal java实现

    Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...

  10. oracle-Oracle试题

    ylbtech-doc:oracle-Oracle试题 oracle-Oracle试题 1.A,返回顶部 01.{Oracle题目}你判断下面语句,有什么作用?(选择1项)     GRANT upd ...