Network of Schools POJ - 1236(塔尖算法的入門)

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

Sponsor

題意:n個學校,學校可以發送物品到另一個學校(單向)

         求:1.最少需要幾個學校初始有物品,才能使物品傳遞到所有學校。
                2.最少需要加幾條邊,纔可以實現強連通分量。

塔尖算法問題的常見類型:

          給定一個有向圖N個頂點 1.求至少要選幾個頂點才能做到從這些頂點出發可以到達全部的頂點。

                                                  2.至少要加多少條邊才能使得從任何一個頂點出發都能到達全部頂點

         思路: 塔尖縮點 建圖

                         1.入度爲0的點數

                         2.max(入度爲0的個數,出度爲0的個數) 特別地,強連通分量只有1個時,答案爲0.

此題代碼:

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;
int fir[110],nex[11000],to[11000];
int dfn[110],low[110];
int st[110],co[110];
int num,tot,top,col;
int in[110],out[110];
void build(int x,int y)
{
    to[++tot]=y;
    nex[tot]=fir[x];
    fir[x]=tot;
}
void tarjan(int u)
{
    dfn[u]=low[u]=++num;
    st[++top]=u;
    for(int i=fir[u]; i>0; i=nex[i])
    {
        int v=to[i];
        if(!dfn[v])
        {
            tarjan(v);
            low[u]=min(low[u],low[v]);
        }
        else
        {
            if(!co[v])
                low[u]=min(low[u],dfn[v]);
        }
    }
    if(dfn[u]==low[u])
    {
        co[u]=++col;
        while(st[top]!=u)
        {
            co[st[top]]=col;
            --top;
        }
        --top;
    }
}
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        memset(fir,0,sizeof(fir));
        memset(dfn,0,sizeof(dfn));
        memset(low,0,sizeof(low));
        memset(st,0,sizeof(st));
        memset(co,0,sizeof(co));
        memset(in,0,sizeof(in));
        memset(out,0,sizeof(out));
        memset(to,0,sizeof(to));
        tot=0,top=0,col=0,num=0;
        int x;
        for(int i=1; i<=n; i++)
        {
            while(~scanf("%d",&x))
            {
                if(x==0)
                    break;
                build(i,x);
            }
        }
        for(int i=1; i<=n; i++)
        {
            if(!dfn[i])
                tarjan(i);
        }
        for(int i=1; i<=n; i++)
        {
            for(int j=fir[i]; j; j=nex[j])
            {
                if(co[i]!=co[to[j]])
                {
                    in[co[to[j]]]++;
                    out[co[i]]++;
                }
            }
        }
        int num1=0,num2=0;
        for(int i=1; i<=col; i++)
        {
            if(!in[i])
                num1++;
            if(!out[i])
                num2++;
        }
        if(col==1)
            printf("1\n0\n");
        else
        {
            printf("%d\n",num1);
            printf("%d\n",max(num1,num2));
        }
    }
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章