zoukankan      html  css  js  c++  java
  • Network of Schools(强连通分量+缩点) (问添加几个点最少点是所有点连接+添加最少边使图强连通)

    Network of Schools
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 13801   Accepted: 5505

    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
    题解:
    
    
      1. /题意:  
      2. //给你一个N个点的有向图,问1,最少连接几个点可以直接或间接 连接到所有点。  
      3. //                         2,最少增加几条边使图强连通。  
      4. //思路SCC + 缩点后。统计出入度为0的SCC数sumin和出度为0的SCC数sumout。  
      5. //答案一就是sumin,答案二就是max(sumin, sumout)。注意只有一个SCC时,输出1 0。 
    代码:
     1 #include<iostream>
     2 #include<algorithm>
     3 #include<cstdio>
     4 #include<cstring>
     5 #include<cmath>
     6 #include<vector> 
     7 #include<stack>
     8 #define mem(x,y) memset(x,y,sizeof(x))
     9 using namespace std;
    10 const int INF=0x3f3f3f3f;
    11 const int MAXN=110;
    12 vector<int>vec[MAXN];
    13 stack<int>S;
    14 int dfn[MAXN],low[MAXN],Instack[MAXN],in[MAXN],out[MAXN];
    15 int dfs_blocks,scc,sccon[MAXN];
    16 void targin(int u,int fa){
    17     S.push(u);
    18     Instack[u]=1;
    19     dfn[u]=low[u]=++dfs_blocks;
    20     for(int i=0;i<vec[u].size();i++){
    21         int v=vec[u][i];
    22         if(!dfn[v]){
    23             targin(v,u);
    24             low[u]=min(low[u],low[v]);
    25         }
    26         else if(Instack[v]){
    27             low[u]=min(low[u],dfn[v]);
    28         }
    29     }
    30     if(low[u]==dfn[u]){
    31         scc++;
    32         while(1){
    33             int v=S.top();
    34             S.pop();
    35             Instack[v]=0;
    36             sccon[v]=scc;
    37             if(u==v)break;
    38         }
    39     }
    40 }
    41 int main(){
    42     int N;
    43     while(~scanf("%d",&N)){
    44         scc=0;
    45         for(int i=0;i<MAXN;i++)vec[i].clear();
    46         while(!S.empty())S.pop();
    47         mem(in,0);mem(out,0);mem(Instack,0);
    48         mem(dfn,0);mem(low,0);mem(sccon,0);
    49         for(int i=1;i<=N;i++){
    50             int x;
    51             while(scanf("%d",&x),x!=0){
    52                 vec[i].push_back(x);
    53             }
    54         }
    55         for(int i=1;i<=N;i++){
    56             if(!dfn[i]){
    57                 targin(i,-1);
    58             }
    59         }
    60         for(int i=1;i<=N;i++){
    61             for(int j=0;j<vec[i].size();j++){
    62                 int v=vec[i][j];
    63                 if(sccon[i]!=sccon[v])in[sccon[v]]++,out[sccon[i]]++;
    64             }
    65         }
    66     //    printf("%d
    ",scc);
    67         int numin=0,numout=0;
    68         if(scc==1){
    69             puts("1
    0");continue;
    70         }
    71         for(int i=1;i<=scc;i++){
    72             if(in[i]==0)numin++;
    73             if(out[i]==0)numout++;
    74         }
    75         printf("%d
    %d
    ",numin,max(numin,numout));
    76     }
    77     return 0;
    78 }




  • 相关阅读:
    nyoj 420
    nyoj 46 最少乘法次数
    ACM退役贴
    nyoj 187 快速查找素数
    多校4题目之Trouble
    nyoj 56 阶乘因式分解(一)
    nyoj 70 阶乘因式分解(二)
    nyoj 151 Biorhythms
    nyoj 97 兄弟郊游问题
    多校十 hdoj4393 Throw nails
  • 原文地址:https://www.cnblogs.com/handsomecui/p/4905531.html
Copyright © 2011-2022 走看看