poj 1236 Network of Schools 强连通分量
来源:互联网 发布:德拉蒙德格林2016数据 编辑:程序博客网 时间:2024/06/04 18:13
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.
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.
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.
52 4 3 04 5 0001 0
12
强连通分量进行两遍dfs。第一次dfs,选取任意顶点作为起点,遍历所有尚未访问的顶点,并在回溯前给顶点编号(后序遍历)。对剩余的未访问过的顶点,不断重复此过程。完成标号之后,不断重复上述过程。
第二次dfs,先将所有边反向,然后以标点最大的顶点为起点进行dfs,这样dfs所遍历的顶点集合就构成了一个强连通分量。之后,还有尚未访问的顶点,从中选取最大的顶点不断重复此过程
#include<cstdio>#include<vector>#include<cstring>#include<algorithm>using namespace std;const int M =110;int n;vector<int>G[M];//图的邻接表表示vector<int>RG[M];// 把边反向的图vector<int>vs;//把后序遍历顺序的顶点列表bool used[M];//访问标记int cmp[M];//所属强连通分量的拓扑序int in[M],out[M];//出度、入度void add_edge(int from,int to){ G[from].push_back(to); RG[to].push_back(from);}void dfs(int v){ used[v]=true; for(int i=0;i<G[v].size();i++) { if(!used[G[v][i]]) dfs(G[v][i]); } vs.push_back(v);}void rdfs(int v,int k){ used[v]=true; cmp[v]=k; for(int i=0;i<RG[v].size();i++) { if(!used[RG[v][i]]) rdfs(RG[v][i],k); }}int scc(){ memset(used,0,sizeof(used)); vs.clear(); for(int v=0;v<n;v++) { if(!used[v]) dfs(v); } memset(used,0,sizeof(used)); int k=0; for(int i=vs.size()-1;i>=0;i--) { if(!used[vs[i]]) rdfs(vs[i],k++); } return k;}int main(){ scanf("%d",&n); for(int u=0;u<n;u++) { int v; scanf("%d",&v); while(v) { add_edge(u,--v); scanf("%d",&v); } } int m=scc(); if(m==1) { printf("1\n0\n"); return 0; } for(int u=0;u<n;u++) { for(int i=0;i<G[u].size();i++) { int v=G[u][i]; if(cmp[u]!=cmp[v]) ++out[cmp[u]],++in[cmp[v]]; } } int zout=0,zin=0; for(int i=0;i<m;i++) { if(in[i]==0) ++zin; if(out[i]==0) ++zout; } printf("%d\n",zin); printf("%d\n",max(zin,zout)); return 0;}
阅读全文
0 0
- POJ 1236 Network of Schools强连通分量缩点
- POJ 1236 Network of Schools (强连通分量+缩点)
- POJ 1236 Network of Schools 强连通分量+缩点
- poj 1236 Network of Schools (强连通分量+缩点)
- poj 1236 Network of Schools 强连通分量
- poj 1236 Network of Schools(强连通分量+缩点)
- POJ 1236 Network of Schools 强连通分量
- poj 1236--Network of Schools [强连通分量]
- POJ 1236 Network of Schools(强连通分量)
- POJ 1236 Network of Schools(强连通分量)
- POJ 1236 Network of Schools(强连通分量)
- POJ 1236 Network of Schools(强连通分量)
- POJ 1236 Network of Schools(强连通分量)
- poj 1236 Network of Schools(强连通分量 Tarjan)
- POJ 1236 - Network of Schools(强连通分量)
- poj 1236 Network of Schools(强连通分量缩点)
- POJ - 1236 Network of Schools(强连通分量)
- poj 1236 Network of Schools(强连通分量)
- 大数据Sqoop_09
- 0x5f3759df的数学原理 ----一种快速开方根求倒原理
- JS阻止事件冒泡兼容各浏览器
- 分布式系统的高效运维要点
- 截取视频图片
- poj 1236 Network of Schools 强连通分量
- POJ3259 判断是否存在负权环
- CCF-20161201-中间数(100分)
- 通过 SSH 实现 TCP / IP 隧道(端口转发)
- SGeo中国中心:开源GIS软件、在线教程、数据相关资料
- Servlet (六 Session)
- 欢迎使用CSDN-markdown编辑器
- 第3章 数据的输入与输出
- Python网络数据采集总结——未完待续