天天看點

【連通圖|強連通分量+縮點】POJ-1236 Network of Schools

Network of Schools

Time Limit: 1000MS Memory Limit: 10000K

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

1 0

Sample Output

1

2

Source

IOI 1996

題意: 給出一個有向連通圖,求(1)至少從幾個點出發可以周遊整張圖;(2)至少添加幾條邊可以使該圖變成強連通圖。

思路: 第一問很簡單,強連通分量縮點之後求出縮點後入度為0的點的個數。

第二問,縮點之後存在若幹入度為0和出度為0的點,将這些點互相連接配接起來,就會構成強連通圖。是以答案是max{入度為0的點個數,出度為0的點個數}。

P.S. 注意縮點之後隻剩下一個點的情況,這個時候入度為0和出度為0的點的個數都是1,但是不需要添加邊。

/*
 * ID: j.sure.1
 * PROG:
 * LANG: C++
 */
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <ctime>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <climits>
#include <iostream>
#define PB push_back
#define LL long long
using namespace std;
const int INF = 0x3f3f3f3f;
const double eps = 1e-8;
/****************************************/
const int N = 105, M = N*N;
struct Edge {
    int v, next;
    Edge(){}
    Edge(int _v, int _next):
        v(_v), next(_next){}
}e[M];
int dfn[N], head[N], scc_id[N];
int tot, deep, scc_cnt, n;
int line[M][2], in[N], out[N];
stack <int> s;

void init()
{
    memset(head, -1, sizeof(head));
    memset(dfn, 0, sizeof(dfn));
    memset(in, 0, sizeof(in));
    memset(out, 0, sizeof(out));
    tot = deep = scc_cnt = 0;
}

void add(int u, int v)
{
    e[tot] = Edge(v, head[u]);
    head[u] = tot++;
}

int dfs(int u)
{
    int lowu = dfn[u] = ++deep;
    s.push(u);
    for(int i = head[u]; ~i; i = e[i].next) {
        int v = e[i].v;
        if(!dfn[v]) {
            int lowv = dfs(v);
            lowu = min(lowu, lowv);
        }
        else if(!scc_id[v]) {
            lowu = min(lowu, dfn[v]);
        }
    }
    if(lowu == dfn[u]) {
        scc_cnt++;
        //printf("scc_cnt is %d\n", scc_cnt);
        while(1) {
            int x = s.top(); s.pop();
            scc_id[x] = scc_cnt;//number from 1
            if(x == u) break;
        }
    }
    return lowu;
}

void tarjan()
{
    for(int i = 1; i <= n; i++) {
        if(!dfn[i]) dfs(i);
    }
}

int main()
{
#ifdef J_Sure
    freopen("000.in", "r", stdin);
    //freopen("999.out", "w", stdout);
#endif
    scanf("%d", &n);
    init();
    int j, L = 0;
    for(int i = 1; i <= n; i++) {
        while(scanf("%d", &j), j) {
            add(i, j);
            line[L][0] = i; line[L][1] = j;
            L++;
        }
    }
    tarjan();
    if(scc_cnt == 1) {
        printf("1\n0\n");
        return 0;
    }
    for(int i = 0; i < L; i++) {
        int u = scc_id[line[i][0]], v = scc_id[line[i][1]];
        if(u != v) {
            in[v]++;
            out[u]++;
        }
    }
    int noIn = 0, noOut = 0;
    for(int i = 1; i <= scc_cnt; i++) {
        if(!in[i]) noIn++;
        if(!out[i]) noOut++;
    }
    printf("%d\n", noIn);
    printf("%d\n", max(noIn, noOut));
    return 0;
}           

繼續閱讀