【强连通】POJ 1236 Network of Schools

POJ 1236 Network of Schools 【传送门】

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 26493 Accepted: 10459

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

解题思路:

子任务A:求最少选择多少个结点才能到达所有的网络。即求缩点之后的图的出度为0的个数,因为出度不为0就可以不选这个点。

子任务B:增加多少边才能连通。求入度为0、出度为0的大者,因为任务B要求在任意学校投放软件使得所有学校都能收到,所以很明显是需要整张图形成一个环,而环中所有节点入度和出度都不为0,所以需要把所有入度和出度的点度数增加。然后求出较大者就可以了。

AC代码:

#include<iostream>
#include<cstdio>
#include<vector>
#include<cstring>
#include<stack>
#include<queue>
#include<map>
#include<set> 
#include<algorithm>
using namespace std;
#define lowbit(x) (x&-x)
#define mem(a,b) memset(a,b,sizeof(a))
#define eps 1e-9
#define INF 999999
#define MAXN 100000+5
//struct edge{
//	int to;
//	//int val;
//};
//int next[MAXN]; 
vector<int> G[MAXN];
int dfn[MAXN],low[MAXN];
bool instack[MAXN];
stack<int> s;				
int timing;				//时间戳 
int color[MAXN];		//代表当前结点所属第几个scc 
int colorcnt[MAXN];		//代表第某个scc有几个结点 
int cnt;				//scc个数 
int V;				
int Outdee[MAXN],Indee[MAXN];


void init()
{
	for(int i=0;i<=V;i++)
		G[i].clear();
	while(!s.empty())
		s.pop();
	mem(dfn,0);mem(low,0);
	mem(color,0);
	mem(colorcnt,0);
	mem(instack,false);
	timing=cnt=0;
}

void tarjan(int u)
{
	timing++;
	dfn[u]=low[u]=timing;
	s.push(u);
	instack[u]=true;
	for(int i=0;i<G[u].size();i++)
	{
		//int v=next[u];
		int v=G[u][i];
		if(dfn[v]==0)
		{	
			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])
	{
		cnt++;
		int temp;
		do
		{
			//记录每个点属于哪个scc
			temp=s.top();
			s.pop();
			instack[temp]=false;
			color[temp]=cnt;
			colorcnt[cnt]++;	
		}while(u!=temp);	
	}
}

int main()
{
	bool ins[MAXN];
	ios::sync_with_stdio(false);
	mem(Outdee,0);
	mem(Indee,0);
	mem(ins,false);
	cin >> V;
	for(int i=1;i<=V;i++){
		int a;
		while(cin >> a && a){
			G[i].push_back(a);
		}
	}
	
	for(int i=1;i<=V;i++)
		if(dfn[i]==0)
			tarjan(i);	
//	cout <<"cnt: "<< cnt << endl;
	if(cnt==1)
		cout << cnt << endl << "0\n";
	else{
		int minn=INF;
		for(int k=1;k<=cnt;k++)
		{
			mem(ins,false);
			for(int i=1;i<=V;i++){
				if(color[i]==k)
				{
					for(int j=0;j<G[i].size();j++){
						int v=G[i][j];
						if(color[i]!=color[v]&&ins[v]==false){
							//cout << i  << " " << v << endl;
							ins[v]=true;
							Outdee[color[i]]++;
							Indee[color[v]]++;
						}
					}	
				}	
			}
		}
		int ans1=0,ans2=0;	
		for(int i=1;i<=cnt;i++){
			if(Outdee[i]==0)
				ans2++;
			if(Indee[i]==0)
				ans1++;
	//		minn=min(minn,cnt-outde[i]);
	//		cout << "de[" << i <<"]:" << outde[i] << endl;
		}
		cout << ans1 << endl;
		cout << max(ans1,ans2) << endl;
	}
//	for(int i=1;i<=V;i++){
//		cout << i << " color: " << color[i] << " colorcnt: " << colorcnt[i] << endl;	
//	}
	
	return 0;
}

/*
4
2 0
3 0
1 0
1 0

7
5 0
5 0
5 0
5 6 0
0
0
0

5
2 0
3 0
4 0
5 0
0

*/

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值