poj 1236 Network of Schools(强连通缩点)

题目链接

Network of Schools
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 11651 Accepted: 4637

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

题意:n个点的有向图,问最少以多少个点为起点,可以访问完整个图?问最少加多少条边可以让整个图强连通?

题解:强连通缩点后,第一个问题的答案显然就是入度为0的点的个数。第二个问题的答案就是max(入读为0的点的个数,出度为0的点的个数)。当然要特判图本身就是强连通的情况。

对于第2个问题的简要证明:

假设把边看成无向边, 能连通的点看成一个连通块。

那么如果连通块的个数为1,将出度为0的点向入读为0的点连边,并且让所有出度为0的点出度不为0,让所有入度为0的点入度不为0,那么这样图一定变成强连通的。

对于连通块的个数大于1的情况,将出度为0的点尽量连向非本连通块的入度为0的点,并且让所有出度为0的点出度不为0,让所有入度为0的点入度不为0,那么这样图也一定是强连通的。

换句话说:如果一个有向图所有点的入度和出度都不为0,我们可以在每个点出度入度不改变的情况下,让这个图变为强连通。

证明:如果有两个强连通分量,两个极大强连通分量里的点的入度和出度都不为0。假设极大两个强连通分量依次为a,b。假设a中的一条边为(a1->a2),b中的一条边为(b1->b2)。若我们删除这两边,新建两条边a1->b2,b1->a2。这样图中所有的点入度和出度是没有变化的。但是新图的极大强连通分量合成了一个。

代码如下:

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<math.h>
#include<stack>
#define nn 110
#define inff 0x3fffffff
using namespace std;
int n;
struct node
{
    int en,next;
}E[nn*nn];
int p[nn],num;
void init()
{
    memset(p,-1,sizeof(p));
    num=0;
}
void add(int st,int en)
{
    E[num].en=en;
    E[num].next=p[st];
    p[st]=num++;
}
int dfn[nn],low[nn];
int df;
bool insta[nn];
stack<int>sta;
int cnt;
int fa[nn];
void dfs(int id)
{
    dfn[id]=low[id]=++df;
    insta[id]=true;
    sta.push(id);
    int i,w;
    for(i=p[id];i+1;i=E[i].next)
    {
        w=E[i].en;
        if(dfn[w]==-1)
        {
            dfs(w);
            low[id]=min(low[id],low[w]);
        }
        else if(insta[w])
            low[id]=min(low[id],dfn[w]);
    }
    if(dfn[id]==low[id])
    {
        ++cnt;
        int ix;
        while(1)
        {
            ix=sta.top();
            sta.pop();
            insta[ix]=false;
            fa[ix]=cnt;
            if(ix==id)
                break;
        }
    }
}
int cd[nn],rd[nn];
void solve()
{
    int i,j,w;
    memset(cd,0,sizeof(cd));
    memset(rd,0,sizeof(rd));
    memset(dfn,-1,sizeof(dfn));
    memset(insta,false,sizeof(insta));
    df=0;
    cnt=0;
    for(i=1;i<=n;i++)
    {
        if(dfn[i]==-1)
        {
            dfs(i);
        }
    }
    for(i=1;i<=n;i++)
    {
        for(j=p[i];j+1;j=E[j].next)
        {
            w=E[j].en;
            if(fa[w]!=fa[i])
            {
                cd[fa[i]]++;
                rd[fa[w]]++;
            }
        }
    }
}
int main()
{
    int i,u;
    while(scanf("%d",&n)!=EOF)
    {
        init();
        for(i=1;i<=n;i++)
        {
           while(scanf("%d",&u)&&u)
           {
               add(i,u);
           }
        }
        solve();
        int ans1,ans2;
        ans1=ans2=0;
        for(i=1;i<=cnt;i++)
        {
            if(rd[i]==0)
                ans1++;
            if(cd[i]==0)
                ans2++;
        }
        printf("%d\n",ans1);
        if(cnt==1)
            ans1=ans2=0;
        printf("%d\n",max(ans1,ans2));
    }
    return 0;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值