文章标题 POJ 1236 : Network of Schools (强联通分量+缩点)

来源:互联网 发布:uu大魔王淘宝店铺 编辑:程序博客网 时间:2024/06/05 08:09

Network of Schools

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 ( 2< N < 100)各学校之间有单向的网络,每个学校得到一套软件后,可以通过单向网络向周边的学校传输。
1:初始至少需要向多少个学校发放软件,使得网络内所有的学校最终都能得到软件。
2,至少需要添加几条传输线路(边),使任意向一个学校发放软件后,经过若干次传送,网络内所有的学校最终都能得到软件。

分析:对于问题1,其实也就是在一个有向图里,至少需要选几个点,从这些点能到达全部点;对于问题2,就是在图里面再加几条有向边使得在任意顶点出发,能到达其他顶点。我们先求出强联通分量然后缩点,然后重新建图,此时就是一个有向无环图,然后用ans1保存入度为0 的数目,ans2保存出度为0 的数目,然后对于问题1,就是ans1;对于问题2,就是max(ans1,ans2)
代码:

#include<iostream>#include<string>#include<cstdio>#include<cstring>#include<vector>#include<math.h>#include<map>#include<queue> #include<algorithm>using namespace std;const int inf = 0x3f3f3f3f;typedef pair<int,int> pii;const int maxn = 105;int mp[105][105];vector<int>G[maxn];int belong[maxn];//表示属于哪个联通分量 int dfn[maxn],low[maxn];int ins[maxn],block,id;int st[maxn],tp;int sz[maxn];   //联通分量的数目 int n; int rudu[maxn],chudu[maxn];//缩点完后新的图的出度和入度 void init(){    id=tp=block=0;    memset (low,0,sizeof (low));    memset (dfn,0,sizeof (dfn));    memset (sz,0,sizeof (sz));    memset (rudu,0,sizeof (rudu));    memset (chudu,0,sizeof (chudu));    memset (mp,0,sizeof (mp));}void tarjan(int u){    dfn[u]=low[u]=++id;    ins[u]=1;//表示在栈里面     st[++tp]=u;//进栈     for (int i=0;i<G[u].size();i++){        int v=G[u][i];        if (!dfn[v]){            tarjan(v);            low[u]=min(low[u],low[v]);        }        else if (ins[v])low[u]=min(low[u],dfn[v]);    }     if (low[u]==dfn[u]){        block++;//新的连通分量         int v;        do{            v=st[tp--];            ins[v]=0;            belong[v]=block;            sz[block]++;        }while (u!=v);    }}int main (){       while (scanf ("%d",&n)!=EOF){        init();        for (int i=0;i<=n;i++)G[i].clear();//清空         int tmp;        for (int i=1;i<=n;i++){            while (1){                scanf ("%d",&tmp);                if (tmp==0)break;                mp[i][tmp]=1;//保存边的关系                 G[i].push_back(tmp);//建图             }        }        for (int i=1;i<=n;i++){//求联通分量             if (!dfn[i]){                tarjan(i);            }        }        if (block==1){//如果只有一个连通分量直接输出答案             printf ("1\n");            printf ("0\n");            continue;        }        for (int i=1;i<=n;i++){            for (int j=1;j<=n;j++){                if (mp[i][j]==1){                    if (belong[i]!=belong[j]){//不在同一个联通分量的出度和入度加一                         chudu[belong[i]]++;                        rudu[belong[j]]++;                    }                }            }        }        int ans1=0;        int ans2=0;        //printf ("block=%d\n",block);        for (int i=1;i<=block;i++){            if (rudu[i]==0)ans1++;            if (chudu[i]==0)ans2++;        }        printf ("%d\n",ans1);        printf ("%d\n",max(ans1,ans2));    }    return 0;}
阅读全文
0 0
原创粉丝点击