Link

传送门

Problem

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.

Mean

有N个学校,向这些学校发布软件。一个学校可以将软件分发给多个其他学校。

第一问求至少分发多少份软件拷贝才能使得所有学校都能用软件。

第二问求往图中加几条边才能使得:向任意学校分发软件,其他学校都能用上软件。

Analysis

求强连通分量,将同一联通分量缩点,这样就形成一个DAG。然后求出DAG每个点的入度和出度。设DAG中入度为0的点为cnt1,出度为0的点为cnt2。那么,第一问答案就是cnt1,第二问答案是max(cnt1, cnt2)

Code

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <stack>
#include <iostream>
using namespace std;

const int maxn = 110;
int N, M;
vector<int> G[maxn];
int pre[maxn], lowlink[maxn], sccno[maxn], dfs_clock, scc_cnt;
int in[maxn], out[maxn];
stack<int> st;

void dfs(int u) {
    pre[u] = lowlink[u] = ++dfs_clock;
    st.push(u);
    for(int i = 0; i < G[u].size(); ++i) {
        int v = G[u][i];
        if(!pre[v]) {
            dfs(v);
            lowlink[u] = min(lowlink[u], lowlink[v]);
        } else if(!sccno[v]) {
            lowlink[u] = min(lowlink[u], pre[v]);
        }
    }
    if(lowlink[u] == pre[u]) {
        ++scc_cnt;
        while(true) {
            int x = st.top(); st.pop();
            sccno[x] = scc_cnt;
            if(x == u) break;
        }
    }
}

void find_scc() {
    dfs_clock = scc_cnt = 0;
    memset(sccno, 0, sizeof(sccno));
    memset(pre, 0, sizeof(pre));
    for(int i = 1; i <= N; ++i)
        if(!pre[i]) dfs(i);
}


int main()
{
    scanf("%d", &N);
    for(int i = 1; i <= N; ++i) {
        int to;
        while(scanf("%d", &to), to)
            G[i].push_back(to);
    }
    find_scc();
    if(scc_cnt == 1) {
        printf("1\n0\n");
        return 0;
    }
    for(int i = 1; i <= N; ++i) {
        for(int j = 0; j < G[i].size(); ++j) {
            int u = i, v = G[i][j];
            if(sccno[u] != sccno[v]) {
                ++in[sccno[v]];
                ++out[sccno[u]];
            }
        }
    }
    int cnt1 = 0, cnt2 = 0;
    for(int i = 1; i <= scc_cnt; ++i) {
        if(!in[i]) ++cnt1;
        if(!out[i]) ++cnt2;
    }
    printf("%d\n%d\n", cnt1, max(cnt1, cnt2));
    return 0;
}

PS: 地球离开了谁都会转,风走了,云不会纠缠。认真,就输了。

欢迎留言