POJ - 1236 强连通分量

Network of Schools

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 22905   Accepted: 8981

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

题目链接:https://vjudge.net/problem/17001/origin

题意:各学校之间有单向的网络,每个学校得到一套软件,可以通过单向网络向周边的学校传输。问题1:初始至少需要向多少个学校发放软件,使得网络内所有的学校最终都能得到软件。

问题2:至少需要添加几条传输线路(边),使任意向一个学校发放软件后,经过若干次传送,网络内所有的学校最终都能得到软件。
对于问题1,就是求强连通向量后的入度为0的点的个数。
对于问题2,当sig = 1时,不需要添加任何途径。否者取进度和入度两者点的最大值。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;

const int maxn = 105;
int low[maxn], dfn[maxn];
int inde[maxn], outde[maxn];
int color[maxn];
int vis[maxn];
int sta[maxn];
int n, cnt, len, sig;
vector<int> G[maxn];

void init()
{
    for(int i = 0; i < maxn; i++)
        G[i].clear();
    memset(low, 0, sizeof low);
    memset(dfn, 0, sizeof dfn);
    memset(vis, 0, sizeof vis);
    memset(color, 0, sizeof color);
    memset(inde, 0, sizeof inde);
    memset(outde, 0, sizeof outde);
    cnt = 0;
    len = -1;
    sig = 0;
}

void Tarjan(int u)
{
    vis[u] = 1;
    sta[++len] = u;
    low[u] = dfn[u] =  ++cnt;
    for(int i = 0; i < G[u].size(); i++)
    {
        int v = G[u][i];
        if(vis[v] == 0)
            Tarjan(v);
        if(vis[v] == 1)
            low[u] = min(low[u], low[v]); 
    }
    if(dfn[u] == low[u])
    {
        sig ++;
        do{
            int v = sta[len];
            color[v] = sig;
            vis[v] = -1;
         }while(sta[len--] != u);
    }
}

void slove()
{
    for(int i = 1; i <= n; i++)
    {
        if(vis[i] == 0)
             Tarjan(i);
    }
    for(int i = 1; i <= n; i++)
        for(int j = 0; j < G[i].size(); j++)
        {
            int v = G[i][j];
            if(color[i] != color[v])
            {
                inde[color[v]]++;
                outde[color[i]]++; 
            }
        }
    int ans1 = 0, ans2 = 0;
    for(int i = 1; i <= sig; i++)
    {
        if(inde[i] == 0)
            ans1++;
        if(outde[i] == 0)
            ans2++;
    }
    //cout << sig << endl;
    if(sig == 1)
        ans2 = 0;
    else
        ans2 = max(ans1, ans2);
    printf("%d\n%d\n", ans1, ans2);
}

int main()
{
    while(~scanf("%d", &n))
    {
        init();
        for(int i = 1; i <= n; i++)
        {
            int u;
            while(~scanf("%d", &u) && u)
            {
                G[i].push_back(u);
            }
        }
        slove();
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_38295645/article/details/81356531
今日推荐