• poj 1236 Network of Schools(又是强连通分量+缩点)


    http://poj.org/problem?id=1236

    Network of Schools
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 9481   Accepted: 3767

    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

     
    【题解】:
      这题大意是给一个有向图,求至少给多少个结点发消息能使消息传遍整个网络,并进一步求出至少添加多少条边能使对图中任意一个结点发消息都能使消息传遍整个网络。可以先用kosaraju将强连通分支缩点,得到原图的基图,然后统计入度为0的连通分量个数和出度为0的连通分量个数,入度为0的必须给它发消息,入度不为0的不必给发消息,所以第一问所求即为缩点后的图中入度为0的个数,至于第二问,只需将入度为0的结点与出度为0的结点连接即可满足要求,最少需加边数目为两者之中的较大者,需注意的是,单只有一个连通分量时,输出结果为0 。
     
    【code】:
     
      1 /**
      2 Judge Status:Accepted      Memory:772K
      3 Time:0MS     Language:G++
      4 Code Length:2155B   Author:cj
      5 */
      6 
      7 #include<iostream>
      8 #include<stdio.h>
      9 #include<stack>
     10 #include<string.h>
     11 #include<algorithm>
     12 #include<vector>
     13 
     14 #define N 110
     15 using namespace std;
     16 
     17 vector<int> G[N];
     18 int pre[N],lowlink[N],sccno[N],dfs_cnt,scc_cnt;
     19 stack<int> stk;
     20 
     21 int visit[N][N],in[N],out[N];
     22 void Tarjan(int u)
     23 {
     24     pre[u] = lowlink[u] = ++dfs_cnt;
     25     stk.push(u);
     26     int i;
     27     for(i=0;i<G[u].size();i++)
     28     {
     29         int v = G[u][i];
     30         if(!pre[v])
     31         {
     32             Tarjan(v);
     33             lowlink[u] = min(lowlink[u],lowlink[v]);
     34         }
     35         else if(!sccno[v])
     36         {
     37             lowlink[u] = min(lowlink[u],pre[v]);
     38         }
     39     }
     40     if(pre[u]==lowlink[u])
     41     {
     42         int x;
     43         scc_cnt++;
     44         do
     45         {
     46             x = stk.top();
     47             stk.pop();
     48             sccno[x] = scc_cnt;
     49         }while(x!=u);
     50     }
     51 }
     52 
     53 void findncc(int n)
     54 {
     55     dfs_cnt = scc_cnt = 0;
     56     memset(pre,0,sizeof(pre));
     57     memset(lowlink,0,sizeof(lowlink));
     58     memset(sccno,0,sizeof(sccno));
     59     int i;
     60     for(i=1;i<=n;i++)   if(!pre[i]) Tarjan(i);
     61 }
     62 
     63 void getNewMap(int n)
     64 {
     65     int i,j;
     66     memset(visit,0,sizeof(visit));
     67     memset(in,0,sizeof(in));
     68     memset(out,0,sizeof(out));
     69     for(i=1;i<=n;i++)
     70     {
     71         for(j=0;j<G[i].size();j++)
     72         {
     73             int v = sccno[G[i][j]];
     74             int u = sccno[i];   //注意是对sccno[i]数组里的强联通分量进行操作,也就是缩点的过程
     75             if(u!=v)
     76             {
     77                 if(!visit[u][v])
     78                 {
     79                     visit[u][v] = 1;
     80                     in[v]++;   //出度入度统计
     81                     out[u]++;
     82                 }
     83             }
     84         }
     85     }
     86 }
     87 int main()
     88 {
     89     int n;
     90     scanf("%d",&n);
     91     int i;
     92     for(i=1;i<=n;i++)
     93     {
     94         int a;
     95         G[i].clear();
     96         while(~scanf("%d",&a)&&a)
     97         {
     98             G[i].push_back(a);
     99         }
    100     }
    101     findncc(n);
    102     getNewMap(n);
    103     int cnt_in = 0,cnt_out = 0;
    104     for(i=1;i<=scc_cnt;i++)
    105     {
    106         if(!in[i])  cnt_in++;
    107         if(!out[i]) cnt_out++;
    108     }
    109     printf("%d
    ",cnt_in);
    110     if(scc_cnt!=1)  printf("%d
    ",max(cnt_in,cnt_out));  //联通分量只有一个输出0
    111     else    printf("0
    ");
    112     return 0;
    113 }
  • 相关阅读:
    python中函数的定义,调用,全局变量,局部变量,函数的嵌套使用初级篇
    逐行解释和整体解释的理解
    python中函数参数的详解,可变参数,关键字参数等
    python中is和==的区别
    python中的模块,以及包的导入的总结
    Python当中的a += a 与 a = a + a 的区别,可变类型与不可变类型的数据类型,引用传参
    学习Java的第七天
    学习Java的第八天
    学习java的第六天
    学习java第二天
  • 原文地址:https://www.cnblogs.com/crazyapple/p/3253266.html
Copyright © 2020-2023  润新知