POJ 1236 Network of Schools(强连通分量缩点+tarjan算法)

本文探讨了一种算法,旨在解决网络中多个学校间软件分发的问题,包括确定最少的初始分发点以及所需的额外连接数量,以确保软件能够到达网络中的所有学校。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Network of Schools
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 16197 Accepted: 6434

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

IOI 1996

问题:

每一个学校都可以把他的软件发给与他签约的学校,求出至少给多少个学校软件才能使所有的学校都有软件,还有一个问题是要再加多少条边才能使得给任意一个学校软件所有的学校都能拥有软件

 

首先我们当然是用tarjan来把图转化成DAG啊,然后DAG有一条十分重要的性质

DAG性质:对于一个有向无环图,若想让它成为强连通图,至少需要添加max(a,b) a为入度为0的边点的数量,b为出度为0的点的数量;

那稍加分析我们就知道对于第一个问题我们求得是DAG上有多少出度为零的点,第二个问题则是要求出DAG上出度为0的点的数量和入度为零的数量的最大值,我们用tarjan+邻接表来实现

还有一点要特别注意的!!!!有一种特殊情况需要特判,就是只有一个强联通分量的情况,以为我们利用tarjan把图转化成了DAG,应该是无环图,但是别忘了无环图不代表这个就不会出现强联通的情况,如果这有一个强联通分量,那么这个图本身就是强联通的就不需要在加边了

#include <iostream>
#include <cstdio>
#include <cstring>
#include <stack>
using namespace std;
const int maxn=100+10;
const int maxm=10000+10;
int n,first[maxn],next[maxn],uu[maxm],vv[maxm],scc,index,tot,num[maxn],belg[maxn],in[maxn],out[maxn],low[maxn],dfn[maxn],vis[maxn];
stack <int> st;
int min(int a,int b)
{
	if(a<b) return a;
	else return b;
}
int max(int a,int b)
{
	if(a>b) return a;
	else return b;
}
void tarbfs(int u)
{
	//cout<<"3"<<endl;
	index++;
	low[u]=dfn[u]=index;
	st.push(u);
	vis[u]=1;
	int k=first[u];
	//cout<<k<<endl;
	while(k!=-1)
	{
		//cout<<k<<endl;
		int v=vv[k];
		if(!dfn[v])
		{
			tarbfs(v);
			low[u]=min(low[u],low[v]);
		}
		else if(vis[v]) low[u]=min(low[u],dfn[v]);
		k=next[k];
	}
	if(low[u]==dfn[u])
	{
		//cout<<"4"<<endl;
		int v;
		scc++;
		do
		{
			v=st.top();
			st.pop();
			vis[v]=0;
			num[scc]++;
			belg[v]=scc;
		}while(u!=v);
	}
}
void tarjan()
{
	for(int i=1;i<=n;i++)
		if(!dfn[i]) tarbfs(i);
}
void addEdge(int u,int v)
{
	tot++;
	next[tot]=first[u];
	first[u]=tot;
	uu[tot]=u;
	vv[tot]=v;
}
void init()
{
	memset(first,-1,sizeof(first));
	memset(num,0,sizeof(num));
	memset(belg,0,sizeof(belg));
	memset(dfn,0,sizeof(dfn));
	memset(low,0,sizeof(low));
	memset(in,0,sizeof(in));
	memset(out,0,sizeof(out));
	memset(vis,0,sizeof(vis));
	while(!st.empty()) st.pop();
	tot=scc=index=0;	
}
int main()
{
	freopen("input.txt","r",stdin);
	while(scanf("%d",&n)!=EOF)
	{
		init();
		for(int i=1;i<=n;i++)
		{
			int u=i,v;
			while(scanf("%d",&v))
			{
				if(v==0) break;
				addEdge(u,v);
			}
		}
		//cout<<"1"<<endl;
		tarjan();
		//cout<<scc<<endl;
		if(scc==1)
		{
			printf("1\n");
			printf("0\n");
			break;
		}
		for(int i=1;i<=n;i++)
		{
			int k=first[i];
			while(k!=-1)
			{
				int u=i,v=vv[k];
				if(belg[u]!=belg[v])
				{
					out[belg[u]]++;
					in[belg[v]]++;
				}
				k=next[k];
			}
		}
		int incnt=0,outcnt=0;
		for(int i=1;i<=scc;i++)
		{
			if(!out[i]) outcnt++;
			if(!in[i]) incnt++;
		}
		printf("%d\n",incnt);
		printf("%d\n",max(incnt,outcnt));
	}
	return 0;
}


 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值