POJ 1236 Network of Schools(使DAG强联通最小加边数)
2016-08-09 09:34
330 查看
Network of Schools
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
Sample Output
Source
IOI 1996
题目大意:
有V个学校之间有单向通信网络。求:1、至少给多少个学校资料,才能使所有学校得到资料。2、最少还需要建设多少条线路才能使任意一个学校可以向任意一个学校发起通信。
解题思路:
首先要对这个图求强联通分量。那么问题1很明显就是入度为0的强联通分量的个数。至于问题2,仔细想一下,只要让入度为0的强联通分量和出度为0的强联通分量不存在就可以了。那么我们最少需要加边max(入度为0的强联通分量的数目,出度为0的强联通分量的数目)。此外,全图只有一个强联通分量的时候需要特判,这时需要加边数应该为0,而不是用上面方法得到的max(1,1)=1。
AC代码:
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 15788 | Accepted: 6255 |
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
Source
IOI 1996
题目大意:
有V个学校之间有单向通信网络。求:1、至少给多少个学校资料,才能使所有学校得到资料。2、最少还需要建设多少条线路才能使任意一个学校可以向任意一个学校发起通信。
解题思路:
首先要对这个图求强联通分量。那么问题1很明显就是入度为0的强联通分量的个数。至于问题2,仔细想一下,只要让入度为0的强联通分量和出度为0的强联通分量不存在就可以了。那么我们最少需要加边max(入度为0的强联通分量的数目,出度为0的强联通分量的数目)。此外,全图只有一个强联通分量的时候需要特判,这时需要加边数应该为0,而不是用上面方法得到的max(1,1)=1。
AC代码:
#include <iostream> #include <algorithm> #include <cstring> #include <cstdio> #include <queue> #include <vector> using namespace std; const int maxv=100+3; int V;//顶点数 vector<int> G[maxv];//图的邻接表表示 vector<int> rG[maxv];//把边反向后的图 vector<int> vs;//后续遍历顺序的顶点列表 bool used[maxv];//访问节点标记 int cmp[maxv];//所属强联通分量的拓扑序 int in[maxv],out[maxv];//强联通分量入度出度 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()//求强联通分量 { for(int v=0;v<V;++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; } void dfs2(int v) { used[v]=true; for(int i=0;i<G[v].size();++i) if(!used[G[v][i]]) { if(cmp[v]!=cmp[G[v][i]]) { ++out[cmp[v]]; ++in[cmp[G[v][i]]]; } dfs2(G[v][i]); } } int main() { scanf("%d",&V); for(int i=0;i<V;++i) { int to; while(true) { scanf("%d",&to); if(!to) break; add_edge(i,to-1); } } int num=scc(); if(num==1)//当只有一个强联通分量时特判 { puts("1\n0"); return 0; } for(int i=vs.size()-1;i>=0;--i)//统计每个强联通分量的入度和出度 { memset(used,0,sizeof used); if(!used[vs[i]]) dfs2(vs[i]); } int num_in=0,num_out=0; for(int i=0;i<num;++i) { if(!in[i]) ++num_in; if(!out[i]) ++num_out; } printf("%d\n%d\n",num_in,max(num_in,num_out)); return 0; }
相关文章推荐
- POJ 1236 Network of Schools(强联通缩点)
- POJ 1236 Network of Schools(korasaju Algorithm求强连通子集,缩点后DAG上出或入度为0的点)
- 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 强联通分量求出度为0的点和入度为0的点的数目
- POJ 1236 Network of Schools(Tarjan Algorithm求强连通子集,缩点后DAG上出或入度为0的点)
- 文章标题 POJ 1236 : Network of Schools (强联通分量+缩点)
- poj 1236 Network of Schools (最小点基)
- POJ 1236 Network of Schools ★(经典问题:强联通分量+缩点)
- POJ 1236--Network of Schools【scc缩点构图 && 求scc入度为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 (强连通分量+缩点)
- poj1236 Network of Schools