poj-1236.network of schools(强连通分量 + 图的入度出度)
Network of Schools
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 Sample Output 1 Source |
/*************************************************************************
> File Name: poj-1236.network_of_schools.cpp
> Author: CruelKing
> Mail: 2016586625@qq.com
> Created Time: 2019年09月04日 星期三 19时53分40秒
本题大意:给定一个有向图,第一问是让你找出一些点,使得从这些点出发,可以到达图中的所有结点,输出结点数,第二问是问你在图中添加多少条边可以使得从任一点出发都可以访问到图中的其他所有结点.
本题思路:很典型的连通图问题,考虑第一问,求出图中所有的强连通分量,缩点之后建立新图,图中入度为0的点即是这些点,考虑第二问,由于求得的强连通分量都是可以相互到达的,因此我们只需要解决生成的新图的连通性问题,
也就是添加最少的边使得新图形成一个强连通分量,那最优的思路就是选一个入度为零的点让其他所有出度为零的点都指向他,或者选一个出度为零的点,让他指向每个入度为零的点,所以答案就是出度为零和入度为零间的最大值,需要特判的
是,如果原图就是一个强连通分量,那么就不需要加边,所求的的新图应该是一个点,所以这个答案需要特判,切记以上判断结点的出度入度都是求原图的出度和入度.
************************************************************************/ #include <cstdio>
#include <cstring>
using namespace std; const int maxn = + , maxm = * / + ;
int n;
struct Edge {
int from, to, next;
} edge[maxm], edge1[maxm];
int head[maxn], tot;
int low[maxn], dfn[maxn], stack[maxn], belong[maxn];
int Index, top;
int scc;
bool instack[maxn];
bool indegree[maxn];
bool outdegree[maxn]; int max(int a, int b) {
return a > b ? a : b;
} void init() {
tot = ;
memset(head, -,sizeof head);
} void addedge(int u, int v) {
edge[tot] = (Edge){u, v, head[u]}; head[u] = tot ++;
} void tarjan(int u) {
int v;
low[u] = dfn[u] = ++ Index;
stack[top ++] = u;
instack[u] = true;
for(int i = head[u]; ~i; i = edge[i].next) {
v = edge[i].to;
if(!dfn[v]) {
tarjan(v);
if(low[u] > low[v]) low[u] = low[v];
} else if(instack[v] && low[u] > dfn[v]) low[u] = dfn[v];
}
if(low[u] == dfn[u]) {
scc ++;
do {
v = stack[-- top];
instack[v] = false;
belong[v] = scc;
} while(v != u);
}
} void solve() {
memset(dfn, , sizeof dfn);
memset(instack, false, sizeof instack);
Index = scc = top = ;
for(int i = ; i <= n; i ++)
if(!dfn[i]) {
tarjan(i);
}
} bool vis[maxn]; int main() {
memset(indegree, false, sizeof indegree);
memset(outdegree, false, sizeof outdegree);
init();
scanf("%d", &n);
int x;
for(int i = ; i <= n; i ++) {
while(scanf("%d", &x) && x)
addedge(i, x);
}
solve();
for(int i = ; i <= n; i ++)
for(int k = head[i]; ~k; k = edge[k].next)
if(belong[i] != belong[edge[k].to]) {
indegree[belong[edge[k].to]] = true;
outdegree[belong[edge[k].from]] = true;
}
int in0 = , out0 = ;
for(int i = ; i <= scc; i ++) {
if(!indegree[i]) in0 ++;
if(!outdegree[i]) out0 ++;
}
out0 = max(in0, out0);
if(scc == ) out0 = ;
printf("%d\n%d\n", in0, out0);
return ;
}
poj-1236.network of schools(强连通分量 + 图的入度出度)的更多相关文章
- POJ 1236 Network Of Schools (强连通分量缩点求出度为0的和入度为0的分量个数)
Network of Schools A number of schools are connected to a computer network. Agreements have been dev ...
- POJ 1236 Network of Schools (强连通分量缩点求度数)
题意: 求一个有向图中: (1)要选几个点才能把的点走遍 (2)要添加多少条边使得整个图强联通 分析: 对于问题1, 我们只要求出缩点后的图有多少个入度为0的scc就好, 因为有入度的scc可以从其他 ...
- POJ1236 Network of Schools —— 强连通分量 + 缩点 + 入出度
题目链接:http://poj.org/problem?id=1236 Network of Schools Time Limit: 1000MS Memory Limit: 10000K Tot ...
- poj~1236 Network of Schools 强连通入门题
一些学校连接到计算机网络.这些学校之间已经达成了协议: 每所学校都有一份分发软件的学校名单("接收学校"). 请注意,如果B在学校A的分发名单中,则A不一定出现在学校B的名单中您需 ...
- POJ 1236 Network of Schools(强连通分量)
POJ 1236 Network of Schools 题目链接 题意:题意本质上就是,给定一个有向图,问两个问题 1.从哪几个顶点出发,能走全全部点 2.最少连几条边,使得图强连通 思路: #inc ...
- POJ 1236 Network of Schools(强连通 Tarjan+缩点)
POJ 1236 Network of Schools(强连通 Tarjan+缩点) ACM 题目地址:POJ 1236 题意: 给定一张有向图,问最少选择几个点能遍历全图,以及最少加入�几条边使得 ...
- Poj 1236 Network of Schools (Tarjan)
题目链接: Poj 1236 Network of Schools 题目描述: 有n个学校,学校之间有一些单向的用来发射无线电的线路,当一个学校得到网络可以通过线路向其他学校传输网络,1:至少分配几个 ...
- poj 1236 Network of Schools(又是强连通分量+缩点)
http://poj.org/problem?id=1236 Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Su ...
- [tarjan] poj 1236 Network of Schools
主题链接: http://poj.org/problem?id=1236 Network of Schools Time Limit: 1000MS Memory Limit: 10000K To ...
随机推荐
- jmeter录制对于ip代理会失效
jmeter对于ip代理会失效,ip不能走代理,只有域名可以,因此如果需要用jmeter录制ip代理的请求,需要配置hosts访问,将ip转换成域名 如访问http://127.0.0.1:8080/ ...
- idea编写第一个springboot程序
1. 创建一个 springboot 项目 使用 idea 创建的基本步骤: 2. 加入父级,起步依赖 pom.xml文件内容: <?xml version="1.0" en ...
- ubuntu1604-Python35-cuda9-cudnn7-gpu-dockerfile
一,在某目录下有如下文件: -rw-r--r-- 1 root root 1643293725 9月 2 11:46 cuda_9.0.176_384.81_linux.run -rw-r--r-- ...
- 一些 sql 调优的总结
一.sql 优化方案 1)列类型尽量定义成数值类型,且长度尽可能短,如主键和外键,类型字段等等 2)建立单列索引 3)根据需要建立多列联合索引.当单个列过滤之后还有很多数据,那么索引的效率将会 ...
- java文件分片上传,断点续传
文件夹数据库处理逻辑 publicclass DbFolder { JSONObject root; public DbFolder() { this.root = new JSONObject(); ...
- luogu 4059 [Code+#1]找爸爸 动态规划
Description 小A最近一直在找自己的爸爸,用什么办法呢,就是DNA比对.小A有一套自己的DNA序列比较方法,其最终目标是最 大化两个DNA序列的相似程度,具体步骤如下:1.给出两个DNA序列 ...
- C++ - C++简介
一.C与C++ 一般来说,计算机要处理两个概念--算法和数据.C在面世时是过程性的语言,过程性的语言指的是程序的过程较比其他语言,会更加的有序可读(清晰性和可读性).因为它把程序分解成各个分支,并执行 ...
- Elastic-Job介绍
1 什么是分布式任务调度 什么是分布式?当前软件的架构正在逐步转变为分布式架构,将单体结构分为若干服务,服务之间通过网络交互来完成用户的业务处理,如下图,电商系统为分布式架构,由订单服务.商品服务.用 ...
- linux文件夹目录含义及用途
/boot,存放linux启动文件和内核: /initrd,boot loader initialized RAM disk,就是由boot loader初始化的内存盘.在linux内核启动前,boo ...
- Logger工具类
org.slf4j.Logger的简单封装,传入所在类的class,和类名或全类名. public class LoggerFactory { public static Logger getLogg ...