zoukankan      html  css  js  c++  java
  • POJ 1236 Network of Schools(tarjan)

    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

    题目大意:有n个学校,每个学校能够单向到达某些学校,1.求出最少要给几个学校发软件才能使每个学校都有软件用 2.求出最少需要连接多少条边才能使任意学校出发都能到达其他学校
    思路:第一个问的话,我们先求出该图中的所有强连通分量,将每个强连通分量看成一个点,求出入度为0的强连通分量的个数num1即可;第二问则还需要求出强连通分量中出度为0的点的个数num2,最后取max(num1,num2)

     1 #include<iostream>
     2 #include<algorithm>
     3 #include<cstring>
     4 #include<vector>
     5 #include<stack>
     6 
     7 using namespace std;
     8 const int maxn = 205;
     9 int low[maxn],dfn[maxn];
    10 int vis[maxn],f[maxn],num[maxn];
    11 int in[maxn],out[maxn];
    12 vector<int>edge[maxn];
    13 int n,cnt,color;//cnt为low数组的节点数
    14 stack<int>Q;
    15 void tarjan(int u)
    16 {
    17     low[u] = dfn[u] = ++cnt;
    18     vis[u] = 1;
    19     Q.push(u);
    20     for(int i=0;i<edge[u].size();i++){
    21         int t = edge[u][i];
    22         if(!dfn[t]){
    23             tarjan(t);
    24             low[u] =min(low[u],low[t]);
    25         }else if(vis[t])
    26             low[u] = min(low[u],dfn[t]);
    27     }
    28     if(dfn[u]==low[u]){
    29         vis[u] = 0;
    30         f[u] = ++color;//染色缩点
    31         while((Q.top()!=u) && Q.size()){
    32             f[Q.top()] = color;
    33             vis[Q.top()] = 0;
    34             Q.pop();
    35         }
    36         Q.pop();
    37     }
    38 }
    39 void init()
    40 {
    41     memset(low,0,sizeof(low));
    42     memset(dfn,0,sizeof(dfn));
    43     memset(vis,0,sizeof(vis));
    44     memset(num,0,sizeof(num));
    45     memset(in,0,sizeof(in));
    46     memset(out,0,sizeof(out));
    47     memset(f,0,sizeof(f));
    48     cnt = 0;color=0;
    49 }
    50 int main()
    51 {
    52     while(scanf("%d",&n)!=EOF){
    53         init();
    54         for(int i=0;i<=n;i++)edge[i].clear();
    55         for(int x,i=1;i<=n;i++){
    56             while(scanf("%d",&x)&&x)
    57                 edge[i].push_back(x);
    58         }
    59         for(int i=1;i<=n;i++)
    60             if(!dfn[i])
    61                 tarjan(i);
    62         for(int i=1;i<=n;i++){
    63             for(int j=0;j<edge[i].size();j++){
    64                 int v = edge[i][j];
    65                 if(f[i]!=f[v]){//若不属于同一个强连通分量
    66                     in[f[v]]++;
    67                     out[f[i]]++;
    68                 }
    69             }
    70         }
    71         int ans1=0,ans2=0;
    72         for(int i=1;i<=color;i++){
    73             if(in[i]==0)ans1++;
    74             if(out[i]==0)ans2++;
    75         }
    76         if(color==1)printf("1
    0
    ");
    77         else printf("%d
    %d
    ",ans1,max(ans1,ans2));
    78     }
    79     return 0;
    80 }
    View Code
  • 相关阅读:
    [WPF 自定义控件]好用的VisualTreeExtensions
    [UWP]从头开始创建并发布一个番茄钟
    [WPF 自定义控件]使用TextBlockHighlightSource强化高亮的功能,以及使用TypeConverter简化调用
    [WPF 自定义控件]排序、筛选以及高亮
    《写给大家看的设计书》,推荐给想了解设计的程序员
    [WPF 自定义控件]使用WindowChrome自定义RibbonWindow
    [书籍]通过《番茄工作法图解》复习番茄工作法
    [WPF 自定义控件]使用WindowChrome的问题
    [WPF 自定义控件]使用WindowChrome自定义Window Style
    时时监听input内容的改变
  • 原文地址:https://www.cnblogs.com/wangrunhu/p/9681268.html
Copyright © 2011-2022 走看看