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.






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


#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;
    for(int i = 0; i < G[u].size(); ++i) {
        int v = G[u][i];
        if(!pre[v]) {
            lowlink[u] = min(lowlink[u], lowlink[v]);
        } else if(!sccno[v]) {
            lowlink[u] = min(lowlink[u], pre[v]);
    if(lowlink[u] == pre[u]) {
        while(true) {
            int x =; 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)
    if(scc_cnt == 1) {
        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]) {
    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: 地球离开了谁都会转,风走了,云不会纠缠。认真,就输了。