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. oracle 取随机数据

    --取随机数据 select dbms_random.value from dual; ); --百分比

  2. web项目的两个创建形式website和webapplication(转)

    前言 在利用VS2010创建web项目的时候,会有两个选择.可以选择直接创建website网站,还可以选择使用 webapplication应用程序.刚刚接触web开发,看到这两个就疑惑了,既然是都可 ...

  3. Angular 2.0 从0到1 (四)

    第一节:Angular 2.0 从0到1 (一)第二节:Angular 2.0 从0到1 (二)第三节:Angular 2.0 从0到1 (三)第四节:Angular 2.0 从0到1 (四)第五节: ...

  4. linux 查看端口是否被占用

    查看端口是否被占用: netstat -anp | grep port lsof -i:port 查看端口被那个进程占用: netstat -anp | grep port 或使用 lsof -i:p ...

  5. sqoop-1.4.6安装配置

    1. 下载sqoop-1.4.6并解压 wget http://archive.apache.org/dist/sqoop/1.4.6/sqoop-1.4.6.bin__hadoop-2.0.4-al ...

  6. CSS之图片旋转

    主方法为: var Img = function() { var T$ = function(id) { return document.getElementById(id); } var ua = ...

  7. 20分钟入门Redux

    Redux就是个数据中心,不依附于任何框架在哪使用都行.但是和它最搭配的应该就是React了,而且大家学习它的动力大多也是解决React状态管理的问题.都说Redux文档详尽清晰,但我感觉并不友好,它 ...

  8. bzoj3315:[Usaco2013 Nov]Pogo-Cow

    思路:首先可以写出n^3dp的状态转移方程:f[i][j]=max{f[j][k]+val[i]},f[i][j]表示最后一步跳到点从j点跳到i点的最大价值(状态不能设成f[i],因为j对后面的决策是 ...

  9. 查看Aix系统配置命令

    prtconf#topas http://baike.baidu.com/link?url=QruEnlfCqyoqQ565LicyKxIGMQYSkVesj6j9GzHWwzpDOagXtuprhT ...

  10. c++ algorithm 的用法

    1 , accumulate()template<class _II, class _Ty> inline_Ty accumulate(_II _F, _II _L, _Ty _V){fo ...