天天看點

POJ 1236 - Network of Schools(強連通分量)

Network of Schools Time Limit:1000MS    Memory Limit:10000KB    64bit IO Format:%I64d & %I64u Appoint description:

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

題意:有n個學校,學校之間可以傳遞資訊,為單向傳遞。

問題一:至少要向幾個學校傳遞原始資訊,才能保證所有學校都能收到資訊。

問題二:至少要添加多少組關系(每組關系類型如右:a 可以 向 b 傳遞資訊),才能保證 給任意一個學校原始資訊後,其他所有學校都能收到資訊。 思路:這道題其實就是一個有n個頂點的有向圖,先用 Tarjan 算法縮點 , 然後分别統計出 入度為0 和 出度為0 的強連通分量個數 num1 和 num2,那麼, 問題一的答案就是 num1 , 問題二的答案就是 max(num1 , num2),但是有特例:當隻有一個強連通分量時,問題二的答案就是 0 。

<span style="font-size:18px;">#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <string>
#include <algorithm>
#include <queue>
#include <stack>
using namespace std;

const double PI = acos(-1.0);
const double e = 2.718281828459;
const double eps = 1e-8;
const int MAXN = 110;
int n;
struct Edge
{
    int v;
    int w;
    int next;
} edge[MAXN*MAXN];
struct shrink_point
{
    int in;
    int out;
    int num;
} sp[MAXN];
int head[MAXN];
int instack[MAXN];
int scc[MAXN];
int LOW[MAXN];
int DFN[MAXN];
stack<int>Q;
int Index, edge_cnt, scc_cnt;

void addedge(int u, int v)
{
    edge[edge_cnt].v = v;
    edge[edge_cnt].next = head[u];
    head[u] = edge_cnt++;
}

void Tarjan(int u)
{
    LOW[u] = DFN[u] = ++Index;
    instack[u] = 1;
    Q.push(u);
    for(int i = head[u]; i != -1; i = edge[i].next)
    {
        int v = edge[i].v;
        if(!DFN[v])
        {
            Tarjan(v);
            if(LOW[u] > LOW[v])
                LOW[u] = LOW[v];
        }
        else if(instack[v] && LOW[u]>DFN[v])
            LOW[u] = DFN[v];
    }
    if(LOW[u] == DFN[u])
    {
        int j;
        scc_cnt++;
        do
        {
            j = Q.top();
            Q.pop();
            instack[j] = 0;
            scc[j] = scc_cnt;
            sp[scc_cnt].num++;
        }
        while(j != u);
    }
}

void solve()
{
    memset(LOW, 0, sizeof(LOW));
    memset(DFN, 0, sizeof(DFN));
    memset(instack, 0, sizeof(instack));
    memset(sp, 0, sizeof(sp));
    while(!Q.empty())
        Q.pop();
    Index = scc_cnt = 0;
    for(int i = 1; i <= n; i++)
        if(!DFN[i])
            Tarjan(i);
    for(int i = 1; i <= n; i++)
        for(int k = head[i]; k != -1; k = edge[k].next)
        {
            int j = edge[k].v;
            if(scc[i] != scc[j])
            {
                sp[scc[i]].out++;
                sp[scc[j]].in++;
            }
        }
    if(scc_cnt == 1)
    {
        printf("%d\n%d\n", 1, 0);
        return;
    }
    int num1 = 0;
    int num2 = 0;
    for(int i = 1; i <= scc_cnt; i++)
    {
        if(sp[i].out == 0)
            num1++;
        if(sp[i].in == 0)
            num2++;
    }
    //printf("%d %d\n", num1, num2);
    printf("%d\n%d\n", num2, max(num1, num2));
}

int main()
{
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    while(cin>>n)
    {
        int v;
        edge_cnt = 0;
        memset(head, -1, sizeof(head));
        memset(edge, 0, sizeof(edge));
        for(int i = 1; i <= n; i++)
        {
            while(cin>>v&&v)
            {
                addedge(i, v);
            }
        }
//        for(int i = 1; i <= n; i++)
//        {
//            printf("%d", i);
//            for(int k = head[i]; k != -1; k = edge[k].next)
//            {
//                printf(" %d", edge[k].v);
//            }
//            printf("\n");
//        }
        solve();
    }
    return 0;
}
</span>