POJ 1236 Network of Schools ( Tarjan + 缩点 )

Network of Schools
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 8632 Accepted: 3408

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(2<=n<=100) 所学校,每个学校都有个 list,列出了这个学校提供软件的学校名单。

现在问:

        A、为了使所有的学校享用到一个软件,最少有多少学校要 copy 一个软件

        B、为了使传到任意学校的软件能够传到其他所有的学校,至少要添加多少个学校到相应学校的 list 中去

 

做法分析

 

这种题,显然要先给图“化简”,是其成为一个 DAG 图

对于 task A,我们只需统计“化简”后的 DAG 中入度为 0 的点的个数

对于 task B,其实就是求添加多少条边使得 DAG 之间的所有点能够相互到达(形成一个 SCC),有一个结论:答案是入度为 0 的点的个数和出度为 0 的点个数的最大者

 

 

学习Tarjan算法不妨到这位大牛Blog去,讲的倾好的
http://www.byvoid.com/blog/scc-tarjan/

这个也很不错:http://comzyh.tk/blog/archives/517
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>

using namespace std;

const int VM=110;
const int INF=0x3f3f3f3f;

struct Edge{
    int to,nxt;
}edge[VM*VM];

int n,m,cnt,head[VM],dep,top,atype;
int dfn[VM],low[VM],vis[VM],indeg[VM],outdeg[VM],belong[VM];
int stack[VM];

void addedge(int cu,int cv){
    edge[cnt].to=cv;    edge[cnt].nxt=head[cu];     head[cu]=cnt++;
}

void Tarjan(int u){
    dfn[u]=low[u]=++dep;    // dep是时间戳 
    stack[top++]=u;     //入栈 
    vis[u]=1;       //标记入栈
    for(int i=head[u];i!=-1;i=edge[i].nxt){     //枚举所有相连边 
        int v=edge[i].to;   //临时变量 
        if(!dfn[v]){        // v没有被搜索过 
            Tarjan(v);      // 递归搜索v
            low[u]=min(low[u],low[v]);  //回溯中若发现v找到了更老的时间戳,则更新能达到老时间戳 ,否则不变
        }else if(vis[v])        //如果已经印有时间戳 且 时间戳比较小,则有环
            low[u]=min(low[u],dfn[v]);  //当前记录可追溯时间戳更新 
    }
    int j;
    if(dfn[u]==low[u]){     //可追溯最老是自己,表明自己是当前强连通分量的栈底 
        atype++;    //强连通分量数增加
        do{
            j=stack[--top];     //出栈顶元素
            belong[j]=atype;    //记录j所在的强连通分量编号 
            vis[j]=0;           //标记出栈
        }while(u!=j);           //如果是当前元素,弹栈完成 
    }
}

void init(){
    cnt=0,  atype=0,    dep=0,  top=0;
    memset(head,-1,sizeof(head));
    memset(vis,0,sizeof(vis));
    memset(low,0,sizeof(low));
    memset(dfn,0,sizeof(dfn));
    memset(indeg,0,sizeof(indeg));
    memset(outdeg,0,sizeof(outdeg));
    memset(belong,0,sizeof(belong));
}

int main(){

    //freopen("input.txt","r",stdin);

    while(~scanf("%d",&n)){
        init();
        int ans1=0,ans2=0;
        int i,j;
        for(i=1;i<=n;i++){
            while(~scanf("%d",&m) && m)
                addedge(i,m);
        }
        for(i=1;i<=n;i++)
            if(!dfn[i])
                Tarjan(i);
        for(i=1;i<=n;i++){
            for(j=head[i];j!=-1;j=edge[j].nxt){
                int v=edge[j].to;
                if(belong[i]!=belong[v]){
                    outdeg[belong[i]]++;
                    indeg[belong[v]]++;
                }
            }
        }
        for(i=1;i<=atype;i++){
            if(indeg[i]==0)
                ans1++;
            if(outdeg[i]==0)
                ans2++;
        }
        if(atype==1)
            printf("1\n0\n");
        else
            printf("%d\n%d\n",ans1,max(ans1,ans2));
    }
    return 0;
}
 
 

 

 

转载于:https://www.cnblogs.com/jackge/archive/2013/04/13/3017687.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值