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

题目大意:给定一个有向图,求至少要有多少个点, 才干从这些点出发到达全部点;至少要加入多少条边,才干从随意一点出发到达全部点

首先要推出一个定理:在DAG中,对于全部入度不为0的点,一定有入度为0的点可达(由于从入度为0的点倒着走,一定能走到入度不为0的点)

于是此题可用tarjan缩点,求有多少个入度为0的点,这就是第一个问题的答案。

第二个问题的答案为入度为0的点和出度为0的点的最小值。证明比較难。略。

对于这道题,由于仅仅要求入度和出度为0的点,故仅仅需在tarjan过程中记录每一个点归属哪个强连通分量。然后统计输出就可以

#include <iostream>
#include <stdio.h>
#include <string.h> #define MAXE 500
#define MAXV 3000 using namespace std; int N; struct edge
{
int u,v,next;
}edges[MAXV]; int head[MAXE],nCount=0;
int dfn[MAXE],low[MAXE],index=0;
int belong[MAXE],tot=0; //belong[i]=i点所属的强连通分量,tot=强连通分量总数
bool inStack[MAXE];
int stack[MAXE*4],top=0;
bool map[MAXE][MAXE];
int inDegree[MAXE],outDegree[MAXE],inZero=0,outZero=0; //入度。出度 int max(int a,int b)
{
if(a>b) return a;
return b;
} int min(int a,int b)
{
if(a<b) return a;
return b;
} void AddEdge(int U,int V)
{
edges[++nCount].u=U;
edges[nCount].v=V;
edges[nCount].next=head[U];
head[U]=nCount;
} void tarjan(int u)
{
dfn[u]=low[u]=++index;
stack[++top]=u; //该点入栈
inStack[u]=true;
for(int p=head[u];p!=-1;p=edges[p].next)
{
int v=edges[p].v;
if(!dfn[v])
{
tarjan(v);
low[u]=min(low[u],low[v]);
}
else if(inStack[v])
{
low[u]=min(low[u],dfn[v]);
}
}
int v;
if(dfn[u]==low[u])
{
tot++;
do
{
v=stack[top--];
belong[v]=tot;
inStack[v]=false;
}
while(u!=v);
}
} int main()
{
int to;
cin>>N;
memset(head,-1,sizeof(head));
for(int i=1;i<=N;i++)
{
while(1)
{
cin>>to;
if(to==0) break;
AddEdge(i,to);
map[i][to]=true;
}
}
for(int i=1;i<=N;i++)
if(!dfn[i]) tarjan(i);
for(int i=1;i<=N;i++)
for(int j=1;j<=N;j++)
{
if(map[i][j]&&belong[i]!=belong[j])
{
inDegree[belong[j]]++;
outDegree[belong[i]]++;
}
}
for(int i=1;i<=tot;i++)
{
if(!inDegree[i]) inZero++;
if(!outDegree[i]) outZero++;
}
if(tot==1) cout<<1<<endl<<0<<endl;
else cout<<inZero<<endl<<max(inZero,outZero)<<endl;
return 0;
}



[POJ 1236][IOI 1996]Network of Schools的更多相关文章

  1. POJ 1236——Network of Schools——————【加边形成强连通图】

    Network of Schools Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u ...

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

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

  3. [tarjan] poj 1236 Network of Schools

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

  4. POJ 1236 Network of Schools(Tarjan缩点)

    Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16806   Accepted: 66 ...

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

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

  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+缩点)

    Network of Schools Description A number of schools are connected to a computer network. Agreements h ...

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

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

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

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

随机推荐

  1. JPA入门样例(採用JPA的hibernate实现版本号)

    (1).JPA介绍: JPA全称为Java Persistence API ,Java持久化API是Sun公司在Java EE 5规范中提出的Java持久化接口.JPA吸取了眼下Java持久化技术的长 ...

  2. maven编译错误,警告: BASE64Decoder是内部专用 API, 可能会在未来发行版中删除

    修改红色部分版本号为2.3.2              <plugin>                 <groupId>org.apache.maven.plugins& ...

  3. [Gradle] Gradle 简介

    Gradle 是以 Groovy 语言为基础,面向Java应用为主.基于DSL(领域特定语言)语法的自动化构建工具. Ø gradle对多工程的构建支持很出色,工程依赖是gradle的第一公民. Ø ...

  4. #include &lt;NOIP2008 Junior&gt; 双栈排序 ——using namespace wxl;

    题目描述 Tom最近在研究一个有趣的排序问题.如图所示,通过2个栈S1和S2,Tom希望借助以下4种操作实现将输入序列升序排序. 操作a 如果输入序列不为空,将第一个元素压入栈S1 操作b 如果栈S1 ...

  5. java设计模式0--设计模式简介

    设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结.使用设计模式是为了可重用代码.让代码更容易被他人理解.保证代码可靠性. 设计模式的起源 软件 ...

  6. [Python爬虫] 之六:Selenium 常用控件用法

    Selenium 常用控件用法 1.文本框 上图中,如何定位搜索文本框,并输入搜索内容进行搜索 首先:利用方法 find_element_by_xpath定位元素:inputElements = se ...

  7. Hadoop I/O

    Hadoop自带一套原子操作用于数据I/O.当中一些技术,如数据完整性保持和压缩,对于处理多达数个TB的数据时.特别值得关注.另外一些Hadoop工具或API.所形成的构建模块可用于开发分布式系统.比 ...

  8. 【crontab】“bad minute”及“errors in crontab file, can't install”错误处理

    今天有朋友提到,在使用crontab定制后台定时备份任务时报出“bad minute”及“errors in crontab file, can't install”错误.经确认,根本原因是cront ...

  9. xcode_6_beta.dmg

    http://pan.baidu.com/s/1qW2lWoW password:5nty

  10. ArcGIS Add-In调试无法重新生成

    在调试ArcGIS Add-In时,出现错误:无法注册程序集"……\Projects\ArcGISAddIn\ArcGISAddIn\bin\Debug\ArcGISAddIn.dll&qu ...