POJ - 1236 Network of Schools (强连通分支 + 缩点)

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个学校,若两所学校x、y之间有单向边,给x发消息时y也可以收到,问(1)最少给几个学校发消息,使得所有学校都可以收到(2)最少添加多少条边,使得给任意一所学校发消息,所有学校都可以收到

思路:

同一强连通分量中的点缩为1个点,找到入度为0的强连通分量,给这些强连通分量中的某个点发消息时所有的学校都可以收到,所以第一问就是入度为0的强连通分量的个数;

设出度为0的点有m个,它们不可以到达其他点,至少添加m条边,入度为0的点有n个,没有点可以到达它们,至少添加n条边,所以最少添加max(n, m)条边

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N = 1e4 + 10;
const int M = 5e4 + 10;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;

struct Edge {
    int to, next;
}edge1[M], edge2[M];    ///原图和逆图
int head1[N], head2[N];
bool mark1[N], mark2[N];
int tot1, tot2;
int cnt1, cnt2;
int st[N];  ///时间戳
int belong[N];
int num;   ///中间变量,用来数某个连通分量中点的个数
int setnum[N];///强连通分量中点的个数,编号 0∼cnt2-1
int n;

void addedge(int u, int v) {
    edge1[tot1].to = v;
    edge1[tot1].next = head1[u];
    head1[u] = tot1++;

    edge2[tot2].to = u;
    edge2[tot2].next = head2[v];
    head2[v] = tot2++;
}

void dfs1(int u) {
    mark1[u] = 1;
    for(int i = head1[u]; ~i; i = edge1[i].next)
        if(!mark1[edge1[i].to])
            dfs1(edge1[i].to);
    st[cnt1++] = u;
}

void dfs2(int u) {
    mark2[u] = 1;
    num++;
    belong[u] = cnt2;
    for(int i = head2[u]; ~i; i = edge2[i].next)
        if(!mark2[edge2[i].to])
            dfs2(edge2[i].to);
}

void solve(int n) {
    memset(mark1, 0, sizeof(mark1));
    memset(mark2, 0, sizeof(mark2));
    cnt1 = cnt2 = 0;
    for(int i = 1; i <= n; ++i)         ///dfs原图求时间戳
        if(!mark1[i])
            dfs1(i);
    for(int i = cnt1 - 1; i >= 0; --i) { ///dfs逆图
        if(!mark2[st[i]]) {
            num = 0;
            dfs2(st[i]);
            setnum[cnt2++] = num;
        }
    }
}
int in[N], out[N];

void init() {
    tot1 = tot2 = 0;
    memset(head1, -1, sizeof(head1));
    memset(head2, -1, sizeof(head2));
    memset(in, 0, sizeof(in));
    memset(out, 0, sizeof(out));
}

int main() {
    init();
    int u, v;
    scanf("%d", &n);
    for(int u = 1; u <= n; ++u) {
        while(~scanf("%d", &v)) {
            if(v == 0) break;
            addedge(u, v);
        }
    }
    solve(n);
    if(cnt2 == 1) {
        printf("1\n0\n");
        return 0;
    }
    ///缩点 同一强连通分量的点缩为1个点,下标就是强连通分量的标号
    for(int i = 1; i <= n; ++i) {
        for(int j = head2[i]; ~j; j = edge2[j].next) {
            if(belong[i] != belong[edge2[j].to]) {
                out[belong[edge2[j].to]]++; ///edge2是逆图,入点的出度++
                in[belong[i]]++;
            }
        }
    }
    int x = 0, y = 0;
    for(int i = 0; i < cnt2; ++i) { ///cnt2是强连通分量的个数
        if(in[i] == 0) x++;
        if(out[i] == 0) y++;
    }
    printf("%d\n%d\n", x, max(x, y));
    return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值