Network of Schools【強連通 + 縮點】

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 28605 Accepted: 11243

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

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <stack>
using namespace std;

const int maxn = 1e6 + 10;
struct node{
	int u, v;
	int nex;
}edge[maxn];
int cd[maxn], rd[maxn];
int head[maxn];
int belong[maxn];
int dfn[maxn], low[maxn];
int ins[maxn];
int n, m, cnt, kind, index;
stack<int>st;

void addedge(int u, int v) {
	edge[++cnt] = {u, v, head[u]};
	head[u] = cnt;
}

void tarjan(int u) {
	dfn[u] = low[u] = ++index;
	st.push(u);
	ins[u] = true;
	for(int i = head[u]; i; i = edge[i].nex) {
		int v = edge[i].v;
		if(dfn[v] == 0) {
			tarjan(v);
			low[u] = min(low[u], low[v]);
		}
		else if(ins[v])
			low[u] = min(low[u], dfn[v]);
	}
	if(dfn[u] == low[u]) {
		int tem;
		kind++;
		do{
			tem = st.top();
			st.pop();
			ins[tem] = false;
			belong[tem] = kind;
		}while(tem != u);
	}
}

void sd() {
	for(int i = 1; i <= cnt; i++) {
		int u = edge[i].u;
		int v = edge[i].v;
		if(belong[u] != belong[v])
			cd[belong[u]]++, rd[belong[v]]++;
	} 
}

int main() {
	cin >> n;
	int x;
	for(int i = 1; i <= n; i++) {
		while(cin >> x && x) {
			addedge(i, x);
		}
	}
	for(int i = 1; i <= n; i++)
		if(dfn[i] == 0)
			tarjan(i);
//	cout << kind << endl;
	sd();
	int ans1 = 0, ans2 = 0;
	for(int i = 1; i <= kind; i++) {
		if(cd[i] == 0)	ans1++;
		if(rd[i] == 0) ans2++;
	}
//	for(int i= 1; i <= kind; i++) 
//		cout << i << " " << cd[i] << " " << rd[i] << endl;
	cout << ans2 << endl << (kind == 1 ? 0 : max(ans1, ans2)) << endl;
	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章