题目描述

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;
}

最新文章

  1. Replication的犄角旮旯(九)-- sp_setsubscriptionxactseqno,赋予订阅活力的工具
  2. AppStore 内购验证的方法
  3. javascript function new this
  4. js基础之弹性运动(四)
  5. Hibernate入门(1)-第一个Hibernate程序
  6. Swift闭包(Closure)
  7. R学习笔记
  8. MapReduce 规划 系列的12 使用Hadoop Streaming技术集成newLISP文字
  9. Sql Server的艺术(三) SQL聚合函数的应用
  10. 【Flask】微型web框架flask大概介绍
  11. Django的ORM那些相关操作
  12. 腾讯云服务器tomcat端口无法访问
  13. json、demjson
  14. 移动端与PC端的触屏事件
  15. 深入理解es6的promise
  16. IntelliJ IDEA中的properties文件乱码转成中文[unicode码转中文]
  17. linux/mac下命令行rm回收站--rmtrash
  18. Entity Framework实体拆分
  19. Codeforces 582C. Superior Periodic Subarrays(数学+计数)
  20. English——Unit 1

热门文章

  1. 网站开发学习Python实现-Django项目部署-介绍(6.2.1)
  2. Bootstrap留言板界面练习
  3. ctf/web源码泄露及利用办法
  4. Sqlmap 学习笔记1:sqlmap参数
  5. VSCode---REST Client接口测试辅助工具
  6. HCIP----静态实验
  7. 【Shiro学习之六】shiro编码/加密
  8. Zookeeper一致性协议——ZAB
  9. JAVA数据结构(十一)—— 堆及堆排序
  10. linux操作系统可以ping通ssh连接长时间无响应