天天看點

POJ 1236 Network of Schools 強連通分量Network of Schools

求一個有向圖從幾個點出發可以周遊整個圖、以及至少加幾條邊使整張圖強聯通。

縮點以後,顯然入度為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 = , M = ;
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] = ;
    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] = ;
            belong[i] = scc;
        } while (i != u);
    }
}

int main() {
    int i, j, n, m, a, b;
    while (scanf("%d", &n) != EOF) {
        ms(dfn, ); ms(out, ); ms(h, ); ms(in, );
        ts = cnt = top = scc = ;
        for (i = ; i <= n; ++i)
            while (scanf("%d", &a) && a) add(i, a);
        for (i = ; i <= n; ++i)
            if (!dfn[i]) tarjan(i);
        for (i = ; 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 = ;
        for (i = ; i <= scc; ++i) {
            if (!out[i]) ++a;
            if (!in[i]) ++b;
        }
        if (scc == ) puts("1\n0");
        else printf("%d\n%d\n", b, max(a, b));
    }
    return ;
}
           

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

5
2 4 3 0
4 5 0
0
0
1 0
           

Sample Output

1
2
           

Source

IOI 1996