POJ 1236-Network of Schools(强连通分量tarjan)

Network of Schools
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 22159 Accepted: 8722

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台电脑,每台电脑连接着其他电脑,有向的传递信息,问你:1、有至少要用多少台电脑才能使得使得发布一条信息其他电脑都能收到着条信息。2、需要加多少条边才能使得通过任何一台电脑发布一条信息其他电脑都能收到,也就是使得图成为一个强连通图。

强连通分量:tarjan算法详解

解题思路:先运用tarjan算法进行缩点,然后再统计缩点后每个点的入度和出度,设最后求得入度为0的点个数为n,出度为0的点的个数为m,那么答案就是n, max(n, m)。

AC代码:
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<string>
#include<math.h>
#include<stdlib.h>
#include<queue>
#include<map>
#include<set>
#include<stack>
#define bug printf("*********\n");
#define mem0(a) memset(a, 0, sizeof(a));
#define mem1(a) memset(a, -1, sizeof(a));
#define finf(a, n) fill(a, a+n, INF);
#define in1(a) scanf("%d" ,&a);
#define in2(a, b) scanf("%d%d", &a, &b);
#define in3(a, b, c) scanf("%d%d%d", &a, &b, &c);
#define out1(a) printf("%d\n", a);
#define out2(a, b) printf("%d %d\n", a, b);
#define pb(G, a, b) G[a].push_back(b);
using namespace std;
typedef long long LL;
typedef pair<int, int> par;
const int mod = 1e9+7;
const int INF = 1e9+7;
const int N = 1000010;
const double pi = 3.1415926;

int n, cnt, idx, scc;
int head[110], dfn[110], low[110], instack[110], belong[110], in[110], out[110];
stack<int> st;

struct edge
{
    int to;
    int next;
}e[110*110];

void add(int u, int v)
{
    e[cnt].to = v;
    e[cnt].next = head[u];
    head[u] = cnt ++;
}

void tarjan(int k)
{
    dfn[k] = low[k] = idx ++;
    instack[k] = 1;
    st.push(k);
    for(int i = head[k]; i != -1; i = e[i].next) {
        int en = e[i].to;
        if(!dfn[en]) {
            tarjan(en);
            low[k] = min(low[en], low[k]);
        }else if(instack[en]){
            low[k] = min(low[k], dfn[en]);
        }
    }
    if(dfn[k] == low[k]) {
        scc ++;
        int v;
        do {
            v = st.top();
            st.pop();
            instack[v] = 0;
            belong[v] = scc;
        }while(k != v);
    }
}

void solve()
{
    while(!st.empty()) st.pop();
    for(int i = 1; i <= n; i ++) {
        if(!dfn[i])
            tarjan(i);
    }
    if(scc == 1) {
        printf("1\n0\n");
        return;
    }
    for(int i = 1; i <= n; i ++) {
        for(int j = head[i]; j != -1; j = e[j].next) {
            int en = e[j].to;
            if(belong[i] != belong[en]) {
                out[belong[i]] ++;
                in[belong[en]] ++;
            }
        }
    }
    int a = 0;
    int b = 0;
    for(int i = 1; i <= scc; i ++) {
        if(out[i] == 0) b ++;
        if(in[i] == 0) a ++;
    }
    printf("%d\n%d\n", a, max(a, b));
}

int main()
{
    int k;
    while(~scanf("%d", &n)) {
        mem0(dfn);
        mem0(low);
        mem1(head);
        mem0(in);
        mem0(out);
        mem0(belong);
        cnt = scc = 0;
        idx = 1;
        for(int i = 1; i <= n; i ++) {
            while(scanf("%d", &k) && k) {
                add(i, k);
            }
        }
        solve();
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值