[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. 数据分析之CE找数据大法

    一.基本介绍 CE的全称为Cheat Engine,是一款内存修改编辑工具,其官网是http://www.cheatengine.org,可以在其官网下载到最新的CE工具,目前最新版本是Cheat E ...

  2. 20、redis和memcached比较?

    1.Redis和Memcache都是将数据存放在内存中,都是内存数据库.不过memcache还可用于缓存其他东西,例如图片.视频等等: 2.Redis不仅仅支持简单的k/v类型的数据,同时还提供lis ...

  3. setTimeout()和setInterval()方法的区别

    setTimeout(); //5秒后执行yourFunction(),只执行一次 setInterval(); //每隔5秒执行一次 1.setTimeout(funhander,time)的作用是 ...

  4. new操作符的内部运行解析

    在加上new操作符,我们就能完成传统面向对象的class + new的方式创建对象,在Javascript中,我们将这类方式成为Pseudoclassical. 基于上面的例子,我们执行如下代码   ...

  5. 2017-2018-1 20179205《Linux内核原理与设计》第十周作业

    <Linux内核原理与设计>第十周作业 教材17.19.20章学习及收获 1.在Linux以及所有unix系统中,设备被分为以下三种:块设备(blkdev)以块为单位寻址,通过块设备节点来 ...

  6. 利用procdump+Mimikatz 绕过杀软获取Windows明文密码

    思路: 就是通过系统自带的procdump去下载存储用户名密码的文件(应该不能那么说这个文件,但是这样理解没问题),然后用猕猴桃读取. procdump.exe Procdump是一个轻量级的Sysi ...

  7. C++学习之路(一):const与define,结构体对齐,new/delete

    前言:针对C++ Primer和Effective C++两本书,以及技术博客与实验测试,本系列主要是针对C++进行系统化学习,记录学习中遇到的问题和经验. (一)const与define 关于con ...

  8. Linux 入门记录:十七、Linux 命令行文本/文件处理工具

    一.文件浏览 cat 查看文件内容 more 以翻页形式查看文件内容(只能向下翻页) less 以翻页形式查看文件内容(可以上下翻页) head 查看文件的头几行(默认10行) tail 查看文件的尾 ...

  9. MySQL多线程复制故障(slave_pending_jobs_size_max)

    MySQL多线程复制故障(slave_pending_jobs_size_max) http://www.xuchanggang.cn/archives/1079.html

  10. sicily 1036. Crypto Columns

    Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description The columnar encryption scheme scram ...