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

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 题意:向学校发软件,1、问最少向多少个学校发软件就可以间接让所有学校都得到软件 2、问最少添加多少条边就可以使 任意向一所学校发软件 就可以让其余所有学校都收到软件 题解:求出强连通分支后缩点,求出所有入度和出度问0的点的个数insum和outsum,则答案为insum和max(insum,outsum) 输入:第一行是一个n代表接下来有n行,每行输入小于n个数以0结尾,代表这个点与第i行(当前行数)的值i点相连
#include<stdio.h>
#include<string.h>
#include<stack>
#include<vector>
#include<algorithm>
#define MAX 1100
#define MAXM 2001000
#define INF 0x7ffffff
using namespace std;
int low[MAX],dfn[MAX];
int head[MAX],ans;
int instack[MAX];
int dfsclock,scccnt;
int in[MAX],out[MAX];
int sccno[MAX];
stack<int>s;
vector<int>newmap[MAX];
vector<int>scc[MAX];
struct node
{
int beg,end,next;
}edge[MAXM];
void init()
{
ans=0;
memset(head,-1,sizeof(head));
}
void add(int u,int v)
{
edge[ans].beg=u;
edge[ans].end=v;
edge[ans].next=head[u];
head[u]=ans++;
}
void tarjan(int u)
{
int v,i;
s.push(u);
instack[u]=1;
low[u]=dfn[u]=++dfsclock;
for(i=head[u];i!=-1;i=edge[i].next)
{
v=edge[i].end;
if(!dfn[v])
{
tarjan(v);
low[u]=min(low[u],low[v]);
}
else if(instack[v])
low[u]=min(low[u],dfn[v]);
}
if(low[u]==dfn[u])
{
scccnt++;
while(1)
{
v=s.top();
s.pop();
instack[v]=0;
sccno[v]=scccnt;
if(v==u)
break;
}
}
}
void find(int l,int r)
{
dfsclock=scccnt=0;
memset(low,0,sizeof(low));
memset(dfn,0,sizeof(dfn));
memset(instack,0,sizeof(instack));
memset(sccno,0,sizeof(sccno));
for(int i=l;i<=r;i++)
{
if(!dfn[i])
tarjan(i);
}
}
void suodian()
{
int u,v;
memset(in,0,sizeof(in));
memset(out,0,sizeof(out));
for(int i=0;i<ans;i++)
{
u=sccno[edge[i].beg];
v=sccno[edge[i].end];
if(v!=u)
{
newmap[u].push_back(v);
in[v]++;
out[u]++;
}
}
}
void solve()
{
int i,j;
int sum,insum,outsum;
insum=outsum=0;
if(scccnt==1)
{
printf("1\n0\n");
return ;
}
for(i=1;i<=scccnt;i++)
{
if(!in[i])
insum++;
if(!out[i])
outsum++;
}
sum=max(insum,outsum);
printf("%d\n%d\n",insum,sum);
}
int main()
{
int n,m,j,i,a;
while(scanf("%d",&n)!=EOF)
{
init();
for(i=1;i<=n;i++)
{
while(scanf("%d",&a),a)
add(i,a);
}
find(1,n);
suodian();
solve();
}
return 0;
}

  

poj 1236 Network of Schools【强连通求孤立强连通分支个数&&最少加多少条边使其成为强连通图】的更多相关文章

  1. POJ 1236 Network of Schools (强连通分量缩点求度数)

    题意: 求一个有向图中: (1)要选几个点才能把的点走遍 (2)要添加多少条边使得整个图强联通 分析: 对于问题1, 我们只要求出缩点后的图有多少个入度为0的scc就好, 因为有入度的scc可以从其他 ...

  2. POJ 1236 Network Of Schools (强连通分量缩点求出度为0的和入度为0的分量个数)

    Network of Schools A number of schools are connected to a computer network. Agreements have been dev ...

  3. poj~1236 Network of Schools 强连通入门题

    一些学校连接到计算机网络.这些学校之间已经达成了协议: 每所学校都有一份分发软件的学校名单("接收学校"). 请注意,如果B在学校A的分发名单中,则A不一定出现在学校B的名单中您需 ...

  4. poj 1236 Network of Schools : 求需要添加多少条边成为强连通图 tarjan O(E)

    /** problem: http://poj.org/problem?id=1236 缩点后入度为0的点的总数为需要发放软件的学校个数 缩点后出度为0的点的总数和入度为0的点的总数的最大值为需要增加 ...

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

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

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

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

  7. 【强连通分量缩点】poj 1236 Network of Schools

    poj.org/problem?id=1236 [题意] 给定一个有向图,求: (1)至少要选几个顶点,才能做到从这些顶点出发,可以到达全部顶点 (2)至少要加多少条边,才能使得从任何一个顶点出发,都 ...

  8. Poj 1236 Network of Schools (Tarjan)

    题目链接: Poj 1236 Network of Schools 题目描述: 有n个学校,学校之间有一些单向的用来发射无线电的线路,当一个学校得到网络可以通过线路向其他学校传输网络,1:至少分配几个 ...

  9. POJ 1236 Network of Schools 连通图缩点

    题目大意:有向图连通图,第一问求至少需要多少个软件才能传输到所有学校,第二问求至少需要增加多少条路使其成为强连通图 题目思路:利用Tarjan算法经行缩点,第一问就是求缩点后入度为0的点的个数(特殊情 ...

随机推荐

  1. BZOJ Tyvj 1729 文艺平衡树

    Description 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2 3  ...

  2. 【单片机通信协议】CAN总线基础知识

    CAN总线基础知识(一) 1.1 CAN总线是什么? CAN(Controller Area Network)是ISO国际标准化的串行通信协议.广泛应用于汽车.船舶等.具有已经被大家认可的高性能和可靠 ...

  3. Android开发(一):环境搭建

    引言 本系列将记录我在步入Android开发过程中的一些流水账及经验,如有疏漏,还望不吝赐教. 目录 1.JDK安装及配置 2.Eclipse.Android SDK ADT安装及配置 正文 1.JD ...

  4. web前端开发分享-css,js进阶篇

    一,css进阶篇: 等css哪些事儿看了两三遍之后,需要对看过的知识综合应用,这时候需要大量的实践 经验, 简单的想法:把qq首页全屏另存为jpg然后通过ps工具切图结合css转换成html,有无 从 ...

  5. opencv for python 之 突出点检测

    opencv下载地址:http://sourceforge.net/projects/opencvlibrary/files/opencv-win/2.4.3/OpenCV-2.4.3.exe/dow ...

  6. javascript技巧合集

    转http://www.blogjava.net/zhaochengming/archive/2010/04/09/317837.html http://www.cnblogs.com/fxgachi ...

  7. Cassandra查询语言CQL的基本使用

    在window环境下运行CQL语言要先安装python环境,在linux下不需要,cassandra内置了python. 1.查看python版本:python --version2.运行pythod ...

  8. Codeforces 258 Div2

    A题,n*m根木棍,相交放置,轮流取走相交的两根,最后谁不能行动,则输掉. min(n,m)&1 为1则先取者赢. B题,给定一个长度为n,且各不相同的数组,问能否通过交换连续一段L....R ...

  9. tcpdump dump 网络流量

    tcpdump dump 网络流量 描述: Tcpdump 打印一个包的内容的描述在一个网络接口 匹配布尔值表达式 它也可以运行使用-w flag, 这样会保存包的数据到一个文件用于后面分析, 使用- ...

  10. poj1724ROADS(BFS)

    链接 本来想写spfa 加点什么限制什么的可能就过了 写着写着就成裸BFS了 也没优化就水过了 #include <iostream> #include<cstdio> #in ...