poj Network of Schools 1236 (强连通分量+缩点) (问添加几个点最少点是所有点连接+添加最少边使图强连通) 好题

99 篇文章 0 订阅
8 篇文章 0 订阅
Network of Schools
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 13761 Accepted: 5491

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 Outpt

1

2

//题意:
//给你一个N个点的有向图,问1,最少连接几个点可以直接或间接 连接到所有点。
//						   2,最少增加几条边使图强连通。
//思路SCC + 缩点后。统计出入度为0的SCC数sumin和出度为0的SCC数sumout。
//答案一就是sumin,答案二就是max(sumin, sumout)。注意只有一个SCC时,输出1 0。 
#include<stdio.h>
#include<string.h>
#include<stack>
#include<algorithm>
using namespace std;
struct zz
{
	int from;
	int to;
	int next;
}edge[10010];
int head[110],edgenum;
int low[110];
int dfn[110],dfs_clock;
bool instack[110];
int sccon[110],scc_cnt;
int in[110];
int out[110];
int n;
void add(int u,int v)
{
	zz E={u,v,head[u]};
	edge[edgenum]=E;
	head[u]=edgenum++;
}
stack<int>s;
void tarjan(int u,int fa)
{
	dfn[u]=low[u]=++dfs_clock;
	s.push(u);
	instack[u]=true;
	for(int i=head[u];i!=-1;i=edge[i].next)
	{
		int v=edge[i].to;
		if(!dfn[v])
		{
			tarjan(v,u);
			low[u]=min(low[u],low[v]);
		}
		else if(instack[v])
			low[u]=min(low[u],dfn[v]);
	}
	if(low[u]==dfn[u])
	{
		scc_cnt++;
		while(1)
		{
			int v=s.top();
			s.pop();
			instack[v]=false;
			sccon[v]=scc_cnt;
			if(v==u)
				break;
		}
	}
}
void find(int l,int r)
{	
	memset(sccon,0,sizeof(sccon));
	memset(low,0,sizeof(low));
	memset(dfn,0,sizeof(dfn));
	memset(instack,false,sizeof(instack));
	dfs_clock=scc_cnt=0;
	for(int i=l;i<=r;i++)
	{
		if(!dfn[i])
			tarjan(i,-1);
	}
}
void suodian()
{
	int i;
	for(i=1;i<=scc_cnt;i++)
		in[i]=out[i]=0;
	for(i=0;i<edgenum;i++)
	{
		int u=sccon[edge[i].from];
		int v=sccon[edge[i].to];
		if(u!=v)
		{
			in[v]++;
			out[u]++;
		}
	}
}
void solve()
{
	find(1,n);
	suodian();
	if(scc_cnt==1)
	{
		printf("1\n0\n");
		return ;
	}
	int sumin=0,sumout=0;;//记录入度为0的SCC和出度为0的SCC
	for(int i=1;i<=scc_cnt;i++)
	{
		if(in[i]==0)
			sumin++;
		if(out[i]==0)
			sumout++;
	}
	printf("%d\n%d\n",sumin,max(sumin,sumout));
}
int main()
{
	int m,i;
	while(scanf("%d",&n)!=EOF)
	{
		memset(head,-1,sizeof(head));
		edgenum=0;
		for(i=1;i<=n;i++)
		{
			while(scanf("%d",&m),m)
			{
				add(i,m);
			}
		}
		solve();
	}
	return 0;
}


 


 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值