题目描述

n个学校构成一个有向图,通过m条边连接,一:问至少向图中多少个学校投放软件,可以使得所有学校直接或者间接的通过边(假设存在边(u,v),则向u投放v可以得到,而向v投放u不能通过v直接得到)得到软件(假设每次投放的软件无穷多)。二:问至少添加多少条边可以使得只用向一个学校投放软件别的学校都能得到软件

输入格式

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.

输出格式

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.


分析题目可以得出一条结论:在一个强连通分量之内的学校只需要一个学校得到软件,那么整个强连通分量都可以得到。

对于第一问:很显然,我们把所有强连通分量缩点之后,有多少个入度为0的点就是第一问的答案。

第二问的意思就是加入最少的边使得DAG变成一个强连通图。显然,我们只需要从出度为0的点连向入度为0的点即可。所以设入度为0的点数量为cnt_ind,出度为0的点数量为cnt_outd,那么答案就是max(cnt_ind,cnt_outd)。

用Tarjan求强连通分量,时间复杂度为O(N+M)

#include <iostream>
#include <cstring>
#include <cstdio>
#define maxn 10000 + 5
#define maxm 10000 + 5
using namespace std; struct edge {
int from, to, next;
edge() {}
edge(register const int &_from, register const int &_to, register const int &_next) {
from = _from;
to = _to;
next = _next;
}
} e[maxm], ed[maxm]; int head[maxn], k;
int dfn[maxn], low[maxn], tot;
int stack[maxn], top, vis[maxn];
int col[maxn], cnt;
int ind[maxn], outd[maxn];
int n; inline void add(register const int &u, register const int &v) {
e[k] = edge(u, v, head[u]);
head[u] = k++;
} inline void tarjan(register const int &u) {
dfn[u] = low[u] = ++tot;
stack[++top] = u;
vis[u] = true;
for(register int i = head[u]; ~i; i = e[i].next) {
register int v = e[i].to;
if(!dfn[v]) {
tarjan(v);
low[u] = min(low[u], low[v]);
} else if(vis[v]) {
low[u] = min(low[u], dfn[v]);
}
} if(dfn[u] == low[u]) {
register int v;
cnt++;
do {
v = stack[top--];
col[v] = cnt;
vis[v] = false;
} while(u != v);
}
} int main() {
memset(head, -1, sizeof head);
scanf("%d", &n);
for(register int i = 1, v; i <= n; i++) {
while(scanf("%d", &v) == 1 && v) {
add(i, v);
}
} for(register int i = 1; i <= n; i++) if(!dfn[i]) {
tarjan(i);
} for(register int i = 0; i < k; i++) {
register int u = col[e[i].from], v = col[e[i].to];
if(u != v) {
outd[u]++;
ind[v]++;
}
} register int cnt_ind = 0, cnt_outd = 0;
for(register int i = 1; i <= cnt; i++) {
if(!ind[i]) cnt_ind++;
if(!outd[i]) cnt_outd++;
}
if(cnt == 1) printf("1\n0\n");
else printf("%d\n%d\n", cnt_ind, max(cnt_ind, cnt_outd)); return 0;
}

Pku1236 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. Docker 笔记学习

    文章目录 概述 CentOS7 Docker 安装 启动 Docker 后台服务 镜像加速 Docker常用命令 如何创建一个镜像 存储和载入镜像 上传镜像 容器的基本操作 创建容器 终止容器 如何进 ...

  2. Js 添加cookie,写入cookie到主域

    if (getCookie("content") != null && getCookie("content") != "" ...

  3. 前后端分离项目获取后端跨控制器获取不到session

    最近做前后端分离项目(.net core web api  +vue)时,后台跨控制器不能获取到session.由于配置的是共享的session.本来以为是共享session出了问题,就在共享sess ...

  4. 有了它(powermock)再也不担心单元测试不达标了

    为什么要写单元测试 优点:单元测试可以减少bug率,提升代码的质量.还可以通过单元测试来熟悉业务. 公司硬性要求:有些公司可能还会强制要求,每次新增代码.或者变更代码单测覆盖率要达到多少比例才能申请代 ...

  5. CSS解析

    CSS(层叠样式表) CSS层叠样式表(Cascading Style Sheets)是一种用来表现HTML(标准通用标记语言的一个应用)或XML(标准通用标记语言的一个子集)等文件样式的计算机语言. ...

  6. 企业集群架构-02-Rsync

    Rsync 目录 Rsync Rsync基本概述 Rsync应用场景 Rsync传输模式 Rsync服务使用 (1)服务端安装Rsync (2)服务端配置Rsync (3)服务端创建用户 (4)服务端 ...

  7. ROS开源小车TurtleBot3详情介绍(Burger)

    您为什么要选择ROS开源智能小车 ROS(RobotOperating System,机器人操作系统)是目前世界上更主流更多人使用的的机器人开源操作系统.它可以提供操作系统应有的服务,包括硬件抽象,底 ...

  8. 第十八章节 BJROBOT 安卓手机 APP 建地图【ROS全开源阿克曼转向智能网联无人驾驶车】

    1.把小车平放在地板上,用资料里的虚拟机,打开一个终端 ssh 过去主控端启动roslaunch znjrobotbringup.launch 2.在虚拟机端再打开一个终端,ssh 过去主控端启动ro ...

  9. sql操作数据库(1)-->DDL、DML、DQL

    SQL 操作数据库 概念:结构化查询语言 Structured Quary Language 作用:  1.是一种数据库的查询的标准,对所有的数据库都支持  2.不同的数据库SQL语句可能有点不同 ( ...

  10. HDFS中大数据常见运维指令总结

    一.查看HDFS下的参数信息 [root@master ~]# hdfs Usage: hdfs [--config confdir] COMMAND where COMMAND is one of: ...