• luogu2746 校园网


    题目大意:

    一些学校连入一个电脑网络。那些学校已订立了协议:每个学校都会给其它的一些学校分发软件(称作“接受学校”)。注意即使 B 在 A 学校的分发列表中, A 也不一定在 B 学校的列表中。

    你要写一个程序计算,根据协议,为了让网络中所有的学校都用上新软件,必须接受新软件副本的最少学校数目(子任务 A)。更进一步,我们想要确定通过给任意一个学校发送新软件,这个软件就会分发到网络中的所有学校。为了完成这个任务,我们可能必须扩展接收学校列表,使其加入新成员。计算最少需要增加几个扩展,使得不论我们给哪个学校发送新软件,它都会到达其余所有的学校(子任务 B)。一个扩展就是在一个学校的接收学校列表中引入一个新成员。

    这道题可以看出由连通块知识解决,但问题出在子任务B上。我们应当这样思考:对几个连通块进行加边使得它们合并成一个连通块的效果,便是没有入度或出度为0的连通块。所以我们可以把入度为0的连通块放在左边,出度为0的连通块放在右边,我们要做的,就是建立最少的连接左右两侧连通块的边,使得两侧每个连通块都连着一条边。所以答案便是max{入度为0的连通块个数,入度为1的连通块个数}。

    //#define _DEBUG
    
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <vector>
    #include <stack>
    using namespace std;
    
    const int MAX_NODE = 150;
    
    struct Block
    {
        int InDegree, OutDegree;
    }_blocks[MAX_NODE];
    int _blockCnt;
    
    struct Node
    {
        vector<Node*> Next;
        int DfsN, Low;
        bool InStack;
        Block *BlockIn;
    }_nodes[MAX_NODE];
    stack<Node*> St;
    int _vCount, DfsCnt;
    
    void PopStack(Node *cur)
    {
        Block *curBlock = _blocks + ++_blockCnt;
        Node *temp;
        do {
            temp = St.top();
            St.pop();
            temp->BlockIn = curBlock;
            temp->InStack = false;
        } while (temp != cur);
    }
    
    void Dfs(Node *cur)
    {
        cur->DfsN = cur->Low = ++DfsCnt;
        cur->InStack = true;
        St.push(cur);
        for (int i = 0; i < cur->Next.size(); i++)
        {
            if (!cur->Next[i]->DfsN)
            {
                Dfs(cur->Next[i]);
                cur->Low = min(cur->Low, cur->Next[i]->Low);
            }
            else if (cur->Next[i]->InStack)
                cur->Low = min(cur->Low, cur->Next[i]->DfsN);
        }
        if (cur->Low == cur->DfsN)
            PopStack(cur);
    }
    
    void Tarjan()
    {
        for (int i = 1; i <= _vCount; i++)
            if (!_nodes[i].DfsN)
                Dfs(_nodes + i);
    }
    
    void GetDegree()
    {
        for (int i = 1; i <= _vCount; i++)
            for (int j = 0; j < _nodes[i].Next.size(); j++)
                if (_nodes[i].BlockIn != _nodes[i].Next[j]->BlockIn)
                {
                    _nodes[i].BlockIn->OutDegree++;
                    _nodes[i].Next[j]->BlockIn->InDegree++;
                }
    }
    
    int GetRootCnt()
    {
        int ans = 0;
        for (int i = 1; i <= _blockCnt; i++)
            ans += (_blocks[i].InDegree == 0);
        return ans;
    }
    
    int GetLeafCnt()
    {
        int ans = 0;
        for (int i = 1; i <= _blockCnt; i++)
            ans += (_blocks[i].OutDegree == 0);
        return ans;
    }
    
    int main()
    {
        scanf("%d", &_vCount);
        for (int u = 1; u <= _vCount; u++)
        {
            int v;
            while(scanf("%d", &v) && v)
                _nodes[u].Next.push_back(_nodes + v);
        }
        Tarjan();
    	if (_blockCnt == 1)
    	{
    		printf("1
    0
    ");
    		return 0;
    	}
        GetDegree();
        int rootCnt = GetRootCnt(), leafCnt = GetLeafCnt();
        printf("%d
    %d
    ", rootCnt, max(rootCnt, leafCnt));
        return 0;
    }
    

      

  • 相关阅读:
    Map
    Collection接口之Set
    Collection接口之List、泛型
    简介
    递归
    File类
    转换流InputStreamReader、OutputStreamWriter
    springmvc
    集合
    SpringCloud学习笔记
  • 原文地址:https://www.cnblogs.com/headboy2002/p/9397870.html
Copyright © 2020-2023  润新知