Network of Schools (强连通分量加缩点)

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
题意:
1)至少要选几个顶点,才能做到从这些顶点出发,可以到达全部顶点。
2)至少要加几条边,才能使得从任何一个顶点出发,都能到达全部顶点。
需要用到的定理:有向无环图中所有入度不为0的点都可以由某个入度为0的点出发可达
思路:求出所有的SCC,每个SCC缩成一个点,则形成一个DAG,
DAG上有多少个入度为0的点问题1的答案就是多少。
问题2即是加多少条边整个图变成强连通的。
加边方法:为每个入度为0的边添加入边,为每个出度为0的边添加出边,假定n个入度为0的点,m个出度为0的点,选m和n中较大的。
AC的C++程序如下:

#include<iostream>
#include<cstring>
#include<string>
#include<vector>
#include<stack>
#include<algorithm>
using namespace std;
const int maxn = 10005;
int n;
vector<int> g[maxn];
int pre[maxn], lowlink[maxn], sccno[maxn], dfs_clock, scc_cnt;
stack<int> s;
void dfs(int u)
{
    pre[u] = lowlink[u] = ++dfs_clock;
    s.push(u);
    for (int i = 0; i < (int)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++;
        for (;;)
        {
            int x = s.top(); s.pop();
            sccno[x] = scc_cnt;
            if (x == u) break;
        }
    }
}
int main()
{
    while (cin>>n)
    {
        dfs_clock = scc_cnt = 0;
        memset(sccno, 0, sizeof(sccno));
        memset(pre, 0, sizeof(pre));
        memset(lowlink, 0, sizeof(lowlink));
        for (int i = 0; i <= n; i++) g[i].clear();
        while (!s.empty()) s.pop();
        for (int i = 1; i <= n; i++)
        {
            int val;
            while (true)
            {
                cin >> val;
                if (val == 0) break;
                g[i].push_back(val);
            }
        }
        for (int i = 1; i <= n; i++)
        {
            if (!pre[i]) dfs(i);
        }
        if (scc_cnt == 1) //如果整个图是强连通的
        {
            cout << 1 << endl;
            cout << 0 << endl;
        }
        else
        {
            bool into[maxn], outto[maxn];//定义入度和出度是否为0
            for (int i = 1; i <=scc_cnt; i++)  into[i] = true, outto[i] = true; //初始化定义为度为0
            for (int i = 1; i <= n; i++)
            {
                for (int j = 0; j <(int)g[i].size(); j++)
                {
                    int dest = g[i][j];
                    if (sccno[i] != sccno[dest]) //如果i和dest不在一个连通分量中则i
                    {
                        outto[sccno[i]] = false;
                        into[sccno[dest]] = false;
                    }
                }
            }
            int a = 0, b = 0;
            for (int i = 1; i <= scc_cnt; i++)
            {
                if (into[i]) a++;
                if (outto[i]) b++;
            }
            cout << a << endl;
            cout << max(a, b) << endl;
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/jinduo16/article/details/81906188
今日推荐