[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. Apache服务器添加网站目录不在根目录的情况

    Apache原本根目录: /var/www 需要添加的新的Apache网站目录 /home/*** 在Apache服务器虚拟配置下添加一个站点 <VirtualHost *:> Serve ...

  2. windows10安装oracle11g报错ORA-01034、ORA-01078

    ORA-01034表示数据库实例未建立,可以先用管理员账号进入一个空白实例 sqlplus / as sysdba; 如果您当前使用的账号是安装oracle的账号,则不需要账号密码就可以登陆oracl ...

  3. ImageView设置边框 以及内部图片居中显示 在AndroidStudio中添加shape.xml文件

    效果如图 边框设置:shape文件 <shape xmlns:android="http://schemas.android.com/apk/res/android"> ...

  4. 计算机网络课设之基于UDP协议的简易聊天机器人

    前言:2017年6月份计算机网络的课设任务,在同学的帮助和自学下基本搞懂了,基于UDP协议的基本聊天的实现方法.实现起来很简单,原理也很简单,主要是由于老师必须要求使用C语言来写,所以特别麻烦,而且C ...

  5. bisai.py

    比赛专用py #!/usr/etc/env python #encoding:utf-8 #by i3ekr #token import re,os,requests res = "(fla ...

  6. SUSE 11.3 linux ISO下载地址

    http://linux.iingen.unam.mx/pub/Linux/Suse/isos/SLES11/ SLE-11-SP3-SDK-DVD-i586-GM-DVD1.iso 6deaa960 ...

  7. 5-3 Linux内核计时、延时函数与内核定时器【转】

    转自:http://www.xuebuyuan.com/510594.html 5-3 Linux内核计时.延时函数与内核定时器 计时 1. 内核时钟 1.1   内核通过定时器(timer)中断来跟 ...

  8. C中级 MariaDB Connector/C API 编程教程

    引言 - 环境搭建 首先开始环境搭建. 主要在Window 10 + Visual Studio 2015 上构建使用 mariadb connector/c api 进行数据操作开发. 为什么选择在 ...

  9. dubbo支持的远程调用方式

    dubbo RPC(二进制序列化 + tcp协议).http invoker(二进制序列化 + http协议,至少在开源版本没发现对文本序列化的支持).hessian(二进制序列化 + http协议) ...

  10. 手机端GPS定位结合百度地图实现定位

    html页面: <!DOCTYPE html>  <html>  <head>      <meta http-equiv="Content-Typ ...