POJ 题目1236 Network of Schools(强联通)

Network of Schools
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 12015   Accepted: 4783

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

wa了好多次,当有一个联通点时输出设备1 0
题目大意:有n个学校,学校之间可以传递信息,例如学校 a 可以 传达信息给 b , 即a ——> b , 但 b 不一定 能传递信息给 a 。 告诉你每个学校能够向哪些学校传递信息,然后有两个问题:
    问题一:至少要向几个学校传递 原始 信息,才能保证所有学校都能收到信息。
    问题二:至少要添加多少组关系(每组关系类型如右:a 可以 向 b 传递信息),才能保证 给任意一个学校原始信息后,其他所有学校都能收到信息。
ac代码
#include<stdio.h>
#include<string.h>
#include<vector>
#include<iostream>
using namespace std;
#define min(a,b) (a>b?b:a)
#define max(a,b) (a>b?a:b)
/*struct s
{
	int u,v,next;
}edge[110*110*2*100];*/
int low[110],dfn[110],ins[110],belong[110],head[110],stack[110],in[110],out[110];
int cnt,taj,top,time;
vector<int>vt[110];
void init()
{
	memset(dfn,-1,sizeof(dfn));
	memset(ins,0,sizeof(ins));
	memset(belong,-1,sizeof(dfn));
	memset(low,-1,sizeof(low));
	memset(stack,0,sizeof(stack));
	memset(head,-1,sizeof(head));
	memset(out,0,sizeof(out));
	memset(in,0,sizeof(in));
	cnt=top=time=taj=0;
}
/*void add(int u,int v)
{
	edge[cnt].u=u;
	edge[cnt].v=v;
	edge[cnt].next=head[u];
	head[u]=cnt++;
}*/
void tarjan(int u)
{
	dfn[u]=low[u]=time++;
	ins[u]=1;
	stack[top++]=u;
	for(int i=0;i<vt[u].size();i++)
	{
		int v=vt[u][i];
		if(dfn[v]==-1)
		{
			tarjan(v);
			low[u]=min(low[v],low[u]);
		}
		else
			if(ins[v])
				low[u]=min(dfn[v],low[u]);
	}
	if(low[u]==dfn[u])
	{
		int now;
		taj++;
		do
		{
			now=stack[--top];
			ins[now]=0;
			belong[now]=taj;
		}while(now!=u);
	}
}
int main()
{
	int n;
	while(scanf("%d",&n)!=EOF)
	{
		int i;
		init();
		for(i=0;i<=n;i++)
			vt[i].clear();
		for(i=1;i<=n;i++)
		{
			int v;
			while(scanf("%d",&v)!=EOF,v)
			{
				//add(i,v);
				vt[i].push_back(v);
			}
		}
		for(i=1;i<=n;i++)
		{
			if(dfn[i]==-1)
				tarjan(i);
		}
		if(taj==1)
		{
			printf("1\n0\n");
			continue;
		}
		for(i=1;i<=n;i++)
		{
			//int u=edge[i].u;
			//int v=edge[i].v;
		//	printf("%d %d\n",u,belong[u]);
		//	printf("%d %d\n",v,belong[v]);
			int u=i;
			for(int j=0;j<vt[u].size();j++)
			{
				int v=vt[u][j];
				if(belong[u]!=belong[v])
				{
					in[belong[v]]++;
					out[belong[u]]++;
				}
			}
		}
		int ans1=0,ans2=0;
		for(i=1;i<=taj;i++)
		{
			if(in[i]==0)
				ans1++;
			if(out[i]==0)
				ans2++;
		}
		printf("%d\n",ans1);
		printf("%d\n",max(ans1,ans2));
	}
}

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。