zoukankan      html  css  js  c++  java
  • POJ1236 Network of Schools

    POJ1236 Network of Schools

    Time Limit: 1000MS Memory Limit: 10000K

    Total Submissions: 5649 Accepted: 2236

    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(2<N<100)各学校之间有单向的网络,每个学校得到一套软件后,可以通过单向网络向周边的学校传输,问题1:初始至少需要向多少个学校发放软件,使得网络内所有的学校最终都能得到软件。2,至少需要添加几条传输线路(边),使任意向一个学校发放软件后,经过若干次传送,网络内所有的学校最终都能得到软件。

    解题思路:1.先求强连通分支,然后把图缩小成为一个有向无环图;

    2.求出这个有向无环图中,入度为0的点的个数为a,出度为0的点的个数为b;

    3.第一个问题的答案就是a,第二个问题的答案为max{a,b};

    4.最恶心的地方:当一开始这个图就是一个强连通图的时候,第二问题的答案为0。

    #include <stdio.h>
    #include <string.h>
    #include <vector>
    #include <algorithm>
    using namespace std;
    
    vector<int>g1[105],g2[105];
    int n,vis[105],vis2[105],num[105],id[105],gro_id[105],now;
    
    void dfs1(int s)
    {
        vis[s]=1;
        for(int i=0;i<g1[s].size();i++)
            if(vis[g1[s][i]]==0)
                dfs1(g1[s][i]);
        num[s]=++now;
    }
    
    void dfs2(int s)
    {
        vis[s]=1;
        gro_id[s]=now;
        for(int i=0;i<g2[s].size();i++)
            if(vis[g2[s][i]]==0)
                dfs2(g2[s][i]);
    }
    
    bool cmp(int a,int b)
    {
        return num[a]>num[b];
    }
    
    int main()
    {
        scanf("%d",&n);
        memset(vis,0,sizeof(vis));
        memset(num,0,sizeof(num));
        memset(gro_id,0,sizeof(gro_id));
        for(int i=1;i<=n;i++)
            g1[i].clear(),g2[i].clear();
        for(int i=1;i<=n;i++)id[i]=i;
        for(int i=1;i<=n;i++)
        {
            while(1)
            {
                int a;
                scanf("%d",&a);
                if(!a)break;
                g1[i].push_back(a);
                g2[a].push_back(i);
            }
        }
        now=0;
        for(int i=1;i<=n;i++)
            if(!vis[i])dfs1(i);
        sort(id+1,id+1+n,cmp);
        memset(vis,0,sizeof(vis));
        now=0;
        for(int i=1;i<=n;i++)
            if(!vis[id[i]])now++,dfs2(id[i]);
        if(now==1)
        {
            puts("1\n0");
            return 0;
        }
        memset(vis,0,sizeof(vis));
        memset(vis2,0,sizeof(vis2));
        for(int i=1;i<=n;i++)
            for(int j=0;j<g1[i].size();j++)
                if(gro_id[i]!=gro_id[g1[i][j]])
                {
                    vis[gro_id[i]]++;
                    vis2[gro_id[g1[i][j]]]++;
                }
        int le=0,ri=0;
        for(int i=1;i<=now;i++)
        {
            if(vis[i]==0)le++;
            if(vis2[i]==0)ri++;
        }
        printf("%d\n%d\n",ri,le>ri?le:ri);
    }
    

      

  • 相关阅读:
    MERGE引擎 分表后 快速查询所有数据
    MYSQL导入中文数据乱码的四种解决办法
    数据库中为什么不推荐使用外键约束?
    Word转PDF
    YII2 更新数据不成功
    YII2 使用curl请求,返回false
    Yii集成PHPWord
    网站安全DDOS攻击及监测
    nginx日志
    定时任务秒级执行
  • 原文地址:https://www.cnblogs.com/Fatedayt/p/2179124.html
Copyright © 2011-2022 走看看