POJ 1236 Network of Schools【强连通缩点】【Tarjan算法】

55 篇文章 1 订阅
12 篇文章 0 订阅

Network of Schools
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 18759 Accepted: 7387

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


原题链接:http://poj.org/problem?id=1236

题意:

 一些学校连成了网络, 在学校之间存在某个协议:每个学校都维护一张传送表,表明他们要负责将收到的软件传送到表中的所有学校。如果A在B的表中,那么B不一定在A的表中。

    现在的任务就是,给出所有学校及他们维护的表,问1、如果所有学校都要被传送到,那么需要几份软件备份;2、如果只用一份软件备份,那么需要添加几条边?


PS:第二道Tarjan,并且还用了缩点,搞了一下午,注意,此题中Tarjan用到的栈要定义成全局变量.....

参考博客:

http://blog.csdn.net/jiangzh7/article/details/8639709
http://www.cnblogs.com/ACMERY/p/4681803.html
http://blog.csdn.net/wangjian8006/article/details/7888558
http://blog.csdn.net/huzhengnan/article/details/7787595[不错]
http://www.cnblogs.com/kuangbin/archive/2011/08/07/2130277.html


AC代码:

/**
  * 行有余力,则来刷题!
  * 博客链接:http://blog.csdn.net/hurmishine
  * 个人博客网站:http://wuyunfeng.cn/
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <stack>
#include <vector>
using namespace std;
const int maxn=1000+5;
bool a[maxn][maxn];
int low[maxn],dfn[maxn];
int belong[maxn];
int in[maxn],out[maxn];
bool vis[maxn];
int index,cnt;
int n;
stack<int>s;
void Init()
{
    for(int i=0;i<=n;i++)
    {
        dfn[i]=low[i]=vis[i]=0;
    }
    memset(a,false,sizeof(a));
    index=cnt=0;
}
void Tarjan(int u)
{
    low[u]=dfn[u]=++index;
    s.push(u);
    vis[u]=true;
    for(int v=1;v<=n;v++)
    {
        if(!a[u][v])
            continue;
        if(!dfn[v])
        {
            Tarjan(v);
            low[u]=min(low[u],low[v]);
        }
        else if(vis[v])
        {
            low[u]=min(low[u],dfn[v]);
        }
    }
    if(low[u]==dfn[u])
    {
        int x;
        cnt++;
        do
        {
            x=s.top();
            s.pop();
            belong[x]=cnt;
            vis[x]=false;
        }while(x!=u);
    }
}
int main()
{
    //freopen("C:\\Documents and Settings\\Administrator\\桌面\\data.txt","r",stdin);
    while(cin>>n)
    {
        Init();
        int x;
        for(int i=1;i<=n;i++)
        {
            while(scanf("%d",&x)&& x)
            {
                a[i][x]=true;
            }
        }
        for(int i=1;i<=n;i++)
        {
            if(!dfn[i])
                Tarjan(i);
        }

        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                if(a[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])
                t1++;
            if(!out[i])
                t2++;
        }
        if(cnt==1)
            cout<<"1"<<endl<<"0"<<endl;
        else
            cout<<t1<<endl<<max(t1,t2)<<endl;
    }
    return 0;
}

AC代码2:

/**
  * 行有余力,则来刷题!
  * 博客链接:http://blog.csdn.net/hurmishine
  * 个人博客网站:http://wuyunfeng.cn/
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <stack>
using namespace std;
const int maxn=1000+5;
int low[maxn],dfn[maxn];
bool vis[maxn];
int belong[maxn];
int in[maxn],out[maxn];
vector<int>G[maxn];
stack<int>s;
int n;
int index,cnt;
void Init()
{
    cnt=index=0;
    for(int i=0;i<=n;i++)
    {
        G[i].clear();
        low[i]=dfn[i]=vis[i]=0;
        //belong[i]=in[i]=out[i]=
    }
}
void Tarjan(int u)
{
    low[u]=dfn[u]=++index;
    vis[u]=true;
    s.push(u);
    for(int i=0;i<G[u].size();i++)
    {
        int v=G[u][i];
        if(!dfn[v])
        {
            Tarjan(v);
            low[u]=min(low[u],low[v]);
        }
        else if(vis[v])
        {
            low[u]=min(low[u],dfn[v]);
        }
    }
    if(dfn[u]==low[u])
    {
        cnt++;
        int x;
        do
        {
            x=s.top();
            s.pop();
            vis[x]=false;
            belong[x]=cnt;
        }while(x!=u);
    }
}
int main()
{
    //freopen("C:\\Documents and Settings\\Administrator\\桌面\\data.txt","r",stdin);
    while(cin>>n)
    {
        Init();
        int x;
        for(int i=1;i<=n;i++)
        {
            while(scanf("%d",&x),x)
            {
                G[i].push_back(x);
            }
        }
        for(int i=1;i<=n;i++)
        {
            if(!dfn[i])
            {
                Tarjan(i);
            }
        }
        for(int i=1;i<=n;i++)
        {
            for(int j=0;j<G[i].size();j++)
            {
                int v=G[i][j];
                if(belong[i]!=belong[v])
                {
                    //cout<<belong[i]<<","<<belong[v]<<endl;
                    out[belong[i]]++;
                    in[belong[v]]++;
                }
            }
        }
        int t1=0,t2=0;
        for(int i=1;i<=cnt;i++)
        {
            if(!in[i])
                t1++;
            if(!out[i])
                t2++;
        }
        //cout<<cnt<<endl;
        if(cnt==1)
            cout<<"1"<<endl<<"0"<<endl;
        else
            cout<<t1<<endl<<max(t1,t2)<<endl;
    }
    return 0;
}





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值