强联通分量+缩点

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

题目大意:N个学校之间有单向的网络,每个学校得到一套软件后,可以通过单向网络向周边的学校传输。 
问题1:初始至少需要向多少个学校发放软件,使得网络内所有的学校最终都能得到软件。 
问题2:至少需要添加几条传输线路(边),使任意向一个学校发放软件后,经过若干次传送,网络内所有的学校最终都能得到软件
解法:
把强联通分量压缩成点之后,

找入度为0的点的个数。

找max(入度为0的点,出度为0的点)

这里要注意当只有一个强联通分量时要特判一下。

代码:
 

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <cstdlib>
#include <map>
using namespace std;

/* Tarjan算法
* 复杂度O(N+M) */
const int MAXN = 20010;//点数
const int MAXM = 50010;//边数
struct Edge
{
    int to,next;
}edge[MAXM];

int head[MAXN],tot;
int Low[MAXN],DFN[MAXN],Stack[MAXN],Belong[MAXN];//Belong数组的值是1~scc,belong[1]=2表示1点在第2个强联通分量里。
int Index,top;
int scc;//强连通分量的个数
int Instack[MAXN];
int num[MAXN];//各个强连通分量包含点的个数,数组编号1~scc
//num数组不一定需要,结合实际情况
void addedge(int u,int v)
{
    edge[tot].to = v;edge[tot].next = head[u];head[u] = tot++;
}

void Tarjan(int u) {
    int v;
    Low[u] = DFN[u] = ++Index;  //刚搜到一个节点时Low = DFN;
    Stack[top++] = u;  //将该节点入栈
    Instack[u] = 1;  //将入栈标记设置为1
    for (int i = head[u]; i != -1; i = edge[i].next)
    {
        v = edge[i].to;
        if(!DFN[v])
        {
            Tarjan( v);
            if( Low[u] > Low[v] )Low[u] = Low[v];
        }
        else if(Instack[v] && Low[u] > DFN[v])
            Low[u] = DFN[v];
    }
    if(Low[u] == DFN[u])
    {
        scc++;
        do
        {
            v = Stack[--top];
            Instack[v] = false;
            Belong[v] = scc;
            num[scc ]++;
        }
        while( v != u);
    }
}
void solve(int N)
{
    memset( DFN,0 ,sizeof(DFN));
    memset( Instack,false, sizeof(Instack) );
    memset( num,0 ,sizeof(num));
    Index = scc = top = 0;
    for(int i = 1;i <= N;i++)
        if(!DFN [i])
            Tarjan(i);
}
void init()
{
    tot = 0;
    memset( head, -1,sizeof (head));
}
int out[MAXN],in[MAXN];
map <int,int> mp[MAXN];
int main()
{
    int n;
    while(cin >> n)
    {
        init();
        for(int i = 1; i <= n; i++)
        {
            int num;
            while(cin >> num)
            {
                if (num == 0) break;
                mp[i][num] = 1;
                addedge(i,num);
            }
        }
        solve(n);
        if (scc == 1)
        {
            cout << "1\n0\n";
            continue;
        }
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= n; j++)
            {
                if (mp[i][j] == 1 && Belong[i] != Belong[j])
                {
                    out[Belong[i]]++;
                    in[Belong[j]]++;
                }
            }
        }
        int  ans1 = 0,ans2 = 0;
        for (int i = 1; i <= scc; i++)
        {
            if (in[i] == 0) ans1++;
            if (out[i] == 0) ans2++;
        }
        cout << ans1 << endl << max(ans1,ans2) << endl;
    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值