一、内容
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
二、思路
- 首先对建好的图求一次SCC。 第一个输出的答案是叫求最小需要多少软件分配,才能使所有的学习都得到。 那么我们可以给每个拓扑图的头(即入度为0的点)发一个软件,那么这个拓扑图上的所有点都能获得软件。 故第一个答案就是入度为0的点。
- 最后特判下,若只有一个强连通的话,那么需要添加0条边。
三、代码
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 105, M = 1e4 + 5;
struct E {int v, next;} e[M];
int n, u, v, len, top, num, h[N], id[N], ind[N], oud[N], scc_cnt, stack[N], low[N], dfn[N];
bool in_st[N];
void add(int u, int v) {e[++len].v = v; e[len].next = h[u]; h[u] = len;}
void tarjan(int u) {
dfn[u] = low[u] = ++num;
stack[++top] = u; in_st[u] = true;
for (int j = h[u]; j; j = e[j].next) {
int v = e[j].v;
if (!dfn[v]) {
tarjan(v);
low[u] = min(low[u], low[v]);
} else if (in_st[v]) low[u] = min(low[u], dfn[v]);
}
if (dfn[u] == low[u]) {
++scc_cnt; int v;
do {
v = stack[top--]; in_st[v] = false;
id[v] = scc_cnt;
} while (u != v);
}
}
int main() {
scanf("%d", &n);
for (int u = 1; u <= n; u++) {while (scanf("%d", &v), v) {add(u, v);}}
for (int u = 1; u <= n; u++) if (!dfn[u]) tarjan(u);
//求出入度为0的点为答案一 求出max(入度0, 出度0) 为答案二
for (int u = 1; u <= n; u++) {
for (int j = h[u]; j; j = e[j].next) {
int v = e[j].v;
if (id[u] == id[v]) continue;
ind[id[v]]++, oud[id[u]]++;
}
}
int p = 0, q = 0;
for (int i = 1; i <= scc_cnt; i++) {
if (!ind[i]) p++;
if (!oud[i]) q++;
}
printf("%d\n", p);
if (scc_cnt == 1) printf("0");
else printf("%d", max(q, p));
return 0;
}