POJ 1236 Network of Schools

Network of Schools
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 22051   Accepted: 8680

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个点,给出与这5个点相邻的点,让你求两个问题:1.有多少个点能到达所有的点 2.要增加多少条边才能使得每个点都能到达其他的点

我们先将图建好,然后跑一遍tarjan,求得联通分量后进行缩点,求得每个点的出度入度,那么第一个问题就是求入度为0的联通分量里有多少个点,第二个问题就是求max(入度为0的点,出度为0的点)。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<stack>
#include<vector>
using namespace std;
#define DEBUG cout<<"hello"<<endl;
const int maxn=110;
struct Node
{
    int to;
    int next;
}edge[10010];
stack<int> sta;
int cnt,index,scc_cnt;
int in[maxn];
int out[maxn];
int head[maxn];
int dfn[maxn];
int low[maxn];
bool vis[maxn];
int belong[maxn];
void add(int u,int v)
{
    edge[cnt].to=v;
    edge[cnt].next=head[u];
    head[u]=cnt++;
    return;
}
void init()
{
    scc_cnt=cnt=index=0;
    memset(in,0,sizeof(in));
    memset(vis,false,sizeof(vis));
    memset(out,0,sizeof(out));
    memset(dfn,0,sizeof(dfn));
    memset(low,0,sizeof(low));
    memset(belong,-1,sizeof(belong));
    memset(head,-1,sizeof(head));
    while(!sta.empty()) sta.pop();
    return;
}
void tarjan(int node)
{
    dfn[node]=low[node]=++index;
    vis[node]=true;
    sta.push(node);
    for(int i=head[node];~i;i=edge[i].next)
    {
        int v=edge[i].to;
        if(!dfn[v])
        {
            tarjan(v);
            low[node]=min(low[node],low[v]);
        }
        else if(vis[v])
        {
            low[node]=min(low[node],dfn[v]);
        }
    }
    if(dfn[node]==low[node])
    {
        int v=-1;
        ++scc_cnt;
        while(node!=v)
        {
            v=sta.top();
            sta.pop();
            belong[v]=scc_cnt;
            vis[v]=false;
        }
    }
    return;
}
int main()
{
    int n;
    while(cin>>n)
    {
        init();
        for(int i=1;i<=n;i++)
        {
            while(true)
            {
                int num;
                cin>>num;
                if(!num) break;
                add(i,num);
            }
        }
        for(int i=1;i<=n;i++)
        {
            if(!dfn[i])
            {
                tarjan(i);
            }
        }
        for(int node=1;node<=n;node++)
        {
            for(int i=head[node];~i;i=edge[i].next)
            {
                int v=edge[i].to;
                if(belong[node]!=belong[v])
                {
                    in[belong[v]]++;
                    out[belong[node]]++;
                }
            }
        }
        int ansa=0,ansb=0;
        for(int i=1;i<=scc_cnt;i++)
        {
            ansa+=(in[i]==0);
            ansb+=(out[i]==0);
        }
        if(scc_cnt==1)
        {
            cout<<1<<endl;
            cout<<0<<endl;
            continue;
        }
        cout<<ansa<<endl;
        cout<<max(ansa,ansb)<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_37943488/article/details/80426317