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 ...
随机推荐
- ubuntu 7z解压
安装方法: sudo apt-get install p7zip 解压文件: 7z x manager.7z -r -o /home/xx 解释如下: x 代表解压缩文件,并且是按原始 ...
- word和画图
文档和画图收费文档:edu.51cto.com/course/course_id-4992.htmledu.51cto.com/course/course_id-4991.html
- 【Leetcode】位1的个数
解题方案:位操作的技巧 整数 n 和 n-1(n>0) 做与运算,从其二进制形式来看,可以消掉 n 的二进制数值中最后1个 “1” .循环进行,每次消掉1个 “1” .整数 n 的二进制数值中有 ...
- (45)FreeRTOS学习之二
一:架构概述 FreeRTOS是一个相对较小的应用程序.最小化的FreeRTOS内核仅包括3个(.c)文件和少数头文件,总共不到9000行代码,还包括了注释和空行.一个典型的编译后(二进制)代码映像小 ...
- android和网络连接相关的类URL,URLConnection,HttpURLConnection,HttpClient
这几个类都是用于和服务器端的连接,有些功能都能够实现,关系是: 一.URL URL标识着网络上的一个资源:该类包含一些URL自身的方法,如获取URL对应的主机名称,端口号,协议,查询字符串外,还有些方 ...
- long poll、ajax轮询和WebSocket
websocket 的认识深刻有木有.所以转到我博客里,分享一下.比较喜欢看这种博客,读起来很轻松,不枯燥,没有布道师的阵仗,纯粹为分享.废话这么多了,最后再赞一个~ WebSocket是出的东西(协 ...
- Codeforces Aim Tech Round4 (Div2) D
题目链接: 题意: 给你一个严格升序的单链表,但是是用数组来存放的.对于每一个位置来说,你可以知道这个位置的值和下一个的位置.你每一个可以询问一个位置,机器会告诉你这个位置的值,和下一个位置的指针.要 ...
- Promethus
https://blog.csdn.net/zl1zl2zl3/article/details/74332437
- SpringMvc中ModelAndView模型的应用
/** * 目标方法的返回值可以是 ModelAndView 类型. * 其中可以包含视图和模型信息 * SpringMVC 会把 ModelAndView 的 model 中数据放入到 reques ...
- MySQL的explain分析sql语句
explain分析查询 使用 EXPLAIN 关键字可以模拟优化器执行SQL查询语句,从而知道MySQL是如何处理你的SQL语句的.这可以帮你分析你的查询语句或是表结构的性能瓶颈.通过explain命 ...