POJ1236:Network of Schools(强连通)

本文探讨了如何通过最少的学校节点分发软件至整个学校网络,并确保任意节点发起的分发都能覆盖全网的问题。利用Tarjan算法进行图的简化处理,解决了寻找最小分发源及扩展连接以实现强连通性的需求。

Network of Schools
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 19179 Accepted: 7536

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


题意:给N个点M条边的有向图,1问至少选择多少个点传送给信息,其他所有点都能收到;2问至少添加几条边,使得任意选择一个点传送信息,其他点都能收到信息。

思路:问题1容易处理,tarjan缩点找入度为0的点个数即可,问题2相当于问一个DAG添加几条边能变成强连通图,由于本题保证联通分量只有一个,max(in==0,out==0)即可。

# include <iostream>
# include <cstdio>
# include <cstring>
using namespace std;
struct node
{
    int u, v, next;
}edge[3000];
int dep[103], Next[103], stk[103], in_stk[103], low[103];
int belong[103], in[103], out[103];
int tot, h, cnt, ans;
void add_edge(int u, int v)
{
    edge[tot] = node{u,v,Next[u]};
    Next[u] = tot++;
}

void dfs(int u)
{
    dep[u] = low[u] = ++h;
    stk[cnt++] = u;
    in_stk[u] = 1;
    for(int i=Next[u]; i!=-1; i=edge[i].next)
    {
        int v = edge[i].v;
        if(!dep[v])
        {
            dfs(v);
            low[u] = min(low[u], low[v]);
        }
        else if(in_stk[v] && dep[v] < low[u])
            low[u] = dep[v];
    }
    if(low[u] == dep[u])
    {
        int j;
        ++ans;
        do
        {
            j = stk[--cnt];
            in_stk[j] = 0;
            belong[j] = ans;
        }while(j != u);
    }
}
int main()
{
    int n, j;
    while(~scanf("%d",&n))
    {
        tot = h = cnt = ans = 0;
        memset(dep, 0, sizeof(dep));
        memset(Next, -1, sizeof(Next));
        memset(in_stk, 0, sizeof(in_stk));
        memset(belong, 0, sizeof(belong));
        memset(out, 0, sizeof(out));
        memset(in, 0, sizeof(in));
        memset(low, 0, sizeof(low));
        for(int i=1; i<=n; ++i)
            while(scanf("%d",&j),j)
                add_edge(i, j);
        for(int i=1; i<=n; ++i)
            if(!dep[i]) dfs(i);
        for(int i=1; i<=n; ++i)
        {
            for(int j=Next[i]; j!=-1; j=edge[j].next)
            {
                int k = edge[j].v;
                if(belong[i] != belong[k])
                    ++in[belong[k]], ++out[belong[i]];
            }
        }
        int sum = 0, sum2 = 0;
        for(int i=1; i<=ans; ++i)
        {
            if(in[i] == 0) ++sum;
            if(out[i] == 0) ++sum2;
        }
        if(ans == 1) printf("1\n0\n");
        else printf("%d\n%d\n",sum, max(sum, sum2));
    }
    return 0;
}


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值