zoukankan      html  css  js  c++  java
  • Network of Schools(poj1236 Tarjan)

    Network of Schools
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 27846   Accepted: 10960

    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

    解题思路: 题目意思求选则最少的点可以到达所有点      任意一个点可以到达其他所有的点
    Tarjan缩点   求入读为0的点  和    第二个把所有的出度为0的点连到入读为0的点即可  那么就是求max(入读为0点的个数和出度为0点的个数)
     1 #include <iostream>
     2 #include <cstring>
     3 #include <cstdio>
     4 #include <algorithm>
     5 using namespace std;
     6 
     7 const int maxn=110;
     8 int n;
     9 int cnt,head[maxn];
    10 int dfn[maxn],low[maxn],scc[maxn],stk[maxn],index=0,sccnum=0,top=0;
    11 struct Edge{
    12     int v,nxt;
    13 }G[maxn*maxn];
    14 void init(){ cnt=0; memset(head,-1,sizeof(head)); }
    15 void add(int u,int v){
    16     G[cnt].v=v;
    17     G[cnt].nxt=head[u];
    18     head[u]=cnt++;
    19 }
    20 
    21 void tarjan(int root){   //tarjan 模板
    22     if(dfn[root]) return;
    23     dfn[root]=low[root]=++index;
    24     stk[++top]=root;
    25 
    26     for(int i=head[root];~i;i=G[i].nxt){
    27         int v=G[i].v;
    28         if(!dfn[v]){
    29             tarjan(v);
    30             low[root]=min(low[root],low[v]);
    31         }
    32         else if(!scc[v]){   //如果还在站内
    33             low[root]=min(low[root],dfn[v]);
    34         }
    35     }
    36     if(low[root]==dfn[root]){
    37         sccnum++;
    38         for(;;){
    39             int x=stk[top--];
    40             scc[x]=sccnum;
    41             if(x==root) break;
    42         }
    43     }
    44 }
    45 
    46 int in[maxn],out[maxn];
    47 void solve(){
    48     index=0,sccnum=0,top=0;
    49     memset(dfn,0,sizeof(dfn));
    50     memset(low,0,sizeof(low));
    51     memset(scc,0,sizeof(scc));
    52     memset(stk,0,sizeof(stk));
    53     for(int i=1;i<=n;i++){
    54         if(!dfn[i]){
    55             tarjan(i);
    56         }
    57     }
    58     if(sccnum==1){   //特判
    59         printf("1
    0
    ");
    60         return;
    61     }
    62     for(int i=1;i<=sccnum;i++){
    63         in[i]=0,out[i]=0;
    64     }
    65     for(int i=1;i<=n;i++){   //重新建图
    66         for(int j=head[i];~j;j=G[j].nxt){
    67             int v=G[j].v;
    68             if(scc[i]!=scc[v]){
    69                 in[scc[v]]++;
    70                 out[scc[i]]++;
    71             }
    72         }
    73     }
    74     int res1=0,res2=0;
    75     for(int i=1;i<=sccnum;i++){
    76         if(in[i]==0) res1++;
    77         if(out[i]==0) res2++;
    78     }
    79     printf("%d
    %d
    ",res1,max(res1,res2));
    80 }
    81 
    82 int main(){
    83     while(scanf("%d",&n)==1){
    84         init();
    85         for(int i=1,d1;i<=n;i++){
    86             while(scanf("%d",&d1)==1&&d1){
    87                 add(i,d1);
    88             }
    89         }
    90         solve();
    91     }
    92     return 0;
    93 
    94 }
    View Code
  • 相关阅读:
    第九届蓝桥杯省赛c/c++真题明码题解答案,另类excel解法思路
    Windows下将Python源代码.py文件封装成exe可执行文件方法
    windows下python自带的pip安装速度过慢解决方案
    解决:Errors were encountered while processing
    ubuntu 安装 caffe 解决://home/xiaojie/anaconda/lib/libpng16.so.16:对‘inflateValidate@ZLIB_1.2.9’未定义的引用
    ubuntu安装caffe 解决:build_release/tools/caffe: error while loading shared libraries: libcudart.so.8.0: cannot open shar
    ubuntu 禁止内核更新
    ubutun 中notebook出现 Permission denied: Untitled.ipynb
    ubuntu下anaconda使用jupyter notebook加载tensorflow、pytorch
    ubuntu下用anaconda快速安装 pytorch
  • 原文地址:https://www.cnblogs.com/qq-1585047819/p/11962467.html
Copyright © 2011-2022 走看看