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 ...
随机推荐
- J2EE知识总结——面试、笔试
9.2 jdk 1.8的新特性(核心是Lambda 表达式) 参考链接:http://www.bubuko.com/infodetail-690646.html (1)接口的默认方法 (给接口添加一个 ...
- 【leetcode&CN&竞赛】1198.Find Smallest Common Element in All Rows
题目如下: 给你一个矩阵 mat,其中每一行的元素都已经按 递增 顺序排好了.请你帮忙找出在所有这些行中 最小的公共元素. 如果矩阵中没有这样的公共元素,就请返回 -1. 示例: 输入:mat = [ ...
- C++调试的骚操作
打LCT时突然发现的骚操作 举个栗子 正常调试下应该是这样的 然后用光标选中函数名时-- 可以发现函数被运行了一次(每选中一次都会运行) 然而当函数带了变量时就布星了
- Logstash的filter插件介绍
一 官网说明 过滤器插件对事件执行中介处理.通常根据事件的特征有条件地应用过滤器. 以下过滤器插件在下面可用. Plugin Description Github repository aggrega ...
- 【BZOJ3876】 [Ahoi2014]支线剧情
Description [故事背景] 宅男JYY非常喜欢玩RPG游戏,比如仙剑,轩辕剑等等.不过JYY喜欢的并不是战斗场景,而是类似电视剧一般的充满恩怨情仇的剧情.这些游戏往往 都有很多的支线剧情,现 ...
- C++二维数组(指针)做参数
一.问题描述 使用C++编程过程中经常需要使用到二维数组,然而初级程序员在使用过程中经常会出错使程序崩溃.下面就二维指针的定义,初始化,以及二维指针做参数给出简单介绍. 1.二维数组的定义与初始化 在 ...
- 综合KPI报表历史明细数据查询
一.综合计划部KPI明细数据查询--xigu用户要求:需显示第三季度,即789三个月的明细数据解决方法:1.查看SSISC:\Users\Administrator\Documents\Visual ...
- uiautomatorviewer报错 Error taking device screenshot: EOF
报以下错误 估计是端口冲突 解决方法: 1. netstat -ano | findstr 5037 查看占用5037端口的进程 2. taskkill /pid 10508 /f 杀掉此进程 3 ...
- lua源码学习篇四:字节码指令
在llimits.h文件中定义了指令的类型.其实就是32个字节. typedef lu_int32 Instruction; 上节说到变量最终会存入proto的数组k中,返回的索引放在expdesc ...
- 红帽虚拟化RHEV-架构简介
目录 目录 软件环境 RHEV简介 RHEV与KVM的区别 RHEV的组成 RHEV-MManager RHEV-HHypervisor 虚拟机管理程序 存储 RHEV的架构 LDAPIPAAD We ...