**Network of Schools**
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 17641 Accepted: 6968
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
题意:各学校之间有单向的网络,每个学校得到一套软件后,可以通过单向网络向周边的学校传输,问题1:初始至少需要向多少个学校发放软件,使得网络内所有的学校最终都能得到软件。2,至少需要添加几条传输线路(边),使任意向一个学校发放软件后,经过若干次传送,网络内所有的学校最终都能得到软件。
所用算法:强连通分量-tarjan
AC代码:
# include <cstdio>
# include <cstring>
# include <algorithm>
using namespace std;
# define MAXN 105
struct EDGE
{
int to;
int next;
};
EDGE edge[MAXN * 100];
int head[MAXN];
int tot;
int LOW[MAXN];
int DFN[MAXN];
int Stack[MAXN];
int Belong[MAXN];
bool Isinstack[MAXN];
int Index;
int top;
int scc;
int in[MAXN], out[MAXN];
void AddEdge(int u, int v)
{
edge[tot].to = v;
edge[tot].next = head[u];
head[u] = tot++;
}
void Tarjan(int u)
{
int v;
LOW[u] = DFN[u] = ++Index;
Stack[top++] = u;
Isinstack[u] = true;
for (int i = head[u]; i != -1; i = edge[i].next)
{
v = edge[i].to;
if (!DFN[v])
{
Tarjan(v);
if (LOW[u] > LOW[v])
{
LOW[u] = LOW[v];
}
}
else if (Isinstack[v] && LOW[u] > DFN[v])
{
LOW[u] = DFN[v];
}
}
if (LOW[u] == DFN[u])
{
scc++;
do
{
v = Stack[--top];
Belong[v] = scc;
Isinstack[v] = false;
} while (v != u);
}
}
void Solve(int N)
{
int i;
memset(DFN, 0, sizeof(DFN));
memset(Isinstack, false, sizeof(Isinstack));
Index = scc = top = 0;
for (i = 1; i <= N; i++)
{
if (!DFN[i])
{
Tarjan(i);
}
}
if (1 == scc)
{
printf("1\n0\n");
return;
}
for (i = 1; i <= scc; i++)
{
in[i] = out[i] = 0;
}
for (int u = 1; u <= N; u++)
{
for (i = head[u]; i != -1; i = edge[i].next)
{
int v = edge[i].to;
if (Belong[u] != Belong[v])
{
in[Belong[v]]++;
out[Belong[u]]++;
}
}
}
int ans1 = 0, ans2 = 0;
for (i = 1; i <= scc; i++)
{
if (!in[i])
{
ans1++;
}
if (!out[i])
{
ans2++;
}
}
int t = ans1 > ans2 ? ans1 : ans2;
printf("%d\n%d\n", ans1, t);
}
void Init()
{
tot = 0;
memset(head, -1, sizeof(head));
}
int main(void)
{
int n;
int v;
while (~scanf("%d", &n))
{
Init();
for (int i = 1; i <= n; i++)
{
while (scanf("%d", &v) && v)
{
AddEdge(i, v);
}
}
Solve(n);
}
return 0;
}