题目描述

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. HBase过滤器:SingleColumnValueFilter和FirstKeyOnlyFilter一起使用的问题

    FirstKeyOnlyFilter是对第一列进行过滤,hbase中的列按照字典序排列,所以如果SingleColumnValueFilter中的过滤列不是第一列的话,FirstKeyOnlyFilt ...

  2. Linux内核中container_of宏的详细解释

    上一节拒绝造轮子!如何移植并使用Linux内核的通用链表(附完整代码实现)我们在分析Linux内核链表的时候注意到内核在求解结构体偏移的时候巧妙的使用了container_of宏定义,今天我们来详细剖 ...

  3. rbd锁引起kvm虚拟机无法启动的故障

    前言 环境因为一些问题(网络,或者磁盘,或者其它各种异常),引起了集群的状态的一些变化,变化之后,集群的某些虚拟机正常某些虚拟机出现异常,异常现象就是无法启动 特别是win server2008 ,会 ...

  4. 刚入坑之C#《方法》解说

    说好的用一周时间学方法,我都快耽误成两周了.原因就是跟着传智播客的课程做了个飞行棋项目,想要梳理其中的方法却把自己绕晕了.那接下来我先说一下我学到方法的内容,在最后献上飞行器项目的代码,当然是传智播客 ...

  5. HW之蓝队防守

    待看文章: https://blog.csdn.net/DBappSecurity_/article/details/107364216?utm_medium=distribute.pc_releva ...

  6. 【java学习笔记2】访问控制修饰符 public、protected、默认、private

    先写了一个User()类: package chapter01; public class User { // 私有的 private int id; // 受保护的 protected int ag ...

  7. C#随机生成不重复邀请码&创建登录Token&转换人民币大小金额

    /// <summary> /// 创建登陆Token /// </summary> /// <param name="length">< ...

  8. Maven的工程类型有哪些?

    POM工程:POM工程是逻辑工程.用在父级工程或聚合工程中.用来做jar包的版本控制. JAR工程:将会打包成jar用作jar包使用.即常见的本地工程 - Java Project. WAR工程:将会 ...

  9. 死磕以太坊源码分析之MPT树-上

    死磕以太坊源码分析之MPT树-上 前缀树Trie 前缀树(又称字典树),通常来说,一个前缀树是用来存储字符串的.前缀树的每一个节点代表一个字符串(前缀).每一个节点会有多个子节点,通往不同子节点的路径 ...

  10. 简单4步,利用Prometheus Operator实现自定义指标监控

    本文来自Rancher Labs 在过去的文章中,我们花了相当大的篇幅来聊关于监控的话题.这是因为当你正在管理Kubernetes集群时,一切都会以极快的速度发生变化.因此有一个工具来监控集群的健康状 ...