码迷,mamicode.com
首页 > Web开发 > 详细

【连通图|强连通分量+缩点】POJ-1236 Network of Schools

时间:2015-03-08 00:15:14      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:

Network of Schools
Time Limit: 1000MS Memory Limit: 10000K

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
IOI 1996


题意: 给出一个有向连通图,求(1)至少从几个点出发可以遍历整张图;(2)至少添加几条边可以使该图变成强连通图。
思路: 第一问很简单,强连通分量缩点之后求出缩点后入度为0的点的个数。
第二问,缩点之后存在若干入度为0和出度为0的点,将这些点互相连接起来,就会构成强连通图。因此答案是max{入度为0的点个数,出度为0的点个数}。
P.S. 注意缩点之后只剩下一个点的情况,这个时候入度为0和出度为0的点的个数都是1,但是不需要添加边。

/*
 * ID: j.sure.1
 * PROG:
 * LANG: C++
 */
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <ctime>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <climits>
#include <iostream>
#define PB push_back
#define LL long long
using namespace std;
const int INF = 0x3f3f3f3f;
const double eps = 1e-8;
/****************************************/
const int N = 105, M = N*N;
struct Edge {
    int v, next;
    Edge(){}
    Edge(int _v, int _next):
        v(_v), next(_next){}
}e[M];
int dfn[N], head[N], scc_id[N];
int tot, deep, scc_cnt, n;
int line[M][2], in[N], out[N];
stack <int> s;

void init()
{
    memset(head, -1, sizeof(head));
    memset(dfn, 0, sizeof(dfn));
    memset(in, 0, sizeof(in));
    memset(out, 0, sizeof(out));
    tot = deep = scc_cnt = 0;
}

void add(int u, int v)
{
    e[tot] = Edge(v, head[u]);
    head[u] = tot++;
}

int dfs(int u)
{
    int lowu = dfn[u] = ++deep;
    s.push(u);
    for(int i = head[u]; ~i; i = e[i].next) {
        int v = e[i].v;
        if(!dfn[v]) {
            int lowv = dfs(v);
            lowu = min(lowu, lowv);
        }
        else if(!scc_id[v]) {
            lowu = min(lowu, dfn[v]);
        }
    }
    if(lowu == dfn[u]) {
        scc_cnt++;
        //printf("scc_cnt is %d\n", scc_cnt);
        while(1) {
            int x = s.top(); s.pop();
            scc_id[x] = scc_cnt;//number from 1
            if(x == u) break;
        }
    }
    return lowu;
}

void tarjan()
{
    for(int i = 1; i <= n; i++) {
        if(!dfn[i]) dfs(i);
    }
}

int main()
{
#ifdef J_Sure
    freopen("000.in", "r", stdin);
    //freopen("999.out", "w", stdout);
#endif
    scanf("%d", &n);
    init();
    int j, L = 0;
    for(int i = 1; i <= n; i++) {
        while(scanf("%d", &j), j) {
            add(i, j);
            line[L][0] = i; line[L][1] = j;
            L++;
        }
    }
    tarjan();
    if(scc_cnt == 1) {
        printf("1\n0\n");
        return 0;
    }
    for(int i = 0; i < L; i++) {
        int u = scc_id[line[i][0]], v = scc_id[line[i][1]];
        if(u != v) {
            in[v]++;
            out[u]++;
        }
    }
    int noIn = 0, noOut = 0;
    for(int i = 1; i <= scc_cnt; i++) {
        if(!in[i]) noIn++;
        if(!out[i]) noOut++;
    }
    printf("%d\n", noIn);
    printf("%d\n", max(noIn, noOut));
    return 0;
}

【连通图|强连通分量+缩点】POJ-1236 Network of Schools

标签:

原文地址:http://blog.csdn.net/j_sure/article/details/44122251

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!