zoukankan      html  css  js  c++  java
  • POJ 1236

    Network of Schools
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 10500   Accepted: 4189

    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

     
    强连通缩点,第一问是求入度为0的点,第二问是求入度为0和出度为0的点数 的最大值
     
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <algorithm>
     5 #include <stack>
     6 
     7 using namespace std;
     8 
     9 const int MAX_N = 105;
    10 int first[MAX_N],v[MAX_N * MAX_N],Next[MAX_N * MAX_N];
    11 int low[MAX_N],pre[MAX_N],cmp[MAX_N];
    12 int ind[MAX_N],oud[MAX_N];
    13 int N;
    14 int dfs_clock,scc_cnt;
    15 stack<int > S;
    16 
    17 
    18 void dfs(int u) {
    19         low[u] = pre[u] = ++dfs_clock;
    20         S.push(u);
    21         for(int e = first[u]; e != -1; e = Next[e]) {
    22                 if(!pre[ v[e] ]) {
    23                         dfs(v[e]);
    24                         low[ u] = min(low[u],low[ v[e] ]);
    25                 } else {
    26                         if(!cmp[ v[e] ]) {
    27                                 low[u] = min(low[u],pre[ v[e] ]);
    28                         }
    29                 }
    30         }
    31 
    32         if(low[u] == pre[u]) {
    33                 ++scc_cnt;
    34                 for(;;) {
    35                         int x = S.top(); S.pop();
    36                         cmp[x] = scc_cnt;
    37                         if(x == u) break;
    38                 }
    39         }
    40 }
    41 void scc() {
    42         dfs_clock = scc_cnt = 0;
    43         memset(cmp,0,sizeof(cmp));
    44         memset(pre,0,sizeof(pre));
    45 
    46         for(int i = 1; i <= N; ++i)  if(!pre[i]) dfs(i);
    47 }
    48 
    49 void add_edge(int id,int u) {
    50         int e = first[u];
    51         Next[id] = e;
    52         first[u] = id;
    53 }
    54 
    55 void solve() {
    56         scc();
    57         for(int i = 1; i <= N; ++i) {
    58                 for(int e = first[i]; e != -1; e = Next[e]) {
    59                         if(cmp[ v[e] ] == cmp[i]) continue;
    60                         ind[ cmp[ v[e] ] ]++;
    61                         oud[ cmp[i] ]++;
    62                 }
    63         }
    64         int in = 0,ou = 0;
    65         for(int i = 1; i <= scc_cnt; ++i) {
    66                 if(!ind[i]) in++;
    67                 if(!oud[i]) ou++;
    68         }
    69         printf("%d
    %d
    ",in,scc_cnt == 1 ? 0 : max(in,ou));
    70 
    71 }
    72 int main()
    73 {
    74    //freopen("sw.in","r",stdin);
    75 
    76     scanf("%d",&N);
    77     int len = 0;
    78     for(int i = 1; i <= N; ++i) first[i] = -1;
    79 
    80     for(int i = 1; i <= N; ++i) {
    81             int ch;
    82             scanf("%d",&ch);
    83             while(ch != 0) {
    84                    // printf("%d ",ch);
    85 
    86                     add_edge(len,i);
    87                     v[len++] = ch;
    88                     scanf("%d",&ch);
    89             }
    90             //cout << endl;
    91     }
    92 
    93     solve();
    94     //cout << "Hello world!" << endl;
    95     return 0;
    96 }
    View Code
  • 相关阅读:
    推荐几个漂亮实用的JS菜单
    让文字与input居中对齐
    CSS的优先级特性
    博客园2007年度工作总结
    一些页面自动跳转的实现
    C#中的数字格式化、格式日期格式化[转]
    phpBB 3.07 bug 以及 phpBB bug tracker的bug
    Checkout Now Alipay.com
    不敢再用QQ邮箱、Foxmail
    已修复Tree Style Tab XML解析错误(Tree Style Tab 0.10.2010040201)
  • 原文地址:https://www.cnblogs.com/hyxsolitude/p/3697259.html
Copyright © 2011-2022 走看看