POJ1236Network of Schools——强连通分量缩点建图

【题目描述】

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

【题目分析】
有一些学校,现在他们之间可以存在发送软件的关系
A. 问最少给几个学校发送软件就可以让所有学校受到软件
B. 问最少加多少条边后给任意学校发送软件所有的学校都可以收到软件
我们进行强连通分量缩点建图之后统计所有入度为0和出度为0的点,入度为0 的点的数目就是A问题的答案。
对于B问题,答案是入度为0的点和出度为0的点的较大值。
原因我在网上没有找到答案,大家都说答案就是较大值,可是我却一直不太能理解。后来觉得可能存在数学证明什么的可以证明这样一定存在一种方式可以将一个DAG变成一个强连通分量。
我自己的理解的话,首先我们将所有出度为0的点连在不能到达该点的入度为0的点上,最后肯定会形成一个环,通过这个环所有的点都可以相互到达,而剩下的入度为0 的点或者出度为0 的点可以随意连在其他点上,这样就将一个DAG变化为一个强连通图
需要注意的是如果原本的图就是一个强连通图就不要加边,需要特判。

【AC代码】

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<climits>
#include<queue>
#include<vector>
#include<set>
#include<map>
using namespace std;

typedef long long ll;
const int MAXN=1e2+5;
const int MAXM=1e4+5;

struct node
{
    int v,next;
}Edge[MAXM];
int head[MAXN],tot;
int DFN[MAXN],LOW[MAXN];
int color[MAXN],cnt;
bool vis[MAXN];
int idx;
int stack[MAXN],top;
int In[MAXN],Out[MAXN];
int n;
int ans1,ans2,ans3;
int Case=0;

void init()
{
    memset(head,0,sizeof(head)); tot=0;
    idx=0; memset(vis,0,sizeof(vis));
    memset(DFN,0,sizeof(DFN));
    memset(color,0,sizeof(color));
    cnt=0; top=0;
    memset(In,0,sizeof(In));
    memset(Out,0,sizeof(Out));
}


void AddEdge(int u,int v)
{
    tot++;
    Edge[tot].v=v; Edge[tot].next=head[u];
    head[u]=tot;
}

void read()
{
    int u,v;

    for(int i=1;i<=n;i++)
    {
        while(~scanf("%d",&v) && v)
        {
            AddEdge(i,v);
        }
    }
}

void Trajan(int x)
{
    int v,tmp;

    DFN[x]=LOW[x]=++idx;
    stack[++top]=x; vis[x]=true;
    for(int i=head[x];i;i=Edge[i].next)
    {
        v=Edge[i].v;
        if(!DFN[v])
        {
            Trajan(v);
            if(LOW[v]<LOW[x]) LOW[x]=LOW[v];
        }
        else if(vis[v] && DFN[v]<LOW[x])
        {
            LOW[x]=DFN[v];  
        }
    }
    if(DFN[x]==LOW[x])
    {
        cnt++;
        do
        {
            tmp=stack[top--];
            vis[tmp]=false;
            color[tmp]=cnt;
        }while (tmp!=x);
    }
}

void solve()
{
    int v;

    for(int i=1;i<=n;i++)
    {
        if(!DFN[i])
        Trajan(i);
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=head[i];j;j=Edge[j].next)
        {
            v=Edge[j].v;
            if(color[i]!=color[v])
            {
                In[color[v]]++;
                Out[color[i]]++;
            }
        }
    }
    ans1=0; ans2=0;
    int minc;
    for(int i=1;i<=cnt;i++)
    {
        if(!In[i])
        {
            ans1++;
        }
        if(!Out[i])
        {
            ans2++;
        }
    }
    if(cnt==1) ans3=0;
    else ans3=max(ans1,ans2);
    printf("%d\n%d\n",ans1,ans3);
}

int main()
{
    
    while(~scanf("%d",&n) && n)
    {
        init();
        read();
        solve();
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值