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. 我的PHP之旅--SQL语句

    SQL语句 结构化查询语言(Structured Query Language)简称SQL,是一种操作数据的语言. 增加记录 INSERT INTO table_name(字段1, 字段2, 字段3) ...

  2. vs git .ignore

    ## Ignore Visual Studio temporary files, build results, and## files generated by popular Visual Stud ...

  3. 第三章—Windows程序

    这一章我都不知道该如何写了,呵呵~~ 毕竟,Win32是一个非常深奥的系统,目前还容不得我这种 小辈在这儿说三道四,不过,我既然是要写给那些入门阶段的朋友们看的,又不是写给那些搞程序设计老鸟看的,所以 ...

  4. 1012: [JSOI2008]最大数maxnumber

    单点更新,区间求最大值的题: 可以使用树状数组和线段树: #include<cstdio> #include<cstring> #include<algorithm> ...

  5. sql里条件is null 在thinkphp里

    $map['字段名'] = array('exp',' is NULL'); 譬如:$condition['url']  = array('exp',' is NULL');

  6. Ubuntu下Qt编译报错“cannot find -lGL”的解决方案

    转自cannot find -lGL Solved the problem by installing the "libglu1-mesa-dev" package. sudo a ...

  7. U3D版本《暗黑世界V1.0》编译——图文教程!

    原地址:http://blog.csdn.net/uxqclm/article/details/11970773 欢迎来到9秒:www.9miao.com 说明: A. 工具准备:          ...

  8. Python 之 使用 PIL 库做图像处理

    http://www.cnblogs.com/way_testlife/archive/2011/04/17/2019013.html Python 之 使用 PIL 库做图像处理 1. 简介. 图像 ...

  9. javaweb学习总结(二十八)——JSTL标签库之核心标签

    一.JSTL标签库介绍 JSTL标签库的使用是为弥补html标签的不足,规范自定义标签的使用而诞生的.使用JSLT标签的目的就是不希望在jsp页面中出现java逻辑代码 二.JSTL标签库的分类 核心 ...

  10. html5 spring demo

    <!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8& ...