POJ 1236 tarjan 算法求强连通分量

http://poj.org/problem?id=1236

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
题目大意:给出N台电脑,电脑间单向连通传送文件
问题1.网络中最少放几个文件保证所有电脑都能接受到文件
问题2.最少向网络中加几条线保证任意放1个文件所有电脑都能接受到
解题思路:
1.求出强连通分量
2.缩点 ,进行重新构图
3.如果入度为0,说明没有文件传输到该网络。解决问题1,统计入度为0的个数即可
4.一个强连通图的入度和出度都不为0。入度为0相当于根root,出度为0相当于叶子leave.max(root, leave)即为答案。
就是需要加的线数,保证整个网络强连通。
若连通分量个数为0,那么答案为0

#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
#define M 10005
#define N 105
struct edge//链式前向星存储图
{
    int v,next;
}edge[M];
int head[N],stack[N],ins[N],dfn[N],out[N],in[N];
int low[N],belong[N],index,cnt_edge,n,m,cnt_tar,top;
int ee[M][2];
void add_edge(int u,int v)//新增一条边的操作
{
    edge[cnt_edge].v=v;
    edge[cnt_edge].next=head[u];
    head[u]=cnt_edge++;
}
void tarjan(int u)//求强连通量的Tarjan算法(模板)
{
    int j,i,v;
    dfn[u]=low[u]=++index;//为节点u设定次序编号和low初值
    stack[++top]=u;//将节点u压入栈中
    ins[u]=1;
    for(i=head[u];i!=-1;i=edge[i].next)//枚举每一条边
    {
        v=edge[i].v;
        if(!dfn[v])//如果节点v未被访问过
        {
            tarjan(v);//继续向下找
            low[u]=min(low[u],low[v]);
        }
        else if(ins[v])//如果节点还在栈内
            low[u]=min(low[u],dfn[v]);
    }
    if(dfn[u]==low[u])//如果节点u是强连通分量的根
    {
        cnt_tar++;//计数加一
        do
        {
            j=stack[top--];//将v退栈
            ins[j]=0;//不再栈内了
            belong[j]=cnt_tar;//该强连通分量的最后一个出栈的点作为一个缩点出现在新的待建的图中作为该强连通分量的代表
        }while(j!=u);
    }
}
void solve()
{
    int i;
    top=0,index=0,cnt_tar=0;
    memset(dfn,0,sizeof(dfn));
    memset(low,0,sizeof(low));
    for(i=1;i<=n;i++)//求强连通分量时遍历每一个点
    {
        if(!dfn[i])
           tarjan(i);
    }
}
int main()
{
    int i,u,v;
    while(~scanf("%d",&n))
    {
        cnt_edge=0,m=1;
        memset(head,-1,sizeof(head));//head数组置-1
        memset(in,0,sizeof(in));//统计入度为零的强连通分量的个数
        memset(out,0,sizeof(out));//统计初度为零的点的强连通分量的个数
        for(i=1;i<=n;i++)
        {
            while(scanf("%d",&v))
            {
                if(v==0)
                break;
                ee[m][0]=i,ee[m++][1]=v;//前者为一条路的起点,后者为终点
                add_edge(i,v);
            }
        }
        solve();//求强连通分量
        for(i=1;i<=m;i++)
        {
            int xx=belong[ee[i][0]],yy=belong[ee[i][1]];
            if(xx!=yy)
            {
                in[yy]++;//二者不相等则路的终点多对应的点的入度肯定不为零
                out[xx]++;//同上,出度不为零
            }
        }
        int innum=0,outnum=0;
        for(i=1;i<=cnt_tar;i++)
        {
            if(in[i]==0)//统计出度
               innum++;
            if(out[i]==0)//统计入度
               outnum++;
        }
        if(cnt_tar==1)
           printf("1\n0\n");
        else
           printf("%d\n%d\n",innum,max(innum,outnum));
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值