Network of Schools POJ1236(tarjan縮點+強連通分量模板)

Network of Schools
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 19564   Accepted: 7705

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

題目大意:

一些學校連成了網絡, 在學校之間存在某個協議:每個學校都維護一張傳送表,表明他們要負責將收到的軟件傳送到表中的所有學校。如果A在B的表中,那麼B不一定在A的表中。

    現在的任務就是,給出所有學校及他們維護的表,問1、如果所有學校都要被傳送到,那麼需要幾份軟件備份;2、如果只用一份軟件備份,那麼需要添加幾條邊?

解題思路:對於第一個問題來說,需要算出來一共有多少個強連通分量,然後縮點形成一個圖,尋找一共有幾個入度爲0的點,然後對於第二問來說,需要做的是隻要算出入度爲0和出度爲0的點的個數最大值即可

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <cstdlib>
#include<stack>
#include <ctime>
using namespace std;
typedef long long LL;

int check[105],low[105];
vector<int> tu[105];
int index,instack[105],ans[105],a[105],fllow[105],sum;
stack<int> st;
int inn[105],outt[105];
//check[u]數組表示節點u搜索的次序編號時間戳。low[u]爲u能追溯到的最早的時間戳
void tarjan(int x)
{
	check[x]=low[x]=++index;//初始化搜索到的節點x
	st.push(x);//將x點放在求強連通分量的棧中
	instack[x]=1;//x點是否在棧中
	for(int i = 0;i<tu[x].size();i++)
	{
		int k=tu[x][i];
		if(!check[k])
		{
			tarjan(k);
			low[x]=min(low[x],low[k]);//比較當前點x能夠到達的最早時間戳,與他的子樹中的點的最早時間戳比較
		}
		else
		{
			if(instack[k])//如果x爲根的子樹中有K在棧中
			{
				low[x]=min(low[x],check[k]);//比較k的時間戳大小
			}
		}
	}
	if(check[x]==low[x])//x點爲根節點
	{
		sum++;
		int k=0;
		while(k!=x)//直到符合條件前,這些點都是強連通分量
		{
			k=st.top();
			st.pop();
			fllow[k]=sum;//將這些點分組進行標記
			instack[k]=0;
			ans[sum]=x;
		}
	}
}
int main()
{
	int i,n,x;
	cin>>n;
	for(i=1;i<=n;i++)
	{
		for(;;)
		{
			cin>>x;
			if(x==0)
				break;
			tu[i].push_back(x);
			a[x]++;//
		}
	}
	int cnt=0;
	index=0;
	for(i=1;i<=n;i++)
		if(!check[i])//存在多個圖
			tarjan(i);
	int t1,t2;
	t1=t2=0;
	int j;
	for(i=1;i<=n;i++)
	{
		for(j=0;j<tu[i].size();j++)
		{
			int k=tu[i][j];
			if(fllow[k]!=fllow[i])//
			{
				inn[fllow[k]]++;
				outt[fllow[i]]++;
			}
		}
	}
	for(i=1;i<=sum;i++)
	{
		if(inn[i]==0)
		{
			t1++;
		}
		if(outt[i]==0)
		{
			t2++;
		}
	}
	if(sum==1)
	{
		cout<<1<<endl;
		cout<<0<<endl;
		return 0;
	}
	cout<<t1<<endl;
	cout<<max(t1,t2)<<endl;
	return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章