poj1236 Network of Schools(强连通-缩点)

来源:互联网 发布:淘宝怎么设置支持花呗 编辑:程序博客网 时间:2024/06/02 21:13

Network of Schools

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 20000/10000K (Java/Other)
Total Submission(s) : 2   Accepted Submission(s) : 1
Problem 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
/*题目大意:输入一个N,从1到N表示出度,输入每个 i 有m个点可以到达;求选最少的点能到达所有的点,以及找加最少的的边数使整个图变成强连通图关键点:最少的点就是缩点后的入度,而最少的加最少的边,是缩点后入度为0的与出度为0 的中取最大值;具体证明不清楚心得:第一次写好不知道什么原因一直出错,有重写了一次,还好能输出测试数据,wa了一次,突然发现wa了好多次之后a掉是多么激动Time:2014-8-16 23:39*/#include<cstring>#include<cstdio>#include<algorithm>using namespace std;const int MAXN=10000010;const int MAX=110;struct Edge{int to;int next;}edge[MAXN];int N;int head[MAX];int dfn[MAX],low[MAX],stack[MAX],belong[MAX];bool instack[MAX];int in[MAX],out[MAX];int edgeNum,cnt,index,Top; void Add(int a,int b){edge[edgeNum].to=b;edge[edgeNum].next=head[a];head[a]=edgeNum++;}void Init(){Top=edgeNum=index=cnt=0;memset(head,-1,sizeof(head));memset(in,0,sizeof(in));memset(out,0,sizeof(out)); memset(belong,0,sizeof(belong));memset(dfn,0,sizeof(dfn));memset(low,0,sizeof(low));memset(instack,0,sizeof(instack));memset(stack,0,sizeof(stack));for(int i=1;i<=N;i++){int M;while(scanf("%d",&M),M){Add(i,M);}}}void Tarjan(int u){dfn[u]=low[u]=++index;stack[++Top]=u;instack[u]=true;for(int i=head[u];~i;i=edge[i].next){int v=edge[i].to;if(dfn[v]==0){Tarjan(v);low[u]=min(low[u],low[v]);}else if(instack[v]){low[u]=min(low[u],dfn[v]);}}int v;if(low[u]==dfn[u]){cnt++;do{v=stack[Top--];instack[v]=false;belong[v]=cnt;}while(u!=v);}}void solve(){while(scanf("%d",&N)!=EOF){Init();for(int i=1;i<=N;i++){if(dfn[i]==0)Tarjan(i);}for(int i=1;i<=N;i++){for(int j=head[i];~j;j=edge[j].next){int v=edge[j].to;if(belong[i]!=belong[v]){in[belong[v]]++;out[belong[i]]++;}}}int nIn,nOut;nIn=nOut=0;for(int i=1;i<=cnt;i++){if(in[i]==0)nIn++;if(out[i]==0)nOut++;}printf("%d\n",nIn);if(cnt==1)printf("0\n");else{printf("%d\n",max(nIn,nOut));}}}int main(){solve();return 0;}

0 0
原创粉丝点击