poj 1236 Network of Schools (tarjan)
来源:互联网 发布:文言中乎的用法和意义 编辑:程序博客网 时间:2024/05/30 13:42
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
5
2 4 3 0
4 5 0
0
0
1 0
Sample Output
1
2
题意
N(2
题解
超级无敌大水题(我已经沦落至此了),就是求强连通分量的个数和缩点后DAG图中入度和出度的最大值。
代码
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int N = 10010;struct Edge { int u, v, next;} edge[N];bool vis[N];int n, m, num, head[N];int top = 0, cnt = 0, indexx = 0, in[N], out[N], low[N], dfn[N], stack[N], place[N];void add(int u, int v) { num ++; edge[num].u = u; edge[num].v = v; edge[num].next = head[u]; head[u] = num;}void Tarjan(int u) { low[u] = dfn[u] = ++ indexx; vis[u] = true; stack[++ top] = u; for(int i = head[u]; i; i = edge[i].next) { int v = edge[i].v; if(! dfn[v]) { Tarjan(v); low[u] = min(low[u], low[v]); } else if(vis[v]) low[u] = min(low[u], dfn[v]); } if(dfn[u] == low[u]) { vis[u] = false; place[u] = ++ cnt; while(stack[top] != u) { place[stack[top]] = cnt; vis[stack[top --]] = false; } top --; }}int main() { scanf("%d", &n); for(int i = 1; i <= n; i ++) while(scanf("%d", &m) && m) add(i, m); for(int i = 1; i <= n; i ++) if(! place[i]) Tarjan(i); if(cnt == 1) { printf("1\n0"); return 0; } for(int i = 1; i <= num; i ++) { int u = edge[i].u, v = edge[i].v; if(place[u] != place[v]) in[place[v]] ++, out[place[u]] ++; } int ans1 = 0, ans2 = 0; for(int i = 1; i <= cnt; i ++) { if(! in[i]) ans1 ++; if(! out[i]) ans2 ++; } printf("%d\n%d", ans1, max(ans1, ans2)); return 0;}
- poj 1236 Network of Schools (tarjan)
- poj 1236 Network of Schools(Tarjan)
- POJ 1236Network of Schools(tarjan)
- [tarjan] poj 1236 Network of Schools
- poj 1236 Network of Schools 【Tarjan】
- POJ 1236 Network of Schools 【tarjan
- poj 1236 Network of Schools(强连通分量 Tarjan)
- POJ-1236 Network of Schools (强连通分量[Tarjan])
- poj 1236 Network of Schools(tarjan 缩点)
- 【POJ 1236】Network of Schools(tarjan算法)
- POJ 1236 Network of Schools (强连通分量tarjan)
- poj 1236 Network of Schools 1 (tarjan 缩点)
- POJ 1236 Network of Schools(tarjan缩点)
- Network of Schools(tarjan)
- POJ 1236 Network of Schools 强连通图 tarjan
- POJ 1236 Network of Schools tarjan强连通缩点
- poj 1236-Network of Schools:Tarjan算法实现
- Network of Schools - POJ 1236 Tarjan 缩点
- 多张图片上传并预览
- 详细:idea如何设置类头注释和方法注释
- 给Java新手的一些建议——Java知识点归纳(Java基础部分)
- 软中断机制
- mysql timeout参数详解1
- poj 1236 Network of Schools (tarjan)
- 【脚本语言系列】关于Python基础知识调试,你需要知道的事
- 动态绑定 vs 静态绑定
- Android--ListView实现所有Item的全选和未选
- 如何处理在下载文件时,中文的文件名传输
- 628. Maximum Product of Three Numbers
- intellij idean 打jar包
- poj2785(二分折半枚举)
- 回文链表的判断