POJ 1236 Network of Schools (Tarjan)

                          Network of Schools
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 22745   Accepted: 8929

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

注意:
代码有误,谨慎观看(仍可AC)

思路:
求最大强连通分量,缩点。算出出度为0和入度为0的点的个数。ans1就是入度为0的点的个数,因为他们不能从其他点获取,而其他的点一定可以从其他点获取。
ans2就是入度为0的点和出度为0的点的个数的最大值,因为你需要把所有的点都弄得有入度和出度
代码
如果我的推理没有错的话,我的代码应该是错的,只是我强行水过去了。因为正常情况下,除非只有一个强连通分量,ans1不会为0,而我测数据测出来0,所有我强行改得符合数据(就是那个max),没想到居然过了
这一切的根源,都是我的Tarjan模板有问题,而HDU竟然让我用那个模板过了一题,看来我要重学Tarjan了。
#include<iostream>
#include<cstring>
#include<vector>
#include<cstdio>
using namespace std;
vector<int>u[108];
int num[108],low[108];
bool book[108];
int index;
int in[108],out[108];
bool flag[108];
void Tarjan_dfs(int t)
{
    index++;
    low[t]=num[t]=index;
    book[t]=true;
    int siz=u[t].size();
    for(int i=0;i<siz;i++){
        if(!book[u[t][i]]){
            Tarjan_dfs(u[t][i]);
        }
        if(!flag[u[t][i]]){low[t]=min(low[t],low[u[t][i]]);}
    }
}

int main()
{
    int n,x;
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        while(true){
            scanf("%d",&x);
            if(x==0){break;}
            u[i].push_back(x);
        }
    }
    for(int i=1;i<=n;i++){
        if(!book[i]){Tarjan_dfs(i);}
        for(int i=1;i<=n;i++){
            if(num[i]!=0){flag[i]=true;}
        }
    }
    for(int i=1;i<=n;i++){
        for(int j=0;j<u[i].size();j++){
            if(low[i]!=low[u[i][j]]){
                out[low[i]]++;
                in[low[u[i][j]]]++;
            }
        }
    }
    int ans1,ans2;
    ans1=ans2=0;
    int numv=0;
    for(int i=1;i<=n;i++){
        if(num[i]==low[i]){
            numv++;
            if(in[low[i]]==0){ans1++;}
            if(out[low[i]]==0){ans2++;}
        }
    }
    if(numv==1){printf("1\n0\n");}
    else printf("%d\n%d\n",max(1,ans1),max(ans2,ans1));
}

 

posted @ 2018-07-30 22:09  断腿三郎  阅读(176)  评论(0编辑  收藏  举报