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 ...
随机推荐
- shred命令
不做陈冠希必备.... shred --help 用法:shred [选项]... 文件... Overwrite the specified FILE(s) repeatedly, in order ...
- iOS使用protobuf环境的配置
配置protobuf需要HomeBrew工具或则是MacPort.如没有安装,则需要配置HomeBrew工具或则是MacPort. 步骤1(环境配置前的准备工作): 1:使用HomeBrew brew ...
- 通过源码安装PostgresSQL
通过源码安装PostgresSQL 1.1 下载源码包环境: Centos6.8 64位 yum -y install bison flex readline-devel zlib-devel yum ...
- 设置HTML的TextArea标记跟随文本内容自动设置高度
写内容的时候用的是textarea来写,可以换行,然后预览页面也要显示是换行才行,所以预览页面还是要用textarea来显示, 样式去掉边框,不可以拉伸,不可编辑 // html <textar ...
- 【leetcode】1172. Dinner Plate Stacks
题目如下: You have an infinite number of stacks arranged in a row and numbered (left to right) from 0, e ...
- Linux的解压缩相关命令
Linux的解压缩相关命令 知识点: 1.zip命令 2.tar命令 3.压缩和解压常用组合
- SpringCloud学习系列-Eureka服务注册与发现(2)
构建 microservicecloud-eureka-7001 eureka服务注册中心Module 1.新建microservicecloud-eureka-7001 2.pom <proj ...
- js中[]、{}、()区别
一.{ } 大括号,表示定义一个对象,大部分情况下要有成对的属性和值,或是函数体 {}表示对象.[]表示对象的属性.方法,()如果用在方法名后面,代表调用 如:var LangShen = {&quo ...
- Buffer转成字符串
如果data为buffer格式,则: data.toString()
- 可持久化Trie模板
如果你了解过 01 Trie 和 可持久化线段树(例如 : 主席树 ).那么就比较好去可持久化 Trie 可持久化 Trie 当 01 Trie 用的时候能很方便解决一些原本 01 Trie 不能解决 ...