Pku1236 Network of Schools
题目描述
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的更多相关文章
- POJ 1236 Network of Schools(Tarjan缩点)
Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 16806 Accepted: 66 ...
- Network of Schools --POJ1236 Tarjan
Network of Schools Time Limit: 1000MS Memory Limit: 10000K Description A number of schools are conne ...
- [强连通分量] POJ 1236 Network of Schools
Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 16803 Accepted: 66 ...
- POJ1236 - Network of Schools tarjan
Network of Schools Time Limit: 1000MS Memory Limi ...
- POJ 1236 Network of Schools (Tarjan + 缩点)
Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 12240 Accepted: 48 ...
- POJ1236 Network of Schools (强连通)(缩点)
Network of Schools Time Limit: 1000MS ...
- POJ 1236 Network of Schools (有向图的强连通分量)
Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 9073 Accepted: 359 ...
- poj 1236 Network of Schools(连通图入度,出度为0)
http://poj.org/problem?id=1236 Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Su ...
- poj 1236 Network of Schools(又是强连通分量+缩点)
http://poj.org/problem?id=1236 Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Su ...
随机推荐
- 网站开发学习Python实现-Django学习-介绍(6.1.1)
@ 目录 1.MVT 2.ORM 关于作者 1.MVT 主要的目的是为了快速,简便的开发数据库驱动的网站,强调代码的复用,多个组件可以很方便以插件的方式服务于整个框架,采用的是MVT设计模式(差不多的 ...
- xss未看完的文章
https://blog.csdn.net/fen0707/article/details/8596888 XSS介绍与攻击 http://xss.fbisb.com/w ...
- 容器编排系统k8s之Service资源
前文我们了解了k8s上的DemonSet.Job和CronJob控制器的相关话题,回顾请参考:https://www.cnblogs.com/qiuhom-1874/p/14157306.html:今 ...
- 【实时渲染】实时3D渲染如何加速汽车线上体验应用推广
在过去,一支优秀的广告片足以让消费者对一辆汽车产生兴趣.完美的底盘线条或引擎的轰鸣声便会让潜在买家跑到经销商那里试驾.现在,广告还是和往常一样,并没有失去其特性,但86%的买家在与销售交流之前会在网上 ...
- OSPF综合实验
实验要求: 1.R4为ISP,其上只能配置IP地址,R4与其他所有直连设备间使用共有IP 2.R3--R5/6/7为MGRE环境,R3为中心站点 3.整个OSPF环境IP地址为172.16.0.0/1 ...
- [EF] - 作为DAL层遇到的问题
今天在部署一个经典三层的项目的时候,用到了EntityFramework,碰到几个问题: 在用EntityFramework将数据库导入到DAL层后,在BL层引用该DAL后,在测试项目的时候,想要查询 ...
- vue结合element-ui table本地分页
<template> <el-table :data="tableData1.slice((start1-1)*length1,start1*length1)" ...
- FPT: Feature Pyramid Transfomer
导言: 本文介绍了一个在空间和尺度上全活跃特征交互(fully active feature interaction across both space and scales)的特征金字塔transf ...
- 第十五章节 BJROBOT cartographer 算法构建地图【ROS全开源阿克曼转向智能网联无人驾驶车】
建地图前说明:请确保你的小车已经校正好 IMU.角速度.线速度,虚拟机配置好 ROS 网络的前提进行,否则会造成构建地图无边界.虚拟机端无法正常收到小车主控端发布的话题数据等异常情况!! 1.把小车平 ...
- Hbase相关参数详解
转载:http://www.cnblogs.com/nexiyi/p/hbase_config_94.html 版本:0.94-cdh4.2.1 hbase-site.xml配置 hbase.tmp. ...