POJ1236 Network of Schools(SCC)

题目链接 : http://poj.org/problem?id=1236

题目

Network of Schools

Time Limit: 1000MS
Memory Limit: 10000K
Total Submissions: 19658

Accepted: 7744

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个学校通过单向的网络连接,软件可以沿着单向网络传递,第一问:若要传递一个软件,最少需要复制多少份给学校才能使得所有学校都能得到软件。第二问:想要把一个软件只给任意一个学校就能把软件传递给所有学校,最少需要添加几条网络线路。

题解:

任何一个学校,如果有学校指向他,那么,只需把软件给指向他的学校即可,而如果处于一个环中,那么给任意一个学校就可以,如果有学校指向这个环,那么给这个学校就可以完成这个学校加这个环内的所有学校,以此可见,找出所有强连通分量,如果某一个强连通分量的入度为零,则没有学校给这个分量软件,那么就需要最初给他,最终,第一问也就转化成了求入度为零的强连通分量的个数。

第二问,任意一个学校可以到达其他所有学校,也就是最少添加几条边可以是这个图成为强连通图,出度为0的强连通分量必须添加边,使这个分量的学校得以传递软件,入度为0的分量需要添加边指向这个分量,使得软件可以传递到这个分量,所以,使出度为0的分量指向入度为0的分量可以使到达的学校尽可能的多。因为要把所有入度为0的分量和出度为0的分量添加边,恰好这两个可以相互满足,那么只需要找到入度为0的分量的数量和出度为0的分量的数量中的较大值就是要添加的边的数量了。

注意,当题目原本就是一个强连通图的时候,这个图的入度和出度都为0,按照上面的做法需要添加一条边,但实际不需要,所以需要特判。

引用Kuangbin的解释
在DAG上要加几条边,才能使得DAG变成强连通的,问题2的答案就是多少
加边的方法:
要为每个入度为0的点添加入边,为每个出度为0的点添加出边
假定有 n 个入度为0的点,m个出度为0的点,如何加边?
把所有入度为0的点编号 0,1,2,3,4 ….N -1
每次为一个编号为i的入度0点可达的出度0点,添加一条出边,连到编号为(i+1)%N 的那个出度0点,
这需要加n条边
若 m <= n,则
加了这n条边后,已经没有入度0点,则问题解决,一共加了n条边
若 m > n,则还有m-n个入度0点,则从这些点以外任取一点,和这些点都连上边,即可,这还需加m-n条边。
所以,max(m,n)就是第二个问题的解
此外:当只有一个强连通分支的时候,就是缩点后只有一个点,虽然入度出度为0的都有一个,但是实际上不需要增加清单的项了,所以答案是1,0;

用数组模拟stack要比stl慢,谜。

代码

#include <iostream>
#include <cstring>
#include <cstdio>
#include <stack>
using namespace std;
typedef pair<int, int> P;
const int N = 105;
const int M = N*N;
const int INF = 0x3f3f3f3f;
struct edge {
     int to, next;
}graph[M];
int totlen;
int head[N];
int n, m;
void init() {
     totlen = 0;
     memset (head, -1, sizeof head);
}
void addEdge (int u, int v) {
     graph[totlen] = {v, head[u]};
     head[u] = totlen++;
}
int low[N];
int dfn[N];
stack<int> sta;
int belong[N];
bool instack[N];
int Bindex, Bcnt, top;
void tarjan(int u) {
    low[u] = dfn[u] = ++Bindex;
    sta.push(u);
    instack[u] = true;
    for (int i = head[u]; i != -1; i = graph[i].next) {
    int v = graph[i].to;
    if(!dfn[v]) {
         tarjan(v);
         low[u] = min(low[u], low[v]);
    }else if(instack[v]) {
         low[u] = min(low[u], dfn[v]);
    }
}
    if(low[u] == dfn[u]) {
        ++Bcnt;
        int tmp;
        do {
            tmp = sta.top(); sta.pop();
            instack[tmp] = false;
            belong[tmp] = Bcnt;
        } while (tmp != u);
    }
}
void SCC() {
    Bindex = Bcnt = 0;
    memset(dfn, 0, sizeof dfn);
    for(int i = 1; i <= n; i++)
    if(!dfn[i])
         tarjan(i);
}
int main() {
    init();
    scanf("%d", &n);
    for(int i = 1; i <= n; i++) {
    int a;
    while(scanf("%d", &a) && a) {
         addEdge(i, a);
    }
    }
    SCC();
    if(Bcnt == 0) {
            printf("1\n0");
    } else {
    int indu[N];
    int outdu[N];
    memset(indu, 0, sizeof indu);
    memset(outdu, 0, sizeof outdu);
    for (int u = 1; u <= n; u++) {
            for (int i = head[u]; ~i; i = graph[i].next) {
                 if(belong[graph[i].to] != belong[u]) {
                      outdu[belong[u]]++;
                      indu[belong[graph[i].to]]++;
                 }
             }
    }
    int inzero = 0;
    int outzero = 0;
    for(int i = 1; i <= Bcnt; i++) {
            if (indu[i] == 0)
                 inzero++;
            if (outdu[i] == 0)
                 outzero++;
    }
    printf("%d\n%d", inzero, max(inzero, outzero));
      }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值