A
来源:互联网 发布:mac电池健康度怎么看 编辑:程序博客网 时间:2024/05/29 03:42
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
#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <algorithm>using namespace std;/*Tarjan 算法复杂度为O(N+M)*/const int MAXN = 20010;//点数const int MAXM = 50010;//边数struct Edge{ int to, next;}edge[MAXM];int head[MAXN], tot;int Low[MAXN], DFN[MAXN], Stack[MAXN],Belong[MAXN];//Belong数组的值是1~sccint Index, top;int scc;//强连通分量的个数bool Instack[MAXN];int num[MAXN];//各个强连通分量包含的点的个数,数组编号1~sccvoid addedge(int u, int v){ edge[tot].to = v; edge[tot].next = head[u]; head[u] = tot++;}//总的来数,这个算法是在dfs的基础上,通过访问,来判断是否可以构成一个强连通分量。void Tarjan(int u){ int v; Low[u] = DFN[u] = ++Index;//Low 记录能够到达当前点的最小编号是多少,DFN时间戳 Stack[top++] = u; Instack[u] = true; for(int i=head[u];i!=-1;i=edge[i].next)//遍历所有的边 { v = edge[i].to; if(!DFN[v]) { Tarjan(v); if(Low[u]>Low[v]) { Low[u] = Low[v]; } } else if(Instack[v]&&Low[u]>DFN[v]) Low[u] = DFN[v]; } if(Low[u]==DFN[u]) { scc++; do { v = Stack[--top]; Instack[v] = false; Belong[v] = scc; //num[scc]++; } while(v!=u); }}int in[MAXN], out[MAXN];//入度,出度void solve(int N){ memset(DFN, 0, sizeof(DFN)); memset(Instack, false, sizeof(Instack)); //memset(num, 0, sizeof(num)); Index = scc = top = 0; for(int i=1;i<=N;i++) { if(!DFN[i]) { Tarjan(i); } } //kaishi if(scc==1) { printf("1\n0\n"); return ; } for(int i=1;i<=scc;i++) in[i] = out[i] = 0; for(int u=1;u<=N;u++) { for(int i= head[u];i!=-1;i=edge[i].next) { int v = edge[i].to; if(Belong[u]!=Belong[v]) { in[Belong[v]]++; out[Belong[u]]++; } } } int ans1 = 0; int ans2 = 0; for(int i=1;i<=scc;i++) { if(in[i]==0) ans1++; if(out[i]==0) ans2++; } printf("%d\n%d\n", ans1, max(ans1, ans2));}void init()//初始化{ tot = 0; memset(head, -1, sizeof(head));}int main(){ int n; int v; while(~scanf("%d", &n)) { init(); for(int i=1;i<=n;i++) { while(~scanf("%d", &v)&&v) { addedge(i, v);//建边 } } solve(n); } return 0;}
强连通图:有向图+强连通
题目大意:
就是说假如有n个入度为0的点,m个出度为0的点,输出:1,n; 2,max(n, m);要考虑一个节点的情况。
- a
- a
- a
- a
- a
- a
- a
- a
- a
- a
- a
- a
- a
- A
- A*
- a
- A
- a
- window.location (小结)
- C++Primer_第11章_关联容器
- CreateThread导致内存泄露的原因
- 强删文件--->构建IRP---->独占--->正在运行 以及磁盘读写(思路)
- [caioj 1425][状态压缩]游戏---状态压缩dp
- A
- 数字图像处理与OpenCV总结篇:一
- ubuntu 永久修改主机名
- DI之循环依赖 ——spring
- 用python实现五大基本算法
- 如何将经纬度数据通过ArcGIS转换为KML文件展绘到Google Earth 并且获取大致面积
- Memcached简述
- 新项目二之整理资料(原型、需求、流程图)《未完待续》
- RandomAccessFile使用小结