POJ 1236 Network of Schools(强连通分量/Tarjan缩点)
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<=N <=100)个学校之间有单向的网络,每个学校得到一套软件后,可以通过单向网络向周边的学校传输,
- 1:初始至少需要向多少个学校发放软件,使得网络内所有的学校最终都能得到软件。
- 2,至少需要添加几条传输线路(边),使任意向一个学校发放软件后,经过若干次传送,网络内所有的学校最终都能得到软件。
分析:
首先找连通分量,然后看连通分量的入度为0点的总数,出度为0点的总数,那么问要向多少学校发放软件,就是入度为零的个数,这样才能保证所有点最终都能得到软件
第二问添加多少条边可以得到使整个图达到一个强连通分量,答案是入度为0的个数和出度为0的个数中最大的值。将这个图的所有子树找出来,然后将一棵子树的叶子结点(出度为0)连到另外一棵子树的根结点上(入度为0),这样将所有的叶子结点和根节点全部消掉之后,就可以得到一整个强连通分量,看最少多少条边,这样就是看叶子结点和根节点哪个多,即出度为0和入度为0哪个多
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | //入度数组indeg[] 出度数组outdeg[] #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int maxn = 105; struct Edge{ int v,next; }edge[maxn*maxn]; int head[maxn],dfn[maxn],low[maxn],st[maxn],inst[maxn],belong[maxn],indeg[maxn],outdeg[maxn]; int tot,top,in,out,scc_cnt,index,N; void init() { tot = top = scc_cnt = index = 0; in = out = 0; memset (head,-1, sizeof (head)); memset (inst,0, sizeof (inst)); memset (dfn,0, sizeof (dfn)); memset (low,0, sizeof (low)); memset (indeg,0, sizeof (indeg)); memset (outdeg,0, sizeof (outdeg)); } void addedge( int u, int v) { edge[tot] = (Edge){v,head[u]}; head[u] = tot++; } void targin( int u) { int v; dfn[u] = low[u] = ++index; st[++top] = u; inst[u] = 1; for ( int i = head[u];i != -1;i = edge[i].next) { v = edge[i].v; if (!dfn[v]) { targin(v); low[u] = min(low[u],low[v]); } else if (inst[v]) low[u] = min(low[u],dfn[v]); } if (dfn[u] == low[u]) { scc_cnt++; do { v = st[top--]; inst[v] = 0; belong[v] = scc_cnt; } while (u != v); } } int main() { while (~ scanf ( "%d" ,&N)) { int v; init(); for ( int i = 1;i <= N;i++) while (~ scanf ( "%d" ,&v) && v) addedge(i,v); for ( int i = 1;i <= N;i++) if (!dfn[i]) targin(i); for ( int i = 1;i <= N;i++) for ( int j = head[i]; j != -1;j = edge[j].next) { v = edge[j].v; if (belong[i] != belong[v]) { indeg[belong[v]]++; outdeg[belong[i]]++; } } for ( int i = 1;i <= scc_cnt;i++) { if (!indeg[i]) in++; if (!outdeg[i]) out++; } if (scc_cnt == 1) printf ( "1\n0\n" ); else printf ( "%d\n%d\n" ,in,max(in,out)); } return 0; } |
┆ 凉 ┆ 暖 ┆ 降 ┆ 等 ┆ 幸 ┆ 我 ┆ 我 ┆ 里 ┆ 将 ┆ ┆ 可 ┆ 有 ┆ 谦 ┆ 戮 ┆ 那 ┆ ┆ 大 ┆ ┆ 始 ┆ 然 ┆
┆ 薄 ┆ 一 ┆ 临 ┆ 你 ┆ 的 ┆ 还 ┆ 没 ┆ ┆ 来 ┆ ┆ 是 ┆ 来 ┆ 逊 ┆ 没 ┆ 些 ┆ ┆ 雁 ┆ ┆ 终 ┆ 而 ┆
┆ ┆ 暖 ┆ ┆ 如 ┆ 地 ┆ 站 ┆ 有 ┆ ┆ 也 ┆ ┆ 我 ┆ ┆ 的 ┆ 有 ┆ 精 ┆ ┆ 也 ┆ ┆ 没 ┆ 你 ┆
┆ ┆ 这 ┆ ┆ 试 ┆ 方 ┆ 在 ┆ 逃 ┆ ┆ 会 ┆ ┆ 在 ┆ ┆ 清 ┆ 来 ┆ 准 ┆ ┆ 没 ┆ ┆ 有 ┆ 没 ┆
┆ ┆ 生 ┆ ┆ 探 ┆ ┆ 最 ┆ 避 ┆ ┆ 在 ┆ ┆ 这 ┆ ┆ 晨 ┆ ┆ 的 ┆ ┆ 有 ┆ ┆ 来 ┆ 有 ┆
┆ ┆ 之 ┆ ┆ 般 ┆ ┆ 不 ┆ ┆ ┆ 这 ┆ ┆ 里 ┆ ┆ 没 ┆ ┆ 杀 ┆ ┆ 来 ┆ ┆ ┆ 来 ┆
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)