A - Network of Schools
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 <cstdio>
#include <cmath>
#include <stack>
#include <cstring>
#define ll long long
#define db double
using namespace std;
const int mn = 103;
int kuai, belong[mn], in[mn], out[mn];
int ecnt, to[mn * mn], nx[mn * mn], fr[mn];
void addedge(int u, int v)
{
++ecnt;
to[ecnt] = v;
nx[ecnt] = fr[u];
fr[u] = ecnt;
}
/// Tarjan 模板
int idx, low[mn], dfn[mn];
bool insta[mn];
stack<int> sta;
void tarjan(int u)
{
low[u] = dfn[u] = ++idx;
sta.push(u);
insta[u] = 1;
for (int i = fr[u]; i != -1; i = nx[i])
{
int v = to[i];
if (!dfn[v])
{
tarjan(v);
low[u] = min(low[u], low[v]);
}
if (dfn[v] && insta[v])
low[u] = min(low[u], dfn[v]);
}
if (low[u] == dfn[u])
{
++kuai;
int v;
do
{
v = sta.top();
insta[v] = 0;
belong[v] = kuai;
sta.pop();
}
while (v != u);
}
}
///
int main()
{
memset(fr, -1, sizeof fr);
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++)
{
while (1)
{
int t;
scanf("%d", &t);
if (t == 0)
break;
addedge(i, t);
}
}
for (int i = 1; i <= n; i++)
{
if (!dfn[i])
tarjan(i);
}
for (int i = 1; i <= n; i++)
{
for (int j = fr[i]; j != -1; j = nx[j])
{
int u = belong[i], v = belong[to[j]];
if (u != v)
out[u]++, in[v]++;
}
}
int ans1 = 0, ans2 = 0;
for (int i = 1; i <= kuai; i++)
{
if (in[i] == 0)
ans1++;
if (out[i] == 0)
ans2++;
}
if (kuai == 1)
printf("1\n0\n");
else
printf("%d\n%d\n", ans1, max(ans1, ans2));
return 0;
}
本文探讨了一种算法,用于解决学校网络中软件分发的优化问题。算法旨在找到最少的学校数量,确保软件能够通过既定协议到达网络中的所有学校,并计算为达到任意学校发送软件即可覆盖全网的目标所需的最小扩展次数。
1114

被折叠的 条评论
为什么被折叠?



