POJ 1236 Network of Schools 强连通分量

来源:互联网 发布:winpe 网络组件 编辑:程序博客网 时间:2024/06/14 20:46

求一个有向图从几个点出发可以遍历整个图、以及至少加几条边使整张图强联通。

缩点以后,显然入度为0的点的个数就是第一问的答案。
然后第二问答案显然是入度为0和出度为0的个数的最大值,即出入度为0的点间连条边就可以了。

Garbow好像比tarjan快?反正双联通也只会写Tarjan。。

#include <cstdio>#include <cstring>#include <algorithm>using namespace std;#define ms(a, b) memset(a, b, sizeof(a))const int N = 101, M = 10001;int h[N], p[M], v[M], belong[N], out[N], in[N], dfn[N], low[N];int scc, top, ts, cnt, stack[N], instack[N];void add(int a, int b) { p[++cnt] = h[a]; v[cnt] = b; h[a] = cnt; }void tarjan(int u) {    int i;    dfn[u] = low[u] = ++ts;    stack[++top] = u; instack[u] = 1;    for (i = h[u]; i; i = p[i])        if (!dfn[v[i]]) {            tarjan(v[i]);            low[u] = min(low[u], low[v[i]]);        } else if (instack[v[i]])            low[u] = min(low[u], dfn[v[i]]);    if (dfn[u] == low[u]) {        ++scc;        do {            i = stack[top--];            instack[i] = 0;            belong[i] = scc;        } while (i != u);    }}int main() {    int i, j, n, m, a, b;    while (scanf("%d", &n) != EOF) {        ms(dfn, 0); ms(out, 0); ms(h, 0); ms(in, 0);        ts = cnt = top = scc = 0;        for (i = 1; i <= n; ++i)            while (scanf("%d", &a) && a) add(i, a);        for (i = 1; i <= n; ++i)            if (!dfn[i]) tarjan(i);        for (i = 1; i <= n; ++i)            for (j = h[i]; j; j = p[j])                if (belong[i] != belong[v[j]])                    ++out[belong[i]], ++in[belong[v[j]]];        a = b = 0;        for (i = 1; i <= scc; ++i) {            if (!out[i]) ++a;            if (!in[i]) ++b;        }        if (scc == 1) puts("1\n0");        else printf("%d\n%d\n", b, max(a, b));    }    return 0;}

Network of Schools

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 14309 Accepted: 5695

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

52 4 3 04 5 0001 0

Sample Output

12

Source

IOI 1996

0 0
原创粉丝点击