Network of Schools - POJ 1236 Tarjan 缩点

76 篇文章 0 订阅

Network of Schools
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 11939 Accepted: 4759

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缩点后,入度为0的点就是答案1,入度和入度为0的点的最大值就是需要新修多少个路线。

AC代码如下:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct node
{
    int v,next;
}edge[10010];
int n,Head[110],stack[110],DFN[110],Low[110],Belong[110],tot,scnt,cnt,top,in[110],out[110];
bool instack[110];
void add(int u,int v)
{
    edge[tot].v=v;
    edge[tot].next=Head[u];
    Head[u]=tot++;
}
void Tarjan(int u)
{
    int i,j,k,v;
    DFN[u]=Low[u]=++cnt;
    stack[top++]=u;
    instack[u]=1;
    for(i=Head[u];i!=-1;i=edge[i].next)
    {
        v=edge[i].v;
        if(!DFN[v])
        {
            Tarjan(v);
            Low[u]=min(Low[u],Low[v]);
        }
        else if(instack[v])
          Low[u]=min(Low[u],DFN[v]);
    }
    if(DFN[u]==Low[u])
    {
        scnt++;
        do
        {
            v=stack[--top];
            instack[v]=0;
            Belong[v]=scnt;
        }while(u!=v);
    }
}
int main()
{
    int i,j,k,u,v,ans1,ans2;
    scanf("%d",&n);
    memset(Head,-1,sizeof(Head));
    for(i=1;i<=n;i++)
    {
        while(scanf("%d",&v) && v)
          add(i,v);
    }
    for(i=1;i<=n;i++)
       if(!DFN[i])
         Tarjan(i);
    if(scnt==1)
    {
        printf("1\n0\n");
        return 0;
    }
    for(u=1;u<=n;u++)
       for(j=Head[u];j!=-1;j=edge[j].next)
       {


           v=edge[j].v;
           if(Belong[u]!=Belong[v])
           {
               out[Belong[u]]++;
               in[Belong[v]]++;
           }
       }
    ans1=ans2=0;
    for(i=1;i<=scnt;i++)
    {
        if(in[i]==0)
          ans1++;
        if(out[i]==0)
          ans2++;
    }
    ans2=max(ans1,ans2);
    printf("%d\n%d\n",ans1,ans2);
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值