poj1236-network of schools(强连通分量)

来源:互联网 发布:java虚拟主机管理系统 编辑:程序博客网 时间:2024/05/17 04:20
Network of Schools
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 8137 Accepted: 3221

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

52 4 3 04 5 0001 0

Sample Output

12
求出强连通分量,问题A要找出入度为零的点的个数,问题B要求出度为零和入度为零的个数中大的那个,用tarjan算法求出强连通分量,然后遍历一遍就行了,写的时候各种错误……
#include<stdio.h>#include<vector>#include<stack>#include<string.h>using namespace std;vector<int> vec[105];int dfn[105],low[105];int vis[105];int in[105],out[105];int gro_id[105],gro[105];stack<int> sta;int n,now,id;void tarjan(int s){    vis[s]=2;    dfn[s]=low[s]=++now;    sta.push(s);    for(int i=0; i<vec[s].size(); i++)    {        if(vis[vec[s][i]]==0)        {            tarjan(vec[s][i]);            low[s]=low[s]<low[vec[s][i]]?low[s]:low[vec[s][i]];        }        else if(vis[vec[s][i]]==2)            low[s]=low[s]<dfn[vec[s][i]]?low[s]:dfn[vec[s][i]];    }    if(low[s]==dfn[s])    {        id++;        while(1)        {            int t=sta.top();            gro_id[t]=id;            vis[t]=1;            sta.pop();            gro[id]++;            if(t==s)                break;        }    }}int main(){    now=id=0;    memset(vis,0,sizeof(vis));    memset(dfn,0,sizeof(dfn));    memset(low,0,sizeof(low));    memset(gro_id,0,sizeof(gro_id));    memset(gro,0,sizeof(gro));    scanf("%d",&n);    for(int i=1; i<=n; i++)    {        int a;        vec[i].clear();        while(scanf("%d",&a)&&a!=0)            vec[i].push_back(a);    }    for(int i=1; i<=n; i++)        if(vis[i]==0)            tarjan(i);    memset(in,0,sizeof(in));    memset(out,0,sizeof(out));    for(int i=1; i<=n; i++)        for(int j=0; j<vec[i].size(); j++)            if(gro_id[i]!=gro_id[vec[i][j]])            {                out[gro_id[i]]++;                in[gro_id[vec[i][j]]]++;            }    int chu=0,ru=0;    //printf("%d\n",id);    for(int i=1; i<=id; i++)    {        if(out[i]==0)            chu++;        if(in[i]==0)            ru++;    }    if(id==1)        printf("%d\n0\n",ru);    else        printf("%d\n%d\n",ru,chu>ru?chu:ru);    return 0;}

原创粉丝点击