usaco 5.3 Network of Schools(强连通分量+构造强连通最少边)

来源:互联网 发布:淘宝超过七天怎么退货 编辑:程序博客网 时间:2024/05/21 10:57
Network of Schools
IOI '96 Day 1 Problem 3

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.

PROGRAM NAME: schlnet

INPUT FORMAT

The first line of the input file 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.

SAMPLE INPUT (file schlnet.in)

52 4 3 04 5 0001 0

OUTPUT FORMAT

Your program should write two lines to the output file. The first line should contain one positive integer: the solution of subtask A. The second line should contain the solution of subtask B.

SAMPLE OUTPUT (file schlnet.out)

12

题意:给你n个学校的关系图(学校间相互传递软件),每次要分发一个新软件,最少要发给几个学校,如果要使得随便选一个学校,就能发到所有学校,至少要建立几个新的关系。。。

分析:一开始就想到强连通,但是好久没写,想偷懒,就直接深搜加标记入度出度,一顿乱搞,居然过了10组数据,最后一组实在是骗不过去了T_T

后来就复习了下强连通分量,先把原图强连通缩点,新的图是一片深林,只要统计有几个树根和几个叶子,树根的个数就是第一解,叶子和树根的最大值就是第二解,注意可能只有一个节点,那么第二解为0

PS:再也不敢偷懒了,wa了七次啊= =

代码:

/*ID: 15114582PROG: schlnetLANG: C++*/#include<cstdio>#include<iostream>using namespace std;const int mm=111;const int mn=111111;int in[mm],out[mm],head[mm],dfn[mm],low[mm],q[mm],id[mm];int ver[mn],next[mn];int i,j,n,ans1,ans2,edge,index,top,num;void dfs(int u){    dfn[u]=low[u]=++index;    q[top++]=u;    for(int i=head[u],v;i>=0;i=next[i])        if(!dfn[v=ver[i]])            dfs(v),low[u]=min(low[u],low[v]);        else if(!id[v])low[u]=min(low[u],dfn[v]);    if(dfn[u]==low[u])    {        id[u]=++num;        while(q[--top]!=u)id[q[top]]=num;    }}void tarjan(){    top=index=num=0;    for(int i=0;i<=n;++i)id[i]=dfn[i]=0;    for(int i=1;i<=n;++i)        if(!dfn[i])dfs(i);}int main(){    freopen("schlnet.in","r",stdin);    freopen("schlnet.out","w",stdout);    while(~scanf("%d",&n))    {        ans1=ans2=0;        for(edge=i=0;i<=n;++i)in[i]=out[i]=0,head[i]=-1;        for(i=1;i<=n;++i)            while(scanf("%d",&j),j)                ver[edge]=j,next[edge]=head[i],head[i]=edge++;        tarjan();        for(i=1;i<=n;++i)            for(j=head[i];j>=0;j=next[j])                if(id[i]!=id[ver[j]])                    ++in[id[ver[j]]],++out[id[i]];        for(i=1;i<=num;++i)            ans1+=(!in[i]),ans2+=(!out[i]);        if(num<2)ans2=0;        else ans2=max(ans1,ans2);        printf("%d\n%d\n",ans1,ans2);    }    return 0;}