zoukankan      html  css  js  c++  java
  • POJ 1236 Network of Schools (有向图的强连通分量)

    Network of Schools
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 9073   Accepted: 3594

    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

     
     
     
    这题给了一个有向图。
     
    需要解决两个问题:
    第一是需要给多少个点,才能传遍所有点。
     
    第二问是加多少条边,使得整个图变得强连通。
     
    使用Tarjan进行缩点,得到一个SCC图、
     
    这个图有多少个入度为0的,多少个出度为0的。
     
    假设有n个入度为0,m个出度为0
     
    那么第一个答案就是n,第二个答案是max(n,m)
     
     
    具体证明不解释了,貌似以前做过的题目,有解释。
     
    需要注意的是假如只有一个强连通分量,即整个图是连通的,那么第一个答案是1,第二个答案是0
     
     
    #include <stdio.h>
    #include <string.h>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    
    const int MAXN = 110;
    const int MAXM = 110*110;
    
    struct Edge
    {
        int to,next;
    }edge[MAXM];
    int head[MAXN],tot;
    int Low[MAXN],DFN[MAXN],Stack[MAXN],Belong[MAXN];
    int Index,top;
    int scc;
    bool Instack[MAXN];
    
    void addedge(int u,int v)
    {
        edge[tot].to = v;edge[tot].next = head[u];head[u] = tot++;
    }
    void Tarjan(int u)
    {
        int v;
        Low[u] = DFN[u] = ++Index;
        Stack[top++] = u;
        Instack[u] = true;
        for(int i = head[u];i != -1;i = edge[i].next)
        {
            v = edge[i].to;
            if(!DFN[v])
            {
                Tarjan(v);
                if(Low[u] > Low[v])
                      Low[u] = Low[v];
            }
            else if(Instack[v] && Low[u] > DFN[v])
                Low[u] = DFN[v];
        }
        if(Low[u] == DFN[u])
        {
            scc++;
            do
            {
                v = Stack[--top];
                Belong[v] = scc;
                Instack[v] = false;
            }
            while( v!= u);
        }
    }
    int in[MAXN],out[MAXN];
    void solve(int N)
    {
        memset(DFN,0,sizeof(DFN));
        memset(Instack,false,sizeof(Instack));
        Index = scc = top = 0;
        for(int i = 1;i <= N;i++)
            if(!DFN[i])
               Tarjan(i);
        if(scc == 1)
        {
            printf("1
    0
    ");
            return;
        }
        for(int i = 1;i <= scc;i++)
           in[i] = out[i] = 0;
        for(int u = 1;u <= N;u++)
        {
            for(int i = head[u];i != -1;i = edge[i].next)
            {
                int v = edge[i].to;
                if(Belong[u] != Belong[v])
                {
                    in[Belong[v]]++;
                    out[Belong[u]]++;
                }
            }
        }
        int ans1=0,ans2=0;
        for(int i = 1;i <= scc;i++)
        {
            if(in[i]==0)ans1++;
            if(out[i]==0)ans2++;
        }
        printf("%d
    %d
    ",ans1,max(ans1,ans2));
    
    }
    void init()
    {
        tot = 0;
        memset(head,-1,sizeof(head));
    }
    int main()
    {
        int n;
        int v;
        while(scanf("%d",&n) == 1)
        {
            init();
            for(int i = 1;i <= n;i++)
            {
                while(scanf("%d",&v)==1 && v)
                {
                    addedge(i,v);
                }
            }
            solve(n);
        }
        return 0;
    }
     
     
     
     
     
     
     
     
  • 相关阅读:
    对多分类采用10折交叉验证评估实验结果
    对二分类采用10折交叉验证评估实验结果
    对回归采用10折交叉验证评估实验结果
    在多分类任务实验中 用torch.nn实现 𝑳𝟐 正则化
    在多分类任务实验中手动实现 使用 𝑳𝟐 正则化
    随学随用的python-note
    Scala类型参数中协变(+)、逆变(-)、类型上界(<:)和类型下界(>:)的使用
    Scala写排序可以说是简洁又明了
    Scala中的apply实战详解
    Scala单例对象、伴生对象实战详解
  • 原文地址:https://www.cnblogs.com/kuangbin/p/3182611.html
Copyright © 2011-2022 走看看