Network of Schools (强连通分量+缩点)

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

题目大概:

每个学校都可以把软件复制好,交给它名单上的学校。

问题A:把软件复制成几份,然后交给不同的学校,所有学校才能够都有软件。

问题B:添加几条边,能使得这个图变成强连通图。

思路:

找出所有的强连通分量,然后缩点,变成一个新有向无环图,求每个强连通分量的入度和出度。

A:入度是0的点就是复制的最小数量,因为只要给强连通分量一个软件,他就会传到每个点,所以找出没有入口的强连通分量的数量就是要复制的数量(其他强连通分量都可以由这些分量传递过去)。

B:in0为入度为0点的数量,out0出度为0的点的数量,添加的边的数量是=max(in0,out0);这个不好证明,但画一下图,自己理解一下,也是很容易理解的。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <stack>
#define MA 10000+5
using namespace std;

vector<int>g[MA];
int dfn[MA],low[MA];
int sccno[MA],scc_cnt;
int n,m;
int dfs_clock;
stack<int>s;
int in[105],out[105];
void getmap()
{
    int ans=0;int q;
    for(int i=0;i<n;i++)
    {
    while(scanf("%d",&q)&&q)
    {
        q--;
        g[i].push_back(q);

    }
    }
}

int dfs(int x)
{
    int v;
    low[x]=dfn[x]=++dfs_clock;
    s.push(x);
    for(int i=0;i<g[x].size();i++)
    {
        v=g[x][i];
        if(!dfn[v])
        {
            dfs(v);
            low[x]=min(low[x],low[v]);
        }
        else if(!sccno[v])
        {
            low[x]=min(low[x],low[v]);
        }
    }
    if(dfn[x]==low[x])
    {
        scc_cnt++;
        for(;;)
        {
            v=s.top();
            s.pop();
            sccno[v]=scc_cnt;
            if(v==x)break;
        }
    }

}

int find_cut(int l,int r)
{
    memset(low,0,sizeof(low));
    memset(dfn,0,sizeof(dfn));
    memset(sccno,0,sizeof(sccno));
    scc_cnt=dfs_clock=0;

    for(int i=l;i<r;i++)
    {
        if(!dfn[i]){dfs(i);}

    }


}
int main()
{
    while(~scanf("%d",&n))
    {
        for(int i=0;i<n;i++)g[i].clear();
        getmap();
        find_cut(0,n);
        for(int i=1;i<=scc_cnt;i++)
        in[i]=out[i]=1;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<g[i].size();j++)
            {
                int v=g[i][j];
                if(sccno[v]!=sccno[i])
                {
                    out[sccno[i]]=in[sccno[v]]=0;
                }
            }
        }

        int sum1=0,sum2=0;
        for(int i=1;i<=scc_cnt;i++)
        {
            if(in[i])sum1++;
            if(out[i])sum2++;
        }
        if(scc_cnt==1)printf("1\n0\n");
        else printf("%d\n%d\n",sum1,max(sum1,sum2));
    }
    return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值