poj 1236 Network of Schools

一 原题

Network of Schools
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 22353 Accepted: 8786

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


二 分析

给一个有向图,如果顶点A有资源,有一条从A到B的边,那么B也会获得资源,问如果要让所有点获得资源,至少要选择多少个点。如果要求任意选择一个顶点,资源都可以扩散到全部顶点,那么至少还需要增加多少条有向边。

把一个强连通分量缩成一个点,得到新的有向图中是没有环的,第一问就是数新图中有多少个点的入度为0,第二小问就是问入度为0的点的个数和出度为0的点个数的较大值。最后对只有一个连通分量的情况特判一下就好。

三 代码

/*
AUTHOR: maxkibble
LANG: c++
PROB: poj 1236
*/

#include <cstdio>
#include <stack>
#include <vector>

const int maxn = 105;

#define pb push_back

int n;
std::vector<int> g[maxn];

void init() {
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
        while (true) {
            int t;
            scanf("%d", &t);
            if (!t) break;
            g[i].pb(t);
        }
    }
}

int clk, dfu[maxn], low[maxn], sz, belong[maxn];
bool inStack[maxn];
std::stack<int> st;
std::vector<std::vector<int> > comp;

void tarjan(int u) {
    dfu[u] = low[u] = ++clk;
    st.push(u);
    inStack[u] = true;
    for (int i = 0; i < g[u].size(); i++) {
        int v = g[u][i];
        if (!dfu[v]) {
            tarjan(v);
            low[u] = std::min(low[u], low[v]);
        } else if (inStack[v]) {
            low[u] = std::min(low[u], dfu[v]);
        }
    }
    if (dfu[u] == low[u]) {
        std::vector<int> v;
        sz++;
        while (true) {
            int hd = st.top();
            st.pop();
            inStack[hd] = false;
            v.pb(hd);
            belong[hd] = sz;
            if (hd == u) break;
        }
        comp.pb(v);
    }
}

std::vector<int> g1[maxn];
bool mark[maxn][maxn];
int inDegree[maxn], ans1, ans2;

void solveA() {
    for (int i = 1; i <= n; i++) {
        if (!dfu[i]) tarjan(i);
    }
    for (int i = 1; i <= n; i++) {
        for (int j = 0; j < g[i].size(); j++) {
            int d = g[i][j];
            mark[belong[i]][belong[d]] = true;
        }
    }
    for (int i = 1; i <= sz; i++) {
        for (int j = 1; j <= sz && j != i; j++) {
            if (mark[i][j]) {
                g1[i].pb(j);
                inDegree[j]++;
            }
        }
    }
    for (int i = 1; i <= sz; i++) {
        if (!inDegree[i]) ans1++;
    }
    if (sz == 1) ans1 = 1;
    printf("%d\n", ans1);
}

void solveB() {
    for (int i = 1; i <= sz; i++) {
        if (g1[i].size() == 0) ans2++;
    }
    printf("%d\n", (sz == 1)? 0: std::max(ans1, ans2));
}

int main() {
    init();
    solveA();
    solveB();
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值