zoukankan      html  css  js  c++  java
  • poj-1236.network of schools(强连通分量 + 图的入度出度)

    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

    5
    2 4 3 0
    4 5 0
    0
    0
    1 0
    

    Sample Output

    1
    2
    

    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 <cstdio> 11 #include <cstring> 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 %d ", in0, out0); 99 return 0; 100 }
  • 相关阅读:
    厦门航空牵手阿里云打造航空业移动研发中台,研发效率提升50%
    可能是国内第一篇全面解读 Java 现状及趋势的文章
    这样才能正确解锁MaxCompute客户端
    MaxCompute问答整理之10月
    tensorflow入门
    buctoj——合法的出栈顺序
    nyoj299——如何优雅的写矩阵快速幂
    nyoj164——卡特兰数(待填坑)
    nyoj139——康托展开
    字符串练习
  • 原文地址:https://www.cnblogs.com/bianjunting/p/11461674.html
Copyright © 2011-2022 走看看