博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj-1236.network of schools(强连通分量 + 图的入度出度)
阅读量:4477 次
发布时间:2019-06-08

本文共 4896 字,大约阅读时间需要 16 分钟。

Network of Schools

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 27121   Accepted: 10704

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

52 4 3 04 5 0001 0

Sample Output

12

Source

 

 

1 /*************************************************************************  2     > File Name: poj-1236.network_of_schools.cpp  3     > Author: CruelKing  4     > Mail: 2016586625@qq.com   5     > Created Time: 2019年09月04日 星期三 19时53分40秒  6     本题大意:给定一个有向图,第一问是让你找出一些点,使得从这些点出发,可以到达图中的所有结点,输出结点数,第二问是问你在图中添加多少条边可以使得从任一点出发都可以访问到图中的其他所有结点.  7     本题思路:很典型的连通图问题,考虑第一问,求出图中所有的强连通分量,缩点之后建立新图,图中入度为0的点即是这些点,考虑第二问,由于求得的强连通分量都是可以相互到达的,因此我们只需要解决生成的新图的连通性问题, 也就是添加最少的边使得新图形成一个强连通分量,那最优的思路就是选一个入度为零的点让其他所有出度为零的点都指向他,或者选一个出度为零的点,让他指向每个入度为零的点,所以答案就是出度为零和入度为零间的最大值,需要特判的 是,如果原图就是一个强连通分量,那么就不需要加边,所求的的新图应该是一个点,所以这个答案需要特判,切记以上判断结点的出度入度都是求原图的出度和入度.  8  ************************************************************************/  9  10 #include 
11 #include
12 using namespace std; 13 14 const int maxn = 100 + 5, maxm = 100 * 100 / 2 + 5; 15 int n; 16 struct Edge { 17 int from, to, next; 18 } edge[maxm], edge1[maxm]; 19 int head[maxn], tot; 20 int low[maxn], dfn[maxn], stack[maxn], belong[maxn]; 21 int Index, top; 22 int scc; 23 bool instack[maxn]; 24 bool indegree[maxn]; 25 bool outdegree[maxn]; 26 27 int max(int a, int b) { 28 return a > b ? a : b; 29 } 30 31 void init() { 32 tot = 0; 33 memset(head, -1,sizeof head); 34 } 35 36 void addedge(int u, int v) { 37 edge[tot] = (Edge){u, v, head[u]}; head[u] = tot ++; 38 } 39 40 void tarjan(int u) { 41 int v; 42 low[u] = dfn[u] = ++ Index; 43 stack[top ++] = u; 44 instack[u] = true; 45 for(int i = head[u]; ~i; i = edge[i].next) { 46 v = edge[i].to; 47 if(!dfn[v]) { 48 tarjan(v); 49 if(low[u] > low[v]) low[u] = low[v]; 50 } else if(instack[v] && low[u] > dfn[v]) low[u] = dfn[v]; 51 } 52 if(low[u] == dfn[u]) { 53 scc ++; 54 do { 55 v = stack[-- top]; 56 instack[v] = false; 57 belong[v] = scc; 58 } while(v != u); 59 } 60 } 61 62 void solve() { 63 memset(dfn, 0, sizeof dfn); 64 memset(instack, false, sizeof instack); 65 Index = scc = top = 0; 66 for(int i = 1; i <= n; i ++) 67 if(!dfn[i]) { 68 tarjan(i); 69 } 70 } 71 72 bool vis[maxn]; 73 74 int main() { 75 memset(indegree, false, sizeof indegree); 76 memset(outdegree, false, sizeof outdegree); 77 init(); 78 scanf("%d", &n); 79 int x; 80 for(int i = 1; i <= n; i ++) { 81 while(scanf("%d", &x) && x) 82 addedge(i, x); 83 } 84 solve(); 85 for(int i = 1; i <= n; i ++) 86 for(int k = head[i]; ~k; k = edge[k].next) 87 if(belong[i] != belong[edge[k].to]) { 88 indegree[belong[edge[k].to]] = true; 89 outdegree[belong[edge[k].from]] = true; 90 } 91 int in0 = 0, out0 = 0; 92 for(int i = 1; i <= scc; i ++) { 93 if(!indegree[i]) in0 ++; 94 if(!outdegree[i]) out0 ++; 95 } 96 out0 = max(in0, out0); 97 if(scc == 1) out0 = 0; 98 printf("%d\n%d\n", in0, out0); 99 return 0;100 }

 

转载于:https://www.cnblogs.com/bianjunting/p/11461674.html

你可能感兴趣的文章
iOS开发——捕获崩溃信息
查看>>
(for 循环)编程找出四位整数 abcd 中满足 (ab+cd)(ab+cd)=abcd 的数
查看>>
tomcat使用spring-loaded实现应用热部署
查看>>
boost1.53中的lock-free
查看>>
链表_leetcode203
查看>>
基于ajax 的 几个例子 session ,ajax 实现登录,验证码 ,实现ajax表单展示
查看>>
连接不上sql server服务器的解决方案
查看>>
【Java面试题】58 char型变量中能不能存贮一个中文汉字?为什么?
查看>>
C++ Primer 第六章 函数
查看>>
交互设计算法基础(3) - Quick Sort
查看>>
Ubuntu各种软件的安装
查看>>
java开发环境搭建-慕课网
查看>>
NOIP2015-D2T3运输计划
查看>>
Z :彻底了解指针数组,数组指针以及函数指针 [复
查看>>
2013年终总结
查看>>
Start to study Introduction to Algorithms
查看>>
AE常见接口之间的关系(较笼统)+arcgis常见概念
查看>>
正则表达式
查看>>
Mysql的DATE_FORMAT()日期格式转换
查看>>
SparkStreaming入门及例子
查看>>