zoukankan      html  css  js  c++  java
  • poj 1236 Network of Schools

    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
    题意不用说了,很显然,要是一个有向无环图,那么题目就简单了,入度为0的点的个数就是问题一,入度为0的点数和出度为0的点的个数的最大值就是解,那就是怎样把图化成一个有向无环图呢,那是做一遍SCC,还有就是注意只有一个强两桶分量时的情况
    #include<map>
    #include<set>
    #include<stack>
    #include<queue>
    #include<cmath>
    #include<vector>
    #include<cstdio>
    #include<string>
    #include<cstring>
    #include<cstdlib>
    #include<iostream>
    #include<algorithm>
    #define  inf 0x0f0f0f0f
    using namespace std;
    
    int indegree[100+10],outdegree[100+10];
    
    struct SCC
    {
        static const int maxn=1000+10;
        vector<int>group[maxn],scc[maxn];
        int pre[maxn],lowlink[maxn],sccno[maxn],dfs_clock,scc_cnt,n,m;
        stack<int>S;
    
        void init()
        {
            for (int i=0;i<=n;i++) group[i].clear();
        }
    
        void addedge(int from,int to)
        {
            group[from].push_back(to);
        }
    
        void dfs(int u)
        {
            pre[u]=lowlink[u]=++dfs_clock;
            S.push(u);
            for (int i=0;i<group[u].size();i++)
            {
                int v=group[u][i];
                if (!pre[v])
                {
                    dfs(v);
                    lowlink[u]=min(lowlink[u],lowlink[v]);
                }
                else if (!sccno[v])
                {
                    lowlink[u]=min(lowlink[u],pre[v]);
                }
            }
            if (lowlink[u]==pre[u])
            {
                scc_cnt++;
                scc[scc_cnt].clear();
                while (1)
                {
                    int x=S.top();
                    S.pop();
                    scc[scc_cnt].push_back(x);
                    sccno[x]=scc_cnt;
                    if (x==u) break;
                }
            }
        }
    
        void find_scc()
        {
            dfs_clock=scc_cnt=0;
            memset(pre,0,sizeof(pre));
            memset(sccno,0,sizeof(sccno));
            for (int i=1;i<=n;i++)
            if (!pre[i]) dfs(i);
        }
    };
    
    SCC school;
    
    int main()
    {
        int x;
        while (scanf("%d",&school.n)!=EOF)
        {
            school.init();
            for (int i=1;i<=school.n;i++)
            {
                while(scanf("%d",&x)!=EOF && x)
                {
                    school.addedge(i,x);
                }
            }
            school.find_scc();
            for (int i=1;i<=school.scc_cnt;i++)
            {
                indegree[i]=0;
                outdegree[i]=0;
            }
            for (int u=1;u<=school.n;u++)
            {
                for (int i=0;i<school.group[u].size();i++)
                {
                    int v=school.group[u][i];
                    if (school.sccno[u] != school.sccno[v])
                    {
                        indegree[school.sccno[v]]++;
                        outdegree[school.sccno[u]]++;
                    }
                }
            }
            int ans1=0,ans2=0;
            for (int i=1;i<=school.scc_cnt;i++)
            {
                if (indegree[i]==0) ans1++;
                if (outdegree[i]==0) ans2++;
            }
            int ans=max(ans1,ans2);
            if (school.scc_cnt==1)
            {
                ans1=1; ans=0;
            }
            printf("%d
    %d
    ",ans1,ans);
        }
        return 0;
    }
    至少做到我努力了
  • 相关阅读:
    HTTP Server
    分享:uSTL 2.0 发布,STL 标准模板库
    Boost.Asio和ACE之间关于Socket编程的比较
    使用BOOST实现简单的HTTP网页下载
    HTTP Client
    轩辕高端IT培训中心系昆山轩辕软件技术有限公司旗下的IT培训部门
    分享:链表实现的队列
    镕、喆、冇用五笔为何打不出来,在此向各位好友请教! 已回答 搜搜问问
    第一讲 Linux 编程入门与基本编程工具的使用
    C++03:使用Boost-用Asio实现简易Echo Server
  • 原文地址:https://www.cnblogs.com/chensunrise/p/3740921.html
Copyright © 2011-2022 走看看