usaco5.3.3 Network of Schools

一 原题

Network of Schools
IOI '96 Day 1 Problem 3

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.

PROGRAM NAME: schlnet

INPUT FORMAT

The first line of the input file 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.

SAMPLE INPUT (file schlnet.in)

5
2 4 3 0
4 5 0
0
0
1 0

OUTPUT FORMAT

Your program should write two lines to the output file. The first line should contain one positive integer: the solution of subtask A. The second line should contain the solution of subtask B.

SAMPLE OUTPUT (file schlnet.out)

1
2



二 分析

给定一个有向图,求解:(1)至少需要指定多少个点,使得图中所有顶点都从这些点可达;(2)至少需要增加几条边,使得整个图成为一个强连通图。
很容易想到将原图进行强连通分量缩点,两问的答案分别对应新图中:(1)入度为0的点的数量;(2)max(入度为0的点数,出度为0的点数)。
求强连通分量我实用的是tarjan算法,复杂度O(|V| + |E|)


三 代码

运行结果:
USER: Qi Shen [maxkibb3]
TASK: schlnet
LANG: C++

Compiling...
Compile: OK

Executing...
   Test 1: TEST OK [0.000 secs, 4208 KB]
   Test 2: TEST OK [0.000 secs, 4208 KB]
   Test 3: TEST OK [0.000 secs, 4208 KB]
   Test 4: TEST OK [0.000 secs, 4208 KB]
   Test 5: TEST OK [0.000 secs, 4208 KB]
   Test 6: TEST OK [0.000 secs, 4208 KB]
   Test 7: TEST OK [0.000 secs, 4208 KB]
   Test 8: TEST OK [0.000 secs, 4208 KB]
   Test 9: TEST OK [0.000 secs, 4208 KB]
   Test 10: TEST OK [0.000 secs, 4208 KB]
   Test 11: TEST OK [0.000 secs, 4208 KB]

All tests OK.

Your program ('schlnet') produced all correct answers! This is your submission #3 for this problem. Congratulations!


AC代码:
/*
ID:maxkibb3
LANG:C++
PROB:schlnet
*/

#include<cstdio>
#include<vector>
#include<stack>
#include<algorithm>
using namespace std;

const int MAXV = 105;

int N;
bool Map[MAXV][MAXV];
vector< int > E[MAXV];

/*** Global Variables for Tarjan ***/
int Cnt;
int Index[MAXV];
int Lowlink[MAXV];
bool Instack[MAXV];
stack< int > S;
vector< vector<int> > Sc;
bool Edges[MAXV][MAXV];

void init() {
    int tmp;
    scanf("%d", &N);
    for(int i = 1; i <= N; i++)
        while(true) {
            scanf("%d", &tmp);
            if(tmp == 0) break;
            E[i].push_back(tmp);
            Map[i][tmp] = true;
        }
}

void tarjan(int _idx) {
    if(Index[_idx] != -1) return;
    Index[_idx] = Lowlink[_idx] = Cnt++;
    S.push(_idx);
    Instack[_idx] = true;

    for(int i = 0; i < E[_idx].size(); i++) {
        int next = E[_idx][i];
        if(Index[next] == -1) {
            tarjan(next);
            if(Lowlink[next] < Lowlink[_idx])
                Lowlink[_idx] = Lowlink[next];
        }
        else if(Instack[next] && 
                Lowlink[_idx] > Index[next]) {
            Lowlink[_idx] = Index[next];
        }
    }
   
    if(Index[_idx] == Lowlink[_idx]) {
        vector<int> new_sc;
        int top_ele;
        do {
            top_ele = S.top();
            new_sc.push_back(top_ele);
            Instack[top_ele] = false;
            S.pop();
        }
        while(top_ele != _idx);
        Sc.push_back(new_sc);
    }
}

void build_graph() { 
    int ssize = Sc.size();
    for(int i = 0; i < ssize; i++)
    for(int j = i + 1; j < ssize; j++)
    for(int k = 0; k < Sc[i].size(); k++)
    for(int l = 0; l < Sc[j].size(); l++) {
        if(Map[Sc[i][k]][Sc[j][l]])
            Edges[i][j] = true;
        if(Map[Sc[j][l]][Sc[i][k]])
            Edges[j][i] = true;
    }
}

void count() {
    if(Sc.size() == 1) {
        printf("1\n0\n");
        return;
    }
    int in_ele, out_ele;
    int in_zero = 0, out_zero = 0;
    for(int i = 0; i < Sc.size(); i++) {
        in_ele = out_ele = 0;
        for(int j = 0; j < Sc.size(); j++) {
            if(Edges[i][j])
                out_ele++;
            if(Edges[j][i])
                in_ele++;
        }
        if(in_ele == 0) in_zero++;
        if(out_ele == 0) out_zero++;
    }
    printf("%d\n%d\n", in_zero, max(in_zero, out_zero));
}

void solve() {
    for(int i = 1; i <= N; i++)
        Index[i] = -1;
    for(int i = 1; i <= N; i++)
        tarjan(i);
    build_graph();
    count();
}

int main() {
    freopen("schlnet.in", "r", stdin);
    freopen("schlnet.out", "w", stdout);
    init();
    solve();
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值