poj 1236 Network of Schools(强连通分量)

本文介绍了一种解决多个学校间软件分发问题的算法,通过构建邻接表并应用Tarjan算法来确定最小接收者集合。算法分为两部分:一部分计算至少需要多少个学校作为初始接收者;另一部分则确保通过增加最少数量的接收者,能够确保无论选择哪个学校发送软件,都能达到所有学校的接收。实现了两个任务,输出了相关计算结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Network of Schools
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 13328 Accepted: 5330

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


分析:这个博客讲的很好:点击打开链接
代码:
#include<iostream>2
#include<stdio.h>
#include<queue>
#include<string.h>
#include<algorithm>
using namespace std;
const int MAXN=110;
const int INF=0x3f3f3f3f;
int n,map[MAXN][MAXN],low[MAXN],dfn[MAXN],stack[MAXN],head;
int instack[MAXN],belong[MAXN],in[MAXN],out[MAXN],index,cnt;

void init()
{
    memset(map,0,sizeof(map));
    memset(dfn,-1,sizeof(dfn));
    memset(low,0,sizeof(low));
    memset(instack,0,sizeof(instack));
    index=cnt=1;
    head=0;
    int temp;
    for(int i=1;i<=n;i++)
        while(scanf("%d",&temp)&&temp)
            map[i][temp]=1;
}
void tarjan(int x)
{
    low[x]=dfn[x]=index;
    index++;
    stack[++head]=x;
    instack[x]=1;
    for(int i=1;i<=n;i++){
        if(!map[x][i])continue;
        if(dfn[i]==-1){
            tarjan(i);
            low[x]=min(low[x],low[i]);
        }
        else if(instack[i])
            low[x]=min(low[x],dfn[i]);
    }
    if(low[x]==dfn[x]){
        while(1){
            int temp=stack[head--];
            belong[temp]=cnt;
            instack[temp]=0;
            if(temp==x)break;
        }
        cnt++;
    }
}
void solve()
{
    while(scanf("%d",&n)!=EOF){
        init();
        for(int i=1;i<=n;i++)
            if(dfn[i]==-1)
            tarjan(i);
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
        if(map[i][j]&&belong[i]!=belong[j]){
            out[belong[i]]++;
            in[belong[j]]++;
        }
        int t1=0,t2=0;
        for(int i=1;i<cnt;i++){
            if(in[i]==0)t1++;
            if(out[i]==0)t2++;
        }
        if(cnt==2)
            printf("1\n0\n");
        else
            printf("%d\n%d\n",t1,max(t1,t2));
    }
}
int main()
{
    solve();
    return 0;
}

 用邻接表存储数据
#include<iostream>
#include<stdio.h>
#include<queue>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxn=110;
const int INF=0x3f3f3f3f;
const int maxe=110*110;
struct edge
{
    int x,y,next;
}e[maxe];
int g[maxn],dfn[maxn],low[maxn],v[maxn],f[maxn],s[maxn],b[maxn],h[maxn];
int n,tot=0,cnt=0,ans=0,times=0,t=0,ans2;
void ins(int x,int y)
{
    e[++tot].x=x;e[tot].y=y;
    e[tot].next=h[x];h[x]=tot;
}
void tarjan(int x)
{
    int y,i;
    times++;
    t++;
    dfn[x]=low[x]=times;
    v[x]=1;s[t]=x;
    for(int i=h[x];i;i=e[i].next){
        y=e[i].y;
        if(v[y]==0){
            tarjan(y);
            low[x]=min(low[x],low[y]);
        }
        if(v[y]==1){
            low[x]=min(low[x],dfn[y]);
        }
    }
        if(dfn[x]==low[x]){
            cnt++;
            do{
                y=s[t--];
                b[y]=cnt;
                v[y]=2;
            }while(y!=x);
        }
}
int main()
{
    cin>>n;int j;
    for(int i=1;i<=n;i++){
        cin>>j;
        while(j){
            ins(i,j);
            cin>>j;
        }
    }
    for(int i=1;i<=n;i++)
        if(v[i]==0)
        tarjan(i);
    if(cnt==1){
        cout<<1<<endl<<0<<endl;
        return 0;
    }
    for(int i=1;i<=tot;i++)
    if(b[e[i].x]!=b[e[i].y]){
        f[b[e[i].x]]++;
        g[b[e[i].y]]++;
    }
    ans=0;
    for(int i=1;i<=cnt;i++)
        if(g[i]==0)ans++;
    cout<<ans<<endl;
    ans2=0;
    for(int i=1;i<=cnt;i++)
        if(f[i]==0)ans2++;
    cout<<max(ans,ans2)<<endl;
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值