POJ 1236 Network of Schools(强连通分量)

题意:题意杀...网络中的一学校可以将软件发送给其他一些学校,能够发送给谁取决于他们各自维护的一个清单。将学校看成一个节点,给出每个学校的维护清单,问至少需要复制几次软件,使毎个学校都能够得到该软件。然后问在清单中至少添加几项,可使软件只要复制一次,所有学校都可以得到

思路:和HDU2767一样,最少加多少条边使得图强连通

        1.    求出该图的所有强连通分量.

        2.    将每个分量缩点构新DAG图.

        3.    第一问就是新图中入度为0点的个数,第二问是max(入度0点数,出度0点数)

注意:当原本就是强连通的时候要特判


#include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <set>
#include <ctime>
#include <cmath>
#include <cctype>
#include <stack>
using namespace std;
#define maxn 100+10
#define LL long long
int cas=1,T;
vector<int>G[maxn];
int pre[maxn];
int lowlink[maxn];
int sccno[maxn];
int dfs_clock,scc_cnt;
int n,m;
stack<int>S;
void dfs(int u)
{
	pre[u]=lowlink[u]=++dfs_clock;
	S.push(u);
	for (int i = 0;i<G[u].size();i++)
	{
		int v = G[u][i];
		if (!pre[v])
		{
			dfs(v);
			lowlink[u] = min(lowlink[u],lowlink[v]);
		}
		else if (!sccno[v])
		{
			lowlink[u] = min (lowlink[u],pre[v]);
		}
	}
	if (lowlink[u] == pre[u])
	{
		scc_cnt++;
		for (;;)
		{
			int x = S.top();S.pop();
			sccno[x] = scc_cnt;
			if (x==u)
				break;
		}
	}
}

void find_scc(int n)
{
	dfs_clock=scc_cnt=0;
	memset(sccno,0,sizeof(sccno));
	memset(pre,0,sizeof(pre));
	for (int i = 1;i<=n;i++)
		if (!pre[i])
			dfs(i);
}
int in[maxn];
int out[maxn];
int main()
{
	//freopen("in","r",stdin);
	while (scanf("%d",&n)!=EOF && n)
	{	
		memset(in,0,sizeof(in));
		memset(out,0,sizeof(out));
		for (int i = 0;i<=n;i++)
			G[i].clear();
		for (int u=1;u<=n;u++)
		{
			int v;
			while (scanf("%d",&v) && v)
			{
				G[u].push_back(v);
			}
		}
		find_scc(n);
		for (int i = 1;i<=scc_cnt;i++)
		{
            in[i]=out[i]=1;
		}
		for (int u = 1;u<=n;u++)
			for (int i =0;i<G[u].size();i++)
			{
				int v = G[u][i];
				if (sccno[u] != sccno[v])
					in[sccno[v]]=out[sccno[u]]=0;
			}
		int a=0,b=0;
		for (int i = 1;i<=scc_cnt;i++)
		{
			if (in[i])
				a++;
			if (out[i])
				b++;
		}
		if (scc_cnt !=1)
		    printf("%d\n%d\n",a,max(a,b));
		else
			printf("1\n0\n");

	}
	return 0;
}


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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值