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. C# App.config 详解

      读语句: String str = ConfigurationManager.AppSettings["DemoKey"]; 写语句: Configuration cfa = ...

  2. Builder创建者模式

    http://www.codeproject.com/Articles/42415/Builder-Design-Pattern In Elizabeth's day care center, the ...

  3. VS2010中如果忘记函数所在的头文件或者忘记函数的输入输出参数类型怎么办?

    先随便找一个熟悉的函数,右击-转到定义,然后写出目标函数,右击-转到定义

  4. 【转载】React初学者入门须知

    http://www.oschina.net/news/75530/9-things-every-reactjs-beginner-should-know react.js入门学习 看了一遍,没什么特 ...

  5. bzoj4046

    分组赛的题……madan原题,考试想不出来真是SB得不行 首先,从大往小加边,每次加边如果成环必然弹出环上最大边 考虑询问[x,y],如果边权在[x,y]的边弹出了小于等于y的边j,说明j不在最小生成 ...

  6. websocket webworker

    对我来说最快的学习途径是实践,所以找两个东西来练手.一个是websocket一个是webwoker,今天先说第一个. 要理解socket就要先理解http和tcp的区别,简单说就是一个是短链,一个是长 ...

  7. C#进程启动程序,并禁止原窗口操作

    Process myProcess = new Process();            myProcess.StartInfo.FileName = exeName;            myP ...

  8. Android启动activity的4种模式(standard、singleTop、singleTask、singleINstance)

    在AndroidManifest.xml中配置activity时,android:launchMode属性会指定启动activity的模式,有四种: standard singleTop single ...

  9. css的框架——base.css

    一.常用的base.css文件(也是比较简略的,但按需增加) body,ul,li,ol,dl,dd,h1,h2,h3,h4,h5,h6,input,p{ margin:;} ul,ol { padd ...

  10. JS:实用功能

    ylbtech-jQuery:函数-导航 添加样式(addClass).移除样式(removeClass) 轮替函数(toggle()) 选项拼加 全选 网页刷点器 jQuery:3.1,添加样式(a ...