强连通分量+缩点-poj1236
来源:互联网 发布:专业气象软件 编辑:程序博客网 时间:2024/05/01 02:17
Language:
Network of Schools
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 9796 Accepted: 3879
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.
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缩点:根据belong数组建新图,以前一直以为缩点还要复杂的操作,原来不用。下面是代码:#include<iostream>#include<set>#include<vector>#include<queue>#include<cmath>#include<climits>#include<cstdio>#include<string>#include<cstring>#include<algorithm>typedef long long LL;using namespace std;const int MAX_N=110;int N,num,top,cnt;int map[MAX_N][MAX_N];int dfn[MAX_N],low[MAX_N],pre[MAX_N],belong[MAX_N],stack[MAX_N],instack[MAX_N],cnt_size[MAX_N];int indegree[MAX_N],outdegree[MAX_N];void init(){ memset(map,0,sizeof(map)); memset(instack,0,sizeof(instack)); memset(dfn,0,sizeof(dfn)); memset(cnt_size,0,sizeof(cnt_size)); num=top=cnt=0;}void tarjan(int x){ dfn[x]=low[x]=++num; stack[++top]=x; instack[x]=1; for(int i=1; i<=N; i++) { if(!map[x][i]) continue; if(!dfn[i]) { tarjan(i); low[x]=min(low[x],low[i]); } else if(instack[i]) low[x]=min(dfn[i],low[x]); } if(dfn[x]==low[x]) { cnt++; int j; do { j=stack[top--]; instack[j]=0; belong[j]=cnt; cnt_size[cnt]++; } while(j!=x); }}void output(){ memset(indegree,0,sizeof(indegree)); memset(outdegree,0,sizeof(outdegree)); int inzero=0,outzero=0; for(int i=1; i<=N; i++) for(int j=1; j<=N; j++) { if(map[i][j]&&belong[i]!=belong[j]) { indegree[belong[j]]++; outdegree[belong[i]]++; } } for(int i=1; i<=cnt; i++) { if(!indegree[i]) inzero++; if(!outdegree[i]) outzero++; } if(cnt==1) cout<<1<<'\n'<<0<<endl; else printf("%d\n%d",inzero,max(inzero,outzero));}int main(){ //freopen("in.txt","r",stdin); int x; while(cin>>N) { init(); for(int i=1; i<=N; i++) { while(scanf("%d",&x)!=EOF) { if(x==0) break; map[i][x]=1; } } for(int i=1; i<=N; i++) { if(!dfn[i]) tarjan(i); } output(); } return 0;}
- 强连通分量+缩点-poj1236
- poj1236强连通分量+缩点
- poj1236 强连通分量+缩点
- poj1236(*强连通分量,缩点)
- poj1236 强连通分量——缩点
- POJ1236 Network of Schools(缩点+结论+强连通分量)
- poj1236(强连通分量)
- POJ1236 强连通分量
- POJ1236强连通+缩点
- POJ1236强连通分量tarjan
- poj1236强连通分量(Tarjan)
- [POJ1236]Network of Schools(Tarjan缩点+强连通分量)
- poj1236——Network of Schools(强连通分量+缩点)
- poj1236 - Network of Schools(强连通分量Garbow模板+缩点)
- poj1236 Network of Schools--Kosaraju算法 & 缩点 & 强连通分量
- POJ1236---Network of Schools (强连通分量,缩点,Tarjan算法)
- Network of Schools POJ1236(tarjan缩点+强连通分量模板)
- 【强连通分量】Tarjan(缩点)POJ1236-Network of Schools
- (DP6.1.2.1)UVA 147 Dollars(子集和问题)
- C语言与汇编混合编程
- phpStudy无法打开http://localhost/
- Redis API
- 程序员成长的烦恼(奋斗的小鸟)_PDF 电子书
- 强连通分量+缩点-poj1236
- OCP-1Z0-052-V8.02-180题
- 利用PS脚本自动删除7天之前建立的目录-方法1!
- KMP算法的详细解释及实现
- ArcGis10.2(中英文)安装详解及下载地址
- linux中fork函数详解
- MFC 文档视图结构程序结构总揽
- 代码大全(第2版)(奋斗的小鸟)_PDF 电子书
- netfilter/iptables 命令介绍