poj 1236 强连通分量缩点求入度为0出度为0的分量个数 kosaraju算法

           
B - Network of Schools
Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u

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:至少需要添加几条边,使任意向一个学校发放软件后,经过若干次传送,网络内所有的学校最终都能得到软件。
解题思路:
首先找连通分量,然后看连通分量的入度为0点的总数,出度为0点的总数,那么第一问要向多少学校发放软件,就是入度为零的个数,这样才能保证所有点能够找到

然后第二问添加多少条边可以得到使整个图达到一个强连通分量,答案是入度为0的个数和出度为0的个数中最大的
那个,为什么会这样呢,经过我同学的讨论,将这个图的所有子树找出来,然后将一棵子树的叶子结点(出度为0)连到另外一棵子树的根结点上(入度为0),这样将所有的叶子结点和根节点全部消掉之后,就可以得到一整个强连通分量,看最少多少条边,这样就是看叶子结点和根节点哪个多,即出度为0和入度为0哪个多

ACcode:

#include <iostream>
#include<cstdio>
#include<string.h>
#define Maxn 110
using namespace std;
int map[Maxn][Maxn],order[Maxn],belong[Maxn],indegree[Maxn],outdegree[Maxn];
int n,num,count;
bool vis[Maxn];
void dfs(int x)
{
    int i;
    vis[x]=1;
    for(i=1;i<=n;i++)
        if(map[x][i]&&!vis[i])
        dfs(i);
    order[++num]=x;
}
void dfst(int x)
{
    int i;
    belong[x]=count;//记录结点属于哪个连通分量
    vis[x]=1;
    for(i=1;i<=n;i++)
        if(map[i][x]&&!vis[i])
        dfst(i);
}
void kosaraju()
{
    int i;
    memset(vis,0,sizeof(vis));
    num=count=0;
    for(i=1;i<=n;i++)
    if(!vis[i])
    dfs(i);
        memset(vis,0,sizeof(vis));
    for(i=n;i>=1;i--)
        if(!vis[order[i]])
    {
        count++;//连通分量个数
        dfst(order[i]);
    }
}
void output(){
	int i,j,inzero=0,outzero=0;
	for(i=1;i<=n;i++){
		indegree[i]=outdegree[i]=0;
	}
	for(i=1;i<=n;i++)				//找连通分量入度与出度
		for(j=1;j<=n;j++)
			if(map[i][j] && belong[i]!=belong[j]){
				indegree[belong[j]]++;
				outdegree[belong[i]]++;
			}
	for(i=1;i<=count;i++){			//找入度与出度为0的点
		if(!indegree[i]) inzero++;
		if(!outdegree[i]) outzero++;
	}

	if(count==1)					//只有1个结点要特判
		printf("1\n0\n");
	else
		printf("%d\n%d\n",inzero,max(inzero,outzero));
}
int main()
{
    int i,a;
    while(~scanf("%d",&n))
    {
        for(i=1;i<=n;i++)
        {
            while(scanf("%d",&a)&&a)
                map[i][a]=1;
        }
        kosaraju();
        output();
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值