POJ 1236 Network of Schools

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

学了一下Tarjan算法,是求有向图强连通分量的算法。

如果两个顶点可以相互通达,则称两个顶点强连通(strongly connected)。如果有向图G的每两个顶点都强连通,称G是一个强连通图。有向图的极大强连通子图,称为强连通分量(strongly connected components)。

找强连通分量,缩点,求出每一个强连通分量的入度和出度,入度为0代表这个强连通分量必须单独分配软件,所以入度为0的点的数目就是答案一。

把这个连通图变为强连通图,我纠结了半天,最开始认为答案是强连通数目减一,其实并不是,我们并不需要让两个强连通分量之间双向连接,入度为0代表没有强连通分量可以到达这个强连通分量,出度为0代表这个强连通分量不能到达其他强连通分量,其实只要满足不存在入度为0和出度为0的强连通分量就行了,所以最少添加的数目是入度为0与出度为0数目的最大值。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <set>
#include <map>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <bitset>
#include <string>
#include <vector>
#include <iomanip>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <functional>
#define maxn 100005
#define V    105
#define E    100500
using namespace std;
struct edge
{
        int to, next;
} Edge[E];
int head[V], cnt, n;
int indeg[V], outdeg[V]; //点的入度和出度数
int belong[V], low[V], dfn[V], unum, tar; //dfn[]:遍历到u点的时间; low[]:u点可到达的各点中最小的dfn[v]
bool vis[V];//v是否在栈中
void addedge(int u, int v)
{
        Edge[cnt].to = v;
        Edge[cnt].next = head[u];
        head[u] = cnt++;
}
stack<int> s;
void tarjan(int u)
{
        dfn[u] = low[u] = ++tar;//开始时dfn[u] == low[u]

        s.push(u); //不管三七二十一进栈
        vis[u] = true;
        for (int i = head[u]; i != -1; i = Edge[i].next)
        {
                int v = Edge[i].to;
                if (dfn[v] == 0)//如果v点还未遍历
                {
                        tarjan(v);//向下遍历
                        low[u] = min(low[u], low[v]); //确保low[u]最小
                }
                else if (vis[v] && low[u] > dfn[v])//v在栈中,修改low[u]
                {
                        low[u] = dfn[v];
                }
        }
        if (dfn[u] == low[u])//u为该强连通分量中遍历所成树的根
        {
                unum++;
                while (!s.empty())
                {
                        int v = s.top();
                        s.pop();
                        vis[v] = false;
                        belong[v] = unum;
                        if (v == u)
                        {
                                break;
                        }
                }
        }

}
void init()
{
        unum = 0;
        cnt = 0;
        tar = 0;
        memset(dfn, 0, sizeof(dfn));
        memset(vis, false, sizeof(vis));
        memset(head, -1, sizeof(head));
        while (!s.empty())
        {
                s.pop();
        }
}
void count_deg()
{
        memset(indeg, 0, sizeof(indeg));
        memset(outdeg, 0, sizeof(outdeg));
        for (int u = 1; u <= n; ++u)
                for (int i = head[u]; i != -1; i = Edge[i].next)
                {
                        int v = Edge[i].to;
                        if (belong[u] != belong[v])
                        {
                                indeg[belong[v]]++;
                                outdeg[belong[u]]++;
                        }
                }
}
int main()
{
        while (~scanf("%d", &n))
        {
                init();
                for (int u = 1; u <= n; ++u)
                {
                        int v;
                        while (scanf("%d", &v), v)
                        {
                                addedge(u, v);
                        }
                }
                for (int u = 1; u <= n; ++u)
                {
                        if (dfn[u] == 0)
                        {
                                tarjan(u);
                        }
                }
                if (unum == 1)
                {
                        printf("1\n0\n");
                }
                else
                {
                        count_deg();
                        int inc = 0, outc = 0;
                        for (int i = 1; i <= unum; ++i)
                        {
                                if (indeg[i] == 0)
                                {
                                        inc++;
                                }
                                if (outdeg[i] == 0)
                                {
                                        outc++;
                                }
                        }
                        printf("%d\n%d\n", inc, max(inc, outc));
                }
        }
        return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值