POJ-1236(Tarjin)

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

IOI 1996

题目大意:一些学校可以给某些学校发消息,然后问我们至少需要给几个学校发消息才能让所有学校都收到(答案A),要至少增加多少条边才能随便发一条消息可以让所有学校都收到(答案B)。
解题思路:可以看出,在一个环中的点是互相可以到达的,然后我们将一个环缩成一个点,然后再求一次联通图,此时入度为0的点的个数就是我们要求的答案A,最后我们求出来一个图,这个图有一些入度为0的点和一些出度为0的点,而我们需要得到的图是一个更大的环,所以此时我们的答案B就是得到没有入度为0的点和出度为0的点的图要增加的最小边,其实我们可以很容易的想到了,如果只有一个入度为0的点和一个出度为0的点,我们只需要将这两个点连在一其就可以了,所以答案就是1,而如果有多个入度为0的点和出度为0的点,我们要得到一个最大的环,这时我们使用tarjin算法最后得到的连通图是x条链,那么我们把这x条链首尾串连就可以啦。所以答案B就是入度为0的点的个数和出度为0的点的个数的最大值。
代码:

#include <bits/stdc++.h>
using namespace std;
const int maxn=5e5+7;
const int maxm=4e5+7;
struct edge
{
    int v,next;
}e[maxm];
int cnt,head[maxn],dfn[maxn],low[maxn],tot,vis[maxn],num[maxn],st[maxn],top,col,n,m;
void add(int a,int b)
{
    e[++cnt]=edge{b,head[a]};
    head[a]=cnt;
}
void tarjin(int u)
{
    dfn[u]=low[u]=++tot;
    st[++top]=u;
    for(int i=head[u];i;i=e[i].next){
        int v=e[i].v;
        if(!dfn[v]){
            tarjin(v);
            low[u]=min(low[u],low[v]);
        }
        else if(!vis[v]) low[u]=min(low[u],dfn[v]);
    }
    if(low[u]==dfn[u]){
        num[u]=++col;
        vis[u]=1;
        while(st[top]!=u){
            int v=st[top];
            num[v]=col;
            vis[v]=1;
            top--;
        }
        top--;
    }
}
int in[maxn],out[maxn];
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        int x;
        while(scanf("%d",&x) && x){
            add(i,x);
        }
    }
    for(int i=1;i<=n;i++){
        if(!dfn[i])tarjin(i);
    }
    for(int i=1;i<=n;i++){
        for(int j=head[i];j;j=e[j].next){
            int v=e[j].v;
            if(num[i]!=num[v])in[num[v]]++,out[num[i]]++;
        }
    }
    if(col==1){
        printf("1\n0\n");
        return 0;
    }
    int ans1=0,ans2=0;
    for(int i=1;i<=col;i++){
        if(!in[i])ans1++;
        if(!out[i])ans2++;
    }
    //cout<<ans1<<' '<<ans2<<endl;
    ans2=max(ans1,ans2);
    printf("%d\n%d\n",ans1,ans2);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值