poj 1236 Network of Schools【强连通求孤立强连通分支个数&&最少加多少条边使其成为强连通图】

Network of Schools
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 13800   Accepted: 5504

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

题意:向学校发软件,1、问最少向多少个学校发软件就可以间接让所有学校都得到软件 2、问最少添加多少条边就可以使 任意向一所学校发软件 就可以让其余所有学校都收到软件

题解:求出强连通分支后缩点,求出所有入度和出度问0的点的个数insum和outsum,则答案为insum和max(insum,outsum)

输入:第一行是一个n代表接下来有n行,每行输入小于n个数以0结尾,代表这个点与第i行(当前行数)的值i点相连

#include<stdio.h>
#include<string.h>
#include<stack>
#include<vector>
#include<algorithm>
#define MAX 1100
#define MAXM 2001000
#define INF 0x7ffffff
using namespace std;
int low[MAX],dfn[MAX];
int head[MAX],ans;
int instack[MAX];
int dfsclock,scccnt;
int in[MAX],out[MAX];
int sccno[MAX];
stack<int>s;
vector<int>newmap[MAX];
vector<int>scc[MAX];
struct node
{
	int beg,end,next;
}edge[MAXM];
void init()
{
	ans=0;
	memset(head,-1,sizeof(head));
}
void add(int u,int v)
{
	edge[ans].beg=u;
	edge[ans].end=v;
	edge[ans].next=head[u];
	head[u]=ans++;
}
void tarjan(int u)
{
	int v,i;
	s.push(u);
	instack[u]=1;
	low[u]=dfn[u]=++dfsclock;
	for(i=head[u];i!=-1;i=edge[i].next)
	{
		v=edge[i].end;
		if(!dfn[v])
		{
			tarjan(v);
			low[u]=min(low[u],low[v]);
		}
		else if(instack[v])
		    low[u]=min(low[u],dfn[v]);
	}
	if(low[u]==dfn[u])
	{
		scccnt++;
		while(1)
		{
			v=s.top();
			s.pop();
			instack[v]=0;
			sccno[v]=scccnt;
			if(v==u)
			    break;
		}
	}
}
void find(int l,int r)
{
	dfsclock=scccnt=0;
	memset(low,0,sizeof(low));
	memset(dfn,0,sizeof(dfn));
	memset(instack,0,sizeof(instack));
	memset(sccno,0,sizeof(sccno));
	for(int i=l;i<=r;i++)
	{
		if(!dfn[i])
		    tarjan(i);
	}
}
void suodian()
{
	int u,v;
	memset(in,0,sizeof(in));
	memset(out,0,sizeof(out));
	for(int i=0;i<ans;i++)
	{
		u=sccno[edge[i].beg];
		v=sccno[edge[i].end];
		if(v!=u)
		{
			newmap[u].push_back(v);
			in[v]++;
			out[u]++;
		}
	}
}
void solve()
{
	int i,j;
	int sum,insum,outsum;
	insum=outsum=0;
	if(scccnt==1)
	{
		printf("1\n0\n");
		return ;
	}
	for(i=1;i<=scccnt;i++)
	{
		if(!in[i])
		    insum++;
		if(!out[i])
		    outsum++;
	}
	sum=max(insum,outsum);
	printf("%d\n%d\n",insum,sum);
}
int main()
{
	int n,m,j,i,a;
	while(scanf("%d",&n)!=EOF)
	{
		init();
		for(i=1;i<=n;i++)
		{
			while(scanf("%d",&a),a)
				add(i,a);
		}
		find(1,n);
		suodian();
		solve();
	}
	return 0;
} 

  

posted @ 2015-10-23 13:22  非我非非我  阅读(289)  评论(0编辑  收藏  举报