#POJ 1236 Network of Schools(Tarjan)

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

 

题目大意 :  输入一个有向图, 表示从点U到点V可以通信, 现在有两个问题,第一个是至少需要通知几个人, 才能让所有人都被通知到, 第二个是至少添加几条边, 才能做到通知一人所有人都可被通知。

思路 : 这题其实就是tarjan两种题型的合并,双倍的快乐~

            先缩点, 把能够互相通知的合并到一个集合中,然后判断入度和出度, 入度为0代表没有人可以给他通信, 所以他需要我们把它连上, 所以入度为0的数量就是子任务A的答案;出度为0代表他没人可以通知的人, 而要想通知一人做到所有人都可以被通知到,这个图只能是环,要想是环,就不能出现入度 or 出度二为0, 所以要把他们挨着连起来,SUM(入度为0)和SUM(出度为0)的最大值,即为子任务B的答案。  最后注意一点, 如果这张图本身就是一个环,缩点后只有一个点了,这时候应当输出1 和 0, 单独考虑。

AC代码 : 

#include<iostream>
#include<cstring>
#include<stack>
using namespace std;
const int maxn = 1e4 + 5;

struct node
{
    int v, next;
}e[maxn];
int dfn[maxn], low[maxn], suo[maxn], cnt, scnt, tot;
int head[maxn], out[maxn], in[maxn], n, m;
bool vis[maxn];
stack <int> st;
void init() {
     memset(e, 0, sizeof(e));
     memset(dfn, 0, sizeof(dfn));
     memset(low, 0, sizeof(low));
     memset(vis, 0, sizeof(vis));
     memset(suo, 0, sizeof(suo));
     memset(in, 0, sizeof(in));
     memset(out, 0, sizeof(out));
     memset(head, -1, sizeof(head));
     cnt = scnt = tot = 0;
}
void add (int from, int to) {
    e[++cnt].v = to;
    e[cnt].next = head[from];
    head[from] = cnt;
}
void tarjan(int x) {
    dfn[x] = low[x] = ++tot;
    vis[x] = 1;
    st.push(x);
    for (int i = head[x]; i != -1; i = e[i].next) {
        if (!dfn[e[i].v]) {
            tarjan(e[i].v);
            low[x] = min (low[x], low[e[i].v]);
        }
        else if (vis[e[i].v]) low[x] = min (low[x], dfn[e[i].v]);
    }
    if (dfn[x] == low[x]) {
        int k;
        scnt++;
        do {
            k = st.top();
            st.pop();
            vis[k] = 0;
            suo[k] = scnt;
        }
        while (k != x);
    }
}

int main()
{
    while (cin >> n) {
        init();
        for (int i = 1; i <= n; i++) {
            while (cin >> m && m) add (i, m);
        }
        for (int i = 1; i <= n; i++) {
            if (!dfn[i]) tarjan(i);
        }
        if (scnt == 1) {cout << 1 << endl << 0 << endl; continue;}
        for (int i = 1; i <= n; i++) {
            for (int j = head[i]; j != -1; j = e[j].next) {
                int u = suo[i], v = suo[e[j].v];
                if (u != v) out[u]++, in[v]++;
            }
        }
        int sum1 = 0, sum2 = 0;
        for (int i = 1; i <= scnt; i++) {
            if (!in[i]) sum1++;
            if (!out[i]) sum2++;
        }
        cout << sum1 << endl << max (sum1, sum2) << endl;
    }
    return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值