MENU

POJ1236 Network of Schools(Tarjan缩点)

2018 年 04 月 26 日 • 阅读: 1205 • 图论阅读设置

Network of Schools

  • Time Limit: 1000MS
  • Memory Limit: 10000K
  • Total Submissions: 21632
  • Accepted: 8515

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

IOI 1996


链接

http://poj.org/problem?id=1236

题意

给定一个n点m边有向图,问:

  • 至少选择多少个点才能从这些点到达其他所有点
  • 至少添加几条边才能从任意一点到达其他所有点

题解

Tarjan求强连通分量后缩点得到DAG图,设图中入度为0的点的数量为cnt1,出度为0的点的数量为cnt2,那么第一个问题的答案为cnt1,第二个问题的答案为max(cnt1, cnt2),注意,当所有点构成一个强连通分量时,输出1和0。

代码

StatusAccepted
Memory716kB
Length2400
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cstring>
#include <stack>
using namespace std;
const int maxn = 110;
vector <int> G[maxn];
int n;
int tot, scc_cnt;

int dfn[maxn], low[maxn], sccno[maxn], inde[maxn], outde[maxn];
bool vis[maxn];
stack <int> s;

void tarjan(int x) // 求强连通分量
{
    dfn[x] = low[x] = ++tot;
    s.push(x);
    vis[x] = true;
    for (int i = 0; i < G[x].size(); ++i)
    {
        int v = G[x][i];
        if (!dfn[v])
        {
            tarjan(v);
            low[x] = min(low[x], low[v]);
        }
        else if (vis[v])
            low[x] = min(low[x], dfn[v]);
    }
    if (dfn[x] == low[x])
    {
        ++scc_cnt;
        while (1)
        {
            int u = s.top();
            s.pop();
            vis[u] = 0;
            sccno[u] = scc_cnt;
            if (u == x)
                break;
        }
    }
}

void find_scc()
{
    memset(vis, 0, sizeof(vis));
    memset(dfn, 0, sizeof(vis));
    memset(low, 0, sizeof(low));
    memset(sccno, 0, sizeof(sccno));
    tot = scc_cnt = 0;
    for (int i = 0; i < n; ++i)
        if (!dfn[i])
            tarjan(i);
}

int main()
{
    int u;
    while (scanf("%d", &n) != EOF)
    {
        for (int i = 0; i <= n; ++i)
            G[i].clear();
        for (int i = 0; i < n; ++i)
        {
            scanf("%d", &u);
            while (u != 0)
            {
                G[i].push_back(u-1);
                scanf("%d", &u);
            }
        }
        find_scc();
        memset(inde, 0, sizeof(inde));
        memset(outde, 0, sizeof(outde));
        for (int u = 0; u < n; ++u) // 缩点建图
        {
            for (int i = 0; i < G[u].size(); ++i)
            {
                int v = G[u][i];
                if (sccno[u] != sccno[v])
                {
                    inde[sccno[v]]++;
                    outde[sccno[u]]++;
                }
            }
        }
        int cnt1 = 0, cnt2 = 0;
        for (int i = 1; i <= scc_cnt; ++i)
        {
            if (inde[i] == 0)
                cnt1++;
            if (outde[i] == 0)
                cnt2++;
        }
        if (scc_cnt == 1)
            printf("1\n0\n");
        else
            printf("%d\n%d\n", cnt1, max(cnt1, cnt2));
    }
    return 0;
}
//    freopen("/home/taifu/ACMer/Training/data.in", "r", stdin);
//    freopen("/home/taifu/ACMer/Training/data.out","w",stdout);

这题的题意是真的难懂,看了programmy写的题解才知道题目是什么意思。


The end.
2018-04-26 星期四
最后编辑于: 2018 年 07 月 14 日