poj1236——网络学校(强连通)

Network of Schools
Time Limit: 1000MS
Memory Limit: 10000K
Total Submissions: 8244
Accepted: 3270
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就是输出缩点后入度为零点的个数和出度为零的点个数中的较大者。。。

       只要理解了这两点,这道题就是一道很裸的求强连通缩点的题。。。

        建立原图。。。tarjan求强连通分量。。。缩点。。。维护出度和入度。。。

代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
const int N=110;

struct status
{
    int to,next;
}node1[N*N],node2[N*N];
int l=0,n,e=0;
int head[N],first[N],dfsnum[N],low[N];
int stamp,cnt,top,stack[N],belong[N];
bool vis[N];
int ingree[N],outgree[N];

void add1(int u,int v)
{
    node1[l].to=v;
    node1[l].next=head[u];
    head[u]=l++;
}

void add2(int u,int v)
{
    node2[e].to=v;
    node2[e].next=first[u];
    first[u]=e++;
}

void tarjan(int u)
{
    dfsnum[u]=low[u]=++stamp;
    vis[u]=true;
    stack[top++]=u;
    for(int i=head[u];i!=-1;i=node1[i].next)
    {
        int v=node1[i].to;
        if(!dfsnum[v]){tarjan(v);low[u]=min(low[u],low[v]);}
        else if(vis[v])low[u]=min(low[u],dfsnum[v]);
    }
    if(dfsnum[u]==low[u])
    {
        do
        {
            top--;
            vis[stack[top]]=false;
            belong[stack[top]]=cnt;
        }while(stack[top]!=u);
        cnt++;
    }
}


void readdata()
{
    freopen("poj1236.in","r",stdin);
    freopen("poj1236.out","w",stdout);
    scanf("%d",&n);
    memset(head,-1,sizeof(head));
    for(int i=1;i<=n;i++)
    {
        int v;
        while(scanf("%d",&v) && v!=0)
            add1(i,v);
    }
}

void work()
{
    memset(dfsnum,0,sizeof(dfsnum));
    memset(vis,0,sizeof(vis));
    memset(low,0,sizeof(low));
    cnt=top=stamp=0;
    for(int i=1;i<=n;i++)
    if(!dfsnum[i])tarjan(i);
    memset(first,-1,sizeof(first));
    for(int i=1;i<=n;i++)
    {
        for(int j=head[i];j!=-1;j=node1[j].next)
        {
            int v=node1[j].to;
            if(belong[i]!=belong[v])
                add2(belong[i],belong[v]);
        }
    }
    memset(ingree,0,sizeof(ingree));
    memset(outgree,0,sizeof(outgree));
    for(int i=0;i<cnt;i++)
    {
        for(int j=first[i];j!=-1;j=node2[j].next){ingree[node2[j].to]++;outgree[i]++;}
    }
    int sum=0;
    int num=0;
    for(int i=0;i<cnt;i++)
    {
        if(ingree[i]==0)
        {
            sum++;
        }
        if(outgree[i]==0)
        {
            num++;
        }
    }
    if(cnt==1)cout<<1<<endl<<0<<endl;
    else
    {
        cout<<sum<<endl;
        sum>num ? cout<<sum<<endl : cout<<num<<endl;
    }
}

int main()
{
    readdata();
    work();
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值