zoukankan      html  css  js  c++  java
  • POJ1236

                                                     Network of Schools
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 5354   Accepted: 2106

    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

     

    题意:

    有N个学校...每个学校可能向几个学校进行数据传输,问至少需要把一个文件给几个学校可以使给的N个学校都收到文件,再问在加几个通信线路可以使各个学校之间都能直接或间接的传递文件;

    题解:首先跑一发tarjan缩点,

    第一问就是问重建图后入度为0的点,如果没有至少是1;

    第二问就是max(入度为0,出度为0);

    ///1085422276
    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <ctime>
    #include <iostream>
    #include <algorithm>
    #include <set>
    #include <vector>
    #include <queue>
    #include <typeinfo>
    #include <map>
    typedef long long ll;
    using namespace std;
    #define inf 10000000
    inline ll read()
    {
        ll x=0,f=1;
        char ch=getchar();
        while(ch<'0'||ch>'9')
        {
            if(ch=='-')f=-1;
            ch=getchar();
        }
        while(ch>='0'&&ch<='9')
        {
            x=x*10+ch-'0';
            ch=getchar();
        }
        return x*f;
    }
    //***************************************************************
    struct ss
    {
        int to,next;
    } e[1000*1000*2];
    int belong[2000],hav[2010];
    int scc,n,top;
    int in[2001],out[2001],ansl,ansr;
    int head[1011],dfn[1011],low[1101],cnt,t,vis[1101],q[5000],inq[1111];
    void init()
    {
        memset(in,0,sizeof(in));
        memset(out,0,sizeof(out));
        memset(head,0,sizeof(head));
        top=0;
        scc=0;
        cnt=0;
        t=1;
        memset(inq,0,sizeof(inq));
        memset(hav,0,sizeof(hav));
        memset(vis,0,sizeof(vis));
    }
    void add(int u,int v)
    {
        e[t].to=v;
        e[t].next=head[u];
        head[u]=t++;
    }
    void dfs(int a)
    {
        int now=0;
        dfn[a]=low[a]=++cnt;
        vis[a]=inq[a]=1;
        q[++top]=a;
        for(int i=head[a]; i; i=e[i].next)
        {
            if(!vis[e[i].to])
            {
                dfs(e[i].to);
                low[a]=min(low[a],low[e[i].to]);
            }
            else
            {
                if(inq[e[i].to])low[a]=min(low[a],dfn[e[i].to]);///在队列中
            }
        }
        if(low[a]==dfn[a])
        {
            scc++;
            while(now!=a)
            {
                now=q[top--];
                inq[now]=0;
                belong[now]=scc;
                ++hav[scc];
            }
        }
    }
    void rebuild()
    {
        for(int i=1; i<=n; i++)
        {
            for(int j=head[i]; j; j=e[j].next)
            {
                if(belong[i]!=belong[e[j].to])
                {
                    out[belong[i]]++;
                    in[belong[e[j].to]]++;
                }
            }
        }
        int A=0,B=0;
        for(int i=1; i<=scc; i++)
        {
            if(in[i]==0)A++;
            if(out[i]==0)B++;
        }
        ansr=A;
        ansl=max(A,B);
    }
    void tarjan()
    {
        for(int i=1; i<=n; i++)if(!vis[i])dfs(i);
        rebuild();
    }
    int main()
    {
        while(scanf("%d",&n)!=EOF)
        {
            init();
            int x;
            for(int i=1; i<=n; i++)
            {
                while(scanf("%d",&x)!=EOF)
                {
                    if(x==0)break;
                    add(i,x);
                }
            }
            tarjan();
            if(scc==1)ansl=0,ansr=1;
            printf("%d
    %d
    ",ansr,ansl);
        }
    
        return 0;
    }
    代码
  • 相关阅读:
    css 实现的纸张卷曲效果
    前端如何优化代码&前端web安全
    React native
    君士坦丁堡分叉引起的安全问题
    不用外部插件启用u盘ntfs写功能
    使用ubuntu搭建时间机器备份服务
    从一起“盗币”事件再谈合约安全问题
    如何让你的项目同时支持go vendor和go module
    golang plugin的依赖问题
    Plasma Cash合约解读
  • 原文地址:https://www.cnblogs.com/zxhl/p/4753670.html
Copyright © 2011-2022 走看看