Network of Schools
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 16803   Accepted: 6641

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
原题大意:给学校的数目n,接下来n行,第i行有若干个数,以0结束,代表了第i-1个学校可以传递消息的学校编号(单向),问,最少需要多少份拷贝资料,如果所有学校两两间可以传达到资料,那么需要新建多少个列表? 解题思路: 同样缩点,很容易想出入度为0的点即为最少拷贝数。
入度为0的点和出度为0的点的最大值便是所需新建数。
#include<stdio.h>
#include<string.h>
struct list
{
int v;
list *next;
};
list *head[10010],*rear[10010];
int dfn[10010],low[10010],stack[10010],s[10010],times,cnt,top;
bool instack[10010];
void tarjian(int v)
{
dfn[v]=low[v]=++times;
stack[top++]=v;
instack[v]=true;
for(list *p=head[v];p!=NULL;p=p->next)
if(!dfn[p->v])
{
tarjian(p->v);
if(low[p->v]<low[v]) low[v]=low[p->v];
}else if(instack[p->v]&&low[p->v]<low[v]) low[v]=low[p->v];
if(dfn[v]==low[v])
{
++cnt;
do
{
v=stack[--top];
instack[v]=false;
s[v]=cnt;
}while(dfn[v]!=low[v]);
}
return;
}
int main()
{
int n,k,j,i,rud[10010],chd[10010],ans1,ans2;
scanf("%d",&n);
memset(head,0,sizeof(head));
memset(rear,0,sizeof(rear));
for(j=1;j<=n;++j)
{
head[j]=rear[j]=new list;
rear[j]->v=j;
while(scanf("%d",&k),k)
{
rear[j]->next=new list;
rear[j]=rear[j]->next;
rear[j]->v=k;
}
rear[j]->next=NULL;
}
memset(dfn,0,sizeof(dfn));
memset(low,0,sizeof(low));
memset(s,0,sizeof(s));
memset(instack,false,sizeof(instack));
times=cnt=top=0;
for(i=1;i<=n;++i) if(!dfn[i]) tarjian(i);
if(cnt==1)
{
printf("1\n0\n");
return 0;
}
memset(rud,0,sizeof(rud));memset(chd,0,sizeof(chd));
ans1=ans2=0;
for(i=1;i<=n;++i)
for(list *p=head[i];p!=NULL;p=p->next)
if(s[p->v]!=s[i])
{
++rud[s[p->v]];
++chd[s[i]];
}
for(i=1;i<=cnt;++i) if(!rud[i]) ++ans1;
for(i=1;i<=cnt;++i) if(!chd[i]) ++ans2;
printf("%d\n",ans1);
printf("%d\n",ans1>ans2?ans1:ans2);
return 0;
}

  

[强连通分量] POJ 1236 Network of Schools的更多相关文章

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

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

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

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

  3. Poj 1236 Network of Schools (Tarjan)

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

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

    http://poj.org/problem?id=1236 Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Su ...

  5. [tarjan] poj 1236 Network of Schools

    主题链接: http://poj.org/problem?id=1236 Network of Schools Time Limit: 1000MS   Memory Limit: 10000K To ...

  6. poj 1236 Network of Schools(连通图入度,出度为0)

    http://poj.org/problem?id=1236 Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Su ...

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

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

  8. POJ 1236 Network of Schools (有向图的强连通分量)

    Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 9073   Accepted: 359 ...

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

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

随机推荐

  1. Android requires compiler compliance level 5.0 or 6.0. Found '1.7' instead

    Android requires compiler compliance level 5.0 or 6.0. Found '1.7' instead 在解决问题Underscores can only ...

  2. js实现继承的五种方式

    function Parent(firstname) { this.fname=firstname; ; this.sayAge=function() { console.log(this.age); ...

  3. 双向循环链表的Java版本实现

    1.单项循环列表 单向循环链表是单链表的另一种形式,其结构特点是链表中最后一个结点的指针不再是结束标记,而是指向整个链表的第一个结点,从而使单链表形成一个环.和单链表相比,循环单链表的长处是从链尾到链 ...

  4. textarea 多行文本保存数据到DB,取出后恢复换行

    Steps: 1.保存到数据库之前把textarea中的换行字符转换为<br>. var dbStr = textareaStr.replace(/\n|\r\n/g,"< ...

  5. Mac下安装LNMP(Nginx+PHP5.6)环境

    [转自:http://avnpc.com/pages/install-lnmp-on-osx] 安装Homebrew 最近工作环境切换到Mac,所以以OS X Yosemite(10.10.1)为例, ...

  6. Android中帧布局-FrameLayout和网格布局-GridLayout

    帧布局-FrameLayout 一.概念 帧布局中,容器为每个加入其中的空间创建一个空白的区域(成为一帧).每个空间占据一帧,这些帧会按gravity属性自动对齐. 帧布局的效果是将其中的所有空间叠加 ...

  7. 模板引擎:ArtTemplate 使用入门和简单的使用

    下载地址:https://github.com/aui/artTemplate 快速上手请参考:https://github.com/aui/artTemplate 通过阅读artTemplate原文 ...

  8. LA 3523 圆桌骑士

    题目链接:http://vjudge.net/contest/141787#problem/A http://poj.org/problem?id=2942 此题很经典 知识点:DFS染色,点-双连通 ...

  9. Python进程、线程、协程

    进程和线程的解释 进程(process)和线程(thread)是操作系统的基本概念,计算机的核心是CPU,它承担了所有的计算任务: 单个CPU一次只能运行一个任务,代表单个CPU总是运行一个进程,其他 ...

  10. anaconda win10安装报错:UnicodeDecodeError解决方法

    Traceback (most recent call last): File , in <module> import conda.cli File , in <module> ...