Network of Schools(tarjan求有向图的强连通分量+缩点 入门)

Network of Schools

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 25423   Accepted: 10080

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

给一个有向图,N个点,求:

1)至少要选几个顶点,才能做到从这些顶点出发,可以到达全部顶点

2)至少要加多少条边,才能使得从任何一个顶点出发,都能到达全部顶点

复杂度为O(n+m)

什么是SCC,就是强连通分量,有向图中,任意两个顶点能相互到达

Tarjan算法求SCC,并缩点建图。

那么对于问题1,新图中入度为0点的点数即是答案。因为每一个强连通图中选一个点就可以全部答案了,而对于有入度的强连通图只需要上一个连通图中选一个点即可。

对于问题2,答案吗为max(入度为0的点的点数,出度为0的点数)因为对于每个入度或出度为0的点,需要连一条边来解决,那么将出度为0的点连向入度为0的点是最优的。

#include<cstdio>
#include<iostream>
#include<stack>
#include<cstring>
using namespace std;
const int N=1e2+10;
struct edge
{
	int from;
	int to;
	int next;
}e[N*100];
int n,head[N];
int cnt,index,sccnum;
int dfn[N],low[N],in[N],out[N],scc[N]; 
stack<int>que;
void tarjan(int root)//已成功的求出连通分量了 
{
	dfn[root]=low[root]=++index;//更新时间戳 
	que.push(root);//点入栈 
	for(int i=head[root];i;i=e[i].next)//遍历边 
	{
		int v=e[i].to;//这条边的下一个顶点 
		if(!dfn[v])
		{
			tarjan(v);
			low[root]=min(low[v],low[root]);//low只与儿子的low比较 
		}
		else if(!scc[v])low[root]=min(low[root],dfn[v]);//v有可能在上方 
	}
	if(low[root]==dfn[root])
	{
		sccnum++;
		int x;
		do
		{
			x=que.top();
			que.pop();
			scc[x]=sccnum;
		}while(x!=root);
	}
}
void add_edge(int u,int v)
{
	e[++cnt].from=u;
	e[cnt].to=v;//下一个点
	e[cnt].next=head[u];//下一条边 
	head[u]=cnt; 
}
int main()
{
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		int x;
		while(scanf("%d",&x)&&x)
			add_edge(i,x);
	}
	for(int i=1;i<=n;i++)
	if(!dfn[i]) tarjan(i);
	for(int i=1;i<=n;i++)
	{
		for(int j=head[i];j;j=e[j].next)
		{
			if(j==0) break;
			int v=e[j].to;
			if(scc[i]!=scc[v])
			{
				in[scc[v]]++;
				out[scc[i]]++;
			}
		}
	}
	int ans1=0,ans2=0;
	for(int i=1;i<=sccnum;i++)
	{
		if(in[i]==0) 	ans1++;
		if(out[i]==0) 	ans2++;
	}
	if(sccnum==1)
		printf("1\n0\n");
	else
		printf("%d\n%d\n",ans1,max(ans1,ans2));
}

猜你喜欢

转载自blog.csdn.net/qq_41286356/article/details/89366703