POJ-1236-Network of Schools【强连通分量】【缩点】


POJ-1236-Network of Schools


                Time Limit: 1000MS      Memory Limit: 10000K

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

题目链接:POJ-1236

题目大意:N(2 < N < 100)个学校之间有单向的网络,每个学校得到一套软件后,可以通过单向网络向周边的学校传输。
问题1:初始至少需要向多少个学校发放软件,使得网络内所有的学校最终都能得到软件。
问题2:至少需要添加几条传输线路(边),使任意向一个学校发放软件后,经过若干次传送,网络内所有的学校最终都能得到软件。

题目思路:

强连通定义:有向图G中任意两点v1、v2之间都存在着v1到v2的路径及v2到v1的路径。

  1. 利用Tarjan求出各点所属的强连通块
  2. 每个强连通分量缩成一个点。记录每个点的入度和出度
  3. 问题1:DAG上面有多少个入度为0的顶点
    问题2:在DAG上要加几条边,才能使得DAG变成强连通的

此外:当只有一个强连通分支的时候,就是缩点后只有一个点,虽然入度出度为0的都有一个,但是实际上不需要增加清单的项了,所以答案是1,0;

以下是代码:


//
//  POJ-1236-Network of Schools.cpp
//  POJ
//
//  Created by pro on 16/4/1.
//  Copyright (c) 2016年 loy. All rights reserved.
//

#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;

/* Tarjan算法
* 复杂度O(N+M) */
const int MAXN = 20010;//点数
const int MAXM = 50010;//边数
struct Edge
{
    int to,next;
}edge[MAXM];

int head[MAXN],tot;
int Low[MAXN],DFN[MAXN],Stack[MAXN],Belong[MAXN];//Belong数组的值是1~scc
int Index,top;
int scc;//强连通分量的个数
int Instack[MAXN];
int num[MAXN];//各个强连通分量包含点的个数,数组编号1~scc
//num数组不一定需要,结合实际情况
void addedge(int u,int v)
{
    edge[tot].to = v;edge[tot].next = head[u];head[u] = tot++;
}

void Tarjan(int u) {
    int v;
    Low[u] = DFN[u] = ++Index;  //刚搜到一个节点时Low = DFN;
    Stack[top++] = u;  //将该节点入栈
    Instack[u] = 1;  //将入栈标记设置为1
    for (int i = head[u]; i != -1; i = edge[i].next)
    {
        v = edge[i].to;
        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])
    {
        scc++;
        do
        {
            v = Stack[--top];
            Instack[v] = false;
            Belong[v] = scc;
            num[scc ]++;
        }
        while( v != u);
    }
}
void solve(int N)
{
    memset( DFN,0 ,sizeof(DFN));
    memset( Instack,false, sizeof(Instack) );
    memset( num,0 ,sizeof(num));
    Index = scc = top = 0;
    for(int i = 1;i <= N;i++)
        if(!DFN [i])
            Tarjan(i);
}
void init()
{
    tot = 0;
    memset( head, -1,sizeof (head));
}
int out[MAXN],in[MAXN];
map <int,int> mp[MAXN];
int main()
{
    int n;
    while(cin >> n)
    {
        init();
        for(int i = 1; i <= n; i++)
        {
            int num;
            while(cin >> num)
            {
                if (num == 0) break;
                mp[i][num] = 1;
                addedge(i,num);
            }
        }
        solve(n);
        if (scc == 1)
        {
            cout << "1\n0\n";
            continue;
        }
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= n; j++)
            {
                if (mp[i][j] == 1 && Belong[i] != Belong[j])
                {
                    out[Belong[i]]++;
                    in[Belong[j]]++;
                }
            }
        }
        int  ans1 = 0,ans2 = 0;
        for (int i = 1; i <= scc; i++)
        {
            if (in[i] == 0) ans1++;
            if (out[i] == 0) ans2++;
        }
        cout << ans1 << endl << max(ans1,ans2) << endl;
    }
}

你可能感兴趣的:(ACM-图论-强连通,POJ)