poj 1236 Network of Schools(强连通分量 Tarjan)

Network of Schools
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 12080 Accepted: 4811

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

Source


对于学校的网络的 给出两个任务

任务A:计算为使得每个学校都能通过网络收到软件,你至少需要准备多少份软件拷贝。

缩点之后 找出入度为0的点的个数为ans1 即从这些点 可以到达其他所有的点 


任务B:考虑一个更长远的任务,想确保给任意一个学校发放一个新的软件拷贝,该软件拷
贝能发布到网络中的每个学校。为了达到这个目标,必须在列表中增加新成员。计算需要添加新
成员的最小数目。

统计DAG中出度为0的点的个为ans2  然后取ans1 ans2的大值

另外有一个特殊情况 就是只有一个缩点时 输出1 0

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <string>
#include <vector>
#include <queue>

#define MEM(a,x) memset(a,x,sizeof a)
#define eps 1e-8
#define MOD 10009
#define INF 99999999
#define ll __int64
#define bug cout<<"here"<<endl
#define fread freopen("ceshi.txt","r",stdin)
#define fwrite freopen("out.txt","w",stdout)

using namespace std;

void read(int &x)
{
    char ch;
    x=0;
    while(ch=getchar(),ch!=' '&&ch!='\n')
    {
        x=x*10+ch-'0';
    }
}

const int MAXN=5010;
const int MAXM=50010;
struct Edge
{
    int to,next;
}edge[MAXM];
int head[MAXN],tot;
int low[MAXN],dfn[MAXN],sta[MAXN],belong[MAXN];
int index,top;
int scc;
bool instack[MAXN];
int num[MAXN];
int n,m;

void addedge(int u,int v)
{
    edge[tot].to=v; edge[tot].next=head[u]; head[u]=tot++;
}

void Tarjan(int u)
{
//    bug;
    int v;
    low[u]=dfn[u]=++index;
    sta[top++]=u;
    instack[u]=1;
    for(int i=head[u];i!=-1;i=edge[i].next)
    {
//        bug;
        v=edge[i].to;
        if(!dfn[v])
        {
            Tarjan(v);
            low[u]=min(low[u],low[v]);
        }
        else if(instack[v])
            low[u]=min(low[u],dfn[v]);
    }
//    bug;
    if(low[u]==dfn[u])
    {
        scc++;
        do{
            v=sta[--top];
            instack[v]=0;
            belong[v]=scc;
            num[scc]++;
        }while(u!=v);
    }
}

int cd[MAXN],rd[MAXN];
void solve()
{
//    bug;
    MEM(dfn,0);
    MEM(instack,0);
    MEM(cd,0); MEM(rd,0);
    MEM(num,0);
    index=scc=top=0;
    for(int i=1;i<=n;i++)
        if(!dfn[i])
            Tarjan(i);
    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])
            {
                cd[belong[u]]++;
                rd[belong[v]]++;
            }
        }
    }
    int ans1=0,ans2=0;
    for(int i=1;i<=scc;i++)
        if(rd[i]==0) ans1++;
    for(int i=1;i<=scc;i++)
        if(cd[i]==0) ans2++;
    if(scc==1)
        printf("1\n0\n");
    else
        printf("%d\n%d\n",ans1,max(ans1,ans2));
}
void init()
{
    tot=0;
    MEM(head,-1);
}


int main()
{
//    fread;
    while(scanf("%d",&n)!=EOF)
    {
        init();
        for(int i=1;i<=n;i++)
        {
            int v;
            while(1)
            {
                scanf("%d",&v);
                if(v==0)  break;
                addedge(i,v);
            }
        }
        solve();
    }
    return 0;
}

还有  我也很想知道 第二个答案值来源的证明 如果知道的 欢迎分享  





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值