[POJ1236]Network of Schools
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 18969   Accepted: 7467

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

 
题目大意:一个有向图代表学校间的传输网络
     1.要将一文件发放到至少几所学校保证所有学校都能获得文件
     2.至少要添加几条有向边才能从任意学校发出文件使所有学校都可以获得
 
试题分析:缩点,第一问就可以直接得出是入度为0的强联通分量的个数。
        第二问就是MAX(入度为0,出度为0)的强联通分量个数。
     为什么呢?我们只需要将所有出度为0连向入度为0连一条边就好了。
     注意特判只有一个强联通分量的情况。
代码:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std; inline int read(){
int x=0,f=1;char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
for(;isdigit(c);c=getchar()) x=x*10+c-'0';
return x*f;
}
const int MAXN=1001;
const int INF=999999;
int N,M;
vector<int> vec[MAXN];
int dfn[MAXN],low[MAXN],tar[MAXN];
int que[MAXN];
bool inq[MAXN];
int tmp,Col,tot;
int ind[MAXN],oud[MAXN]; void Tarjan(int x){
que[++tmp]=x;
++tot; dfn[x]=low[x]=tot;
inq[x]=true;
for(int i=0;i<vec[x].size();i++){
int to=vec[x][i];
if(!dfn[to]){
Tarjan(to);
low[x]=min(low[x],low[to]);
}
else if(inq[to]) low[x]=min(low[x],dfn[to]);
}
if(low[x]==dfn[x]){
++Col;
tar[x]=Col;
inq[x]=false;
while(que[tmp]!=x){
int k=que[tmp];
tar[k]=Col;
inq[k]=false;
tmp--;
}
tmp--;
}
return ;
}
int ans1,ans2;
int main(){
N=read();
for(int i=1;i<=N;i++){
int v=read();
while(v!=0){
vec[i].push_back(v);
v=read();
}
}
for(int i=1;i<=N;i++) if(!dfn[i]) Tarjan(i);
for(int i=1;i<=N;i++){
for(int j=0;j<vec[i].size();j++){
if(tar[i]!=tar[vec[i][j]]){
ind[tar[vec[i][j]]]++;
oud[tar[i]]++;
}
}
}
if(Col==1){
puts("1\n0");
return 0;
}
for(int i=1;i<=Col;i++)
if(!ind[i]) ans1++;
for(int i=1;i<=Col;i++)
if(!oud[i]) ans2++;
printf("%d\n%d\n",ans1,max(ans1,ans2));
}

  

【图论】Network of Schools的更多相关文章

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

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

  2. Network of Schools --POJ1236 Tarjan

    Network of Schools Time Limit: 1000MS Memory Limit: 10000K Description A number of schools are conne ...

  3. [强连通分量] POJ 1236 Network of Schools

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

  4. POJ1236 - Network of Schools tarjan

                                                     Network of Schools Time Limit: 1000MS   Memory Limi ...

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

    Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12240   Accepted: 48 ...

  6. POJ1236 Network of Schools (强连通)(缩点)

                                                                Network of Schools Time Limit: 1000MS   ...

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

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

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

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

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

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

随机推荐

  1. C# 获取一段日期内的工作日

    /// <summary> /// 根据指定时间段计算工作日天数 /// </summary> /// <param name="firstDay"& ...

  2. python实战===itchat

    import itchat itchat.login() friends=itchat.get_friends(update=True)[0:] male=female=other=0 for i i ...

  3. MinnowBoard

    MinnowBoard https://github.com/RafaelRMachado/MinnowBoard https://github.com/RafaelRMachado https:// ...

  4. 集合类---Map

    Map常用的子类: 一.HashMap详解  1.特点 1)线程不安全.如果想要得到线程安全的HashMap,可以使用Collections的静态方法:Map map = Collections.sy ...

  5. aspxpivotgrid 导出excel时,非绑定咧显示为0的情况

    using DevExpress.XtraPrinting; Exporter.ExportXlsToResponse(this.Title,TextExportMode.Text,true); // ...

  6. Java Redis 连接池 Jedis 工具类

    import org.slf4j.Logger; import org.slf4j.LoggerFactory; import redis.clients.jedis.Jedis; import re ...

  7. RabbitMQ 实践及使用

    目录 - 1. RabbitMQ的安装    - 1.1 配置好 epel    - 1.2 安装 RPM包    - 1.3 创建用户设置权限- 2. RabbitMQ组件- 3. RabbitMQ ...

  8. JavaScript中创建对象的5种模式

    构造函数模式 实现方式: function Person(name, age, job) { this.name = name; this.age = age; this.job = job; thi ...

  9. Django Ajax学习一

    1. 简单的加法 <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...

  10. C语言写随机数

    #include <stdio.h> #include <stdlib.h> #include <time.h> ; unsigned int rand0(); v ...