专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > 网络通信

POJ 1236 - Network of Schools(强接通分量)

发布时间:2011-07-03 06:58:05 文章来源:www.iduyao.cn 采编人员:星星草
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>



友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: