http://poj.org/problem?id=1236

Network of Schools
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 9481   Accepted: 3767

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

Source

 
【题解】:
  这题大意是给一个有向图,求至少给多少个结点发消息能使消息传遍整个网络,并进一步求出至少添加多少条边能使对图中任意一个结点发消息都能使消息传遍整个网络。可以先用kosaraju将强连通分支缩点,得到原图的基图,然后统计入度为0的连通分量个数和出度为0的连通分量个数,入度为0的必须给它发消息,入度不为0的不必给发消息,所以第一问所求即为缩点后的图中入度为0的个数,至于第二问,只需将入度为0的结点与出度为0的结点连接即可满足要求,最少需加边数目为两者之中的较大者,需注意的是,单只有一个连通分量时,输出结果为0 。
 
【code】:
 
 /**
Judge Status:Accepted Memory:772K
Time:0MS Language:G++
Code Length:2155B Author:cj
*/ #include<iostream>
#include<stdio.h>
#include<stack>
#include<string.h>
#include<algorithm>
#include<vector> #define N 110
using namespace std; vector<int> G[N];
int pre[N],lowlink[N],sccno[N],dfs_cnt,scc_cnt;
stack<int> stk; int visit[N][N],in[N],out[N];
void Tarjan(int u)
{
pre[u] = lowlink[u] = ++dfs_cnt;
stk.push(u);
int i;
for(i=;i<G[u].size();i++)
{
int v = G[u][i];
if(!pre[v])
{
Tarjan(v);
lowlink[u] = min(lowlink[u],lowlink[v]);
}
else if(!sccno[v])
{
lowlink[u] = min(lowlink[u],pre[v]);
}
}
if(pre[u]==lowlink[u])
{
int x;
scc_cnt++;
do
{
x = stk.top();
stk.pop();
sccno[x] = scc_cnt;
}while(x!=u);
}
} void findncc(int n)
{
dfs_cnt = scc_cnt = ;
memset(pre,,sizeof(pre));
memset(lowlink,,sizeof(lowlink));
memset(sccno,,sizeof(sccno));
int i;
for(i=;i<=n;i++) if(!pre[i]) Tarjan(i);
} void getNewMap(int n)
{
int i,j;
memset(visit,,sizeof(visit));
memset(in,,sizeof(in));
memset(out,,sizeof(out));
for(i=;i<=n;i++)
{
for(j=;j<G[i].size();j++)
{
int v = sccno[G[i][j]];
int u = sccno[i]; //注意是对sccno[i]数组里的强联通分量进行操作,也就是缩点的过程
if(u!=v)
{
if(!visit[u][v])
{
visit[u][v] = ;
in[v]++; //出度入度统计
out[u]++;
}
}
}
}
}
int main()
{
int n;
scanf("%d",&n);
int i;
for(i=;i<=n;i++)
{
int a;
G[i].clear();
while(~scanf("%d",&a)&&a)
{
G[i].push_back(a);
}
}
findncc(n);
getNewMap(n);
int cnt_in = ,cnt_out = ;
for(i=;i<=scc_cnt;i++)
{
if(!in[i]) cnt_in++;
if(!out[i]) cnt_out++;
}
printf("%d\n",cnt_in);
if(scc_cnt!=) printf("%d\n",max(cnt_in,cnt_out)); //联通分量只有一个输出0
else printf("0\n");
return ;
}

poj 1236 Network of Schools(又是强连通分量+缩点)的更多相关文章

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

    题目大概: 每个学校都可以把软件复制好,交给它名单上的学校. 问题A:把软件复制成几份,然后交给不同的学校,所有学校才能够都有软件. 问题B:添加几条边,能使得这个图变成强连通图. 思路: 找出所有的 ...

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

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

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

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

  4. POJ 1236 Network of Schools(强连通分量/Tarjan缩点)

    传送门 Description A number of schools are connected to a computer network. Agreements have been develo ...

  5. POJ 1236 Network of Schools 有向图强连通分量

    参考这篇博客: http://blog.csdn.net/ascii991/article/details/7466278 #include <stdio.h> #include < ...

  6. poj 1236 Network of Schools(强连通、缩点、出入度)

    题意:给出一个有向图.1:问至少选出多少个点,才能沿有向边遍历所有节点.2:问至少加多少条有向边,使原图强连通. 分析:第一个问题,缩点后找所有树根(入度为0).第二个问题,分别找出入度为0和出度为0 ...

  7. poj 1236 Network of Schools【强连通求孤立强连通分支个数&&最少加多少条边使其成为强连通图】

    Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 13800   Accepted: 55 ...

  8. POJ 1236.Network of Schools (强连通)

    首先要强连通缩点,统计新的图的各点的出度和入度. 第一问直接输出入度为0的点的个数 第二问是要是新的图变成一个强连通图,那么每一个点至少要有一条出边和一条入边,输出出度和入度为0的点数大的那一个 注意 ...

  9. POJ 1236 Network of Schools (tarjan算法+缩点)

    思路:使用tarjan求强连通分量并进行缩点,判断所有入度为0的点,这个点就是必须要给予文件的点,分别计算出度,入度为零的点的个数,取二者的最大值就是把这个图变成强连通需要加的边数. 一个取值需要讨论 ...

随机推荐

  1. jQuery对象和dom对象的辨析和相互转化

    jquery对象和dom对象总是让人感觉很难分清,其实只要做到1对两者概念有明确认识2找出不同点 A DOM 1概念 DOM对象(Document Object Model,文档对象模型)可以把htm ...

  2. Nginx - Additional Modules, Website Access and Logging

    The following set of modules allows you to configure how visitors access your website and the way yo ...

  3. 导出Private API

    首先介绍下private API 它共分为两类: 1 在官方文档中没有呈现的API(在frameworks 下隐藏) 2 苹果明确申明不能使用的API ,在privateFrameworks 下 然后 ...

  4. C#消息模拟

    C#中消息的工作流程: C#中的消息被Application类从应用程序消息队列中取出,然后分发到消息对应的窗体,窗体对象的第一个响应函数是对象中的protected override void Wn ...

  5. spring aop配置文档部分翻译

    欢迎转载交流: http://www.cnblogs.com/shizhongtao/p/3476973.html 下面的文字来自官方文档的翻译,具体事例以后奉上. Advisors "ad ...

  6. 创建featureclass,为它赋别名,并移动到数据集下

    if (pOutFtrClass == null) { //continue; //创建featureclass //得到规范的字段集 IFields pFields = pFeatureClass. ...

  7. 翻转单词顺序VS左旋转字符串

    题目:输入一个英文句子,翻转句子中单词的顺序,但单词内字符的顺序不变.句子中单词以空格符隔开.为简单起见,标点符号和普通字母一样处理.例如输入“I am a student.”,则输出“student ...

  8. browserify总结

    一.browserify 简介 browserify is a tool for compiling node-flavored commonjs modules for the browser. Y ...

  9. rabbitmq+haproxy+keepalived实现高可用集群搭建

    项目需要搭建rabbitmq的高可用集群,最近在学习搭建过程,在这里记录下可以跟大家一起互相交流(这里只是记录了学习之后自己的搭建过程,许多原理的东西没有细说). 搭建环境 CentOS7 64位 R ...

  10. 调用 GetProcAddress 失败,在 ISAPI 筛选器 "C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" 上

    1.选择网站的ISAPI筛选器,设置ASP.NET的 aspnet_filter.dll右键恢复为父项 如果问题还未解决,执行第2步: 2.是否注册了asp.net,打开cmd运行:C:\Window ...