poj1236 Network of Schools

Network of Schools
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 22317 Accepted: 8773

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

任务1:至少给多少个节点发配任务能够保证任务能传递给所有节点

任务2:至少添加多少条边使得向任意节点传递任务,都能在若干次传递后能分配给所有的节点

求出所有的强连通分量并进行缩点,形成一个有向无环图。

对于任务1,我们找出所有的入度为0的点即可

对于任务2,我们想办法让他们的节点任意两点都能相互到达,即形成一个强连通分量。可以发现在一个强连通分量里,出度和入度多没有为0的,我们可以把出度为0的连接到入度为0的,如果仍有剩余的在随便连接一个节点,所有最终答案应为max(in, out)

#include <algorithm>
#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <stack>
using namespace std;
#define N 200010
#define INF 0x3f3f3f3f
struct Edge{
    int u, v;
    int next;
}edge[N*4];
int dfn[N], low[N], deep;
int belong[N], in[N], out[N];
int head[N], cnt, num;
bool vis[N];
stack <int> st;
int ans1, ans2, n, m;
void addedge(int u, int v)
{
    edge[cnt].u = u;
    edge[cnt].v = v;
    edge[cnt].next = head[u];
    head[u] = cnt ++;

}
void init()
{
    memset(in, 0, sizeof(in));
    memset(out, 0, sizeof(out));
    memset(dfn, 0, sizeof(dfn));
    memset(low, 0, sizeof(low));
    memset(head, -1, sizeof(head));
    memset(vis, false, sizeof(vis));
    cnt = 0;
    num = 0;
    deep = 0;
    ans1 = 0;
    ans2 = 0;
}
void tarjan(int u)
{
    dfn[u] = low[u] = ++deep;
    vis[u] = true;
    st.push(u);
    for(int i = head[u]; ~i; i = edge[i].next) {
        int v = edge[i].v;
        if(!dfn[v]) {
            tarjan(v);
            low[u] = min(low[u], low[v]);
        } else if(vis[v]) {
            low[u] = min(low[u], dfn[v]);
        }
    }
    if(dfn[u] == low[u]) {
        ++ num;
        while(1) {
            int now = st.top();
            st.pop();
            vis[now] = false;
            belong[now] = num;
            if(now == u)
                break;
        }
    }
}
void solve()
{
    for(int i = 1; i <= n; ++i) {
        if(!dfn[i])
            tarjan(i);
    }
    if(num == 1) {
        printf("1\n0\n");
        return ;
    }
    
    for(int i = 1; i <= n; ++i) {
        for(int j = head[i]; ~j; j = edge[j].next) {
            int v = edge[j].v;
            if(belong[i] != belong[v]) {
                out[belong[i]] ++;
                in[belong[v]] ++;
            }
        }
    }
    /*
    for(int i = 0; i < cnt; ++i) {
        int u = edge[i].u;
        int v = edge[i].v;
        if(belong[u] != belong[v]) {
            out[belong[u]] ++;
            in[belong[v]] ++;
        }
    }
    */
    for(int i = 1; i <= num; ++i) {
        if(in[i] == 0)
            ans1 ++;
        if(out[i] == 0)
            ans2 ++;
    }
    printf("%d\n%d\n", ans1, max(ans1, ans2));
}
int main()
{

    while(scanf("%d", &n) != EOF) {
        int t;
        init();
        for(int i = 1; i <= n; ++i) {
            while(scanf("%d", &t), t != 0) {
                addedge(i, t);
            }
        }
        solve();
    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值