poj 1236 Network of Schools (最小点基)

Network of Schools
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 20658 Accepted: 8149

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座学校,学校与学校之间可以传输文件,但传输是单向的,问你现在要有一份文件传给所有的学校,问你最少需要多少给几个学校备份,就可以将文件传给全部的学校
并且为了优化传输,若想使备份给任意一个学校(即只用把备份给一个学校),全部学校都能接收到文件,求至少需要增加几条传输线路

解析:
经典的最小点基问题和有向无环图变成强连通图问题
求图G的强连通分量,入度为0的强连通分量个数即为最小点基,从每个入度为0的强连通分量中取出权值最小的点,构成的集合即最小权点基。

#include <cstdio>
#include <stack>
#include <cstring>
using namespace std;
const int MAXN = 1e4+10;

typedef struct node
{
    int u;
    int v;
    int next;
}node;

node edge[MAXN];
int head[MAXN],cnt;
int DFN[MAXN],LOW[MAXN],vis[MAXN];
int n,index;
int comnum;
int com[MAXN];
stack<int> ms;

void addedge(int u,int v)
{
    edge[cnt].u=u;
    edge[cnt].v=v;
    edge[cnt].next=head[u];
    head[u]=cnt++;
}

void tarjan(int x)
{
    DFN[x]=LOW[x]=++index;
    ms.push(x);
    vis[x]=1;
    for(int i=head[x];i!=-1;i=edge[i].next)
    {
        int v=edge[i].v;
        if(!DFN[v])
        {
            tarjan(v);
            LOW[x]=min(LOW[x],LOW[v]);
        }
        else if(vis[v])
        {
            LOW[x]=min(LOW[x],DFN[v]);
        }
    }

    if(DFN[x]==LOW[x])
    {
        int u;
        comnum++;
        do
        {
            u=ms.top();
            ms.pop();
            com[u]=comnum;
            vis[u]=0;
        }while(u!=x);
    }
}

void Tarjansolve()
{
    memset(DFN,0,sizeof(DFN));
    memset(LOW,0,sizeof(LOW));
    memset(vis,0,sizeof(vis));
    index=comnum=0;
    for(int i=1;i<=n;i++)
    {
        if(!DFN[i]) tarjan(i);
    }
}

int indeg[MAXN];
int outdeg[MAXN];
int main()
{
    scanf("%d",&n);
    cnt=0;
    memset(head,-1,sizeof(head));
    for(int i=1;i<=n;i++)
    {
        int tmp;
        while(scanf("%d",&tmp),tmp)
        {
            addedge(i,tmp);
        }
    }

    Tarjansolve();

    memset(indeg,0,sizeof(indeg));
    memset(outdeg,0,sizeof(outdeg));
    for(int i=1;i<=n;i++)
    {
        for(int j=head[i];j!=-1;j=edge[j].next)
        {
            int tt=com[edge[j].v];
            if(tt!=com[i])
            {
                indeg[tt]++;
                outdeg[com[i]]++;
            }
        }
    }
    int in,out;
    in=out=0;
    for(int i=1;i<=comnum;i++)
    {
        if(!indeg[i]) in++;
        if(!outdeg[i]) out++;
    }

    printf("%d\n",in);
    if(comnum==1) printf("0\n");
    else printf("%d\n",max(in,out));
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值