1833: Network of Schools 校园网
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 155 Solved: 63
[Submit][Status][Web Board]
Description
一些学校连入一个电脑网络。那些学校已订立了协议:每个学校都会给其它的一些学校分发软件(称作“接受学校”)。注意如果 B 在 A 学校的分发列表中,那么 A 不必也在 B 学校的列表中。
你要写一个程序计算,根据协议,为了让网络中所有的学校都用上新软件,必须接受新软件副本的最少学校数目(子任务 A)。更进一步,我们想要确定通过给任意一个学校发送新软件,这个软件就会分发到网络中的所有学校。为了完成这个任务,我们可能必须扩展接收学校列表,使其加入新成员。计算最少需要增加几个扩展,使得不论我们给哪个学校发送新软件,它都会到达其余所有的学校(子任务 B)。一个扩展就是在一个学校的接收学校列表中引入一个新成员。
Input
输入文件的第一行包括一个整数 N:网络中的学校数目(2 <= N <= 100)。学校用前 N 个正整数标识。接下来 N 行中每行都表示一个接收学校列表(分发列表)。第 i+1 行包括学校 i 的接收学校的标识符。每个列表用 0 结束。空列表只用一个 0 表示。
Output
你的程序应该在输出文件中输出两行。第一行应该包括一个正整数:子任务 A 的解。第二行应该包括子任务 B 的解。
Sample Input
52 4 3 04 5 0001 0
Sample Output
12
Source
USACO
总结:tarjan + 缩点
#include<bits/stdc++.h>using namespace std;const int maxn = 100005;int dfn[maxn], low[maxn], n, tot = 0, tim;bool vis[maxn]; stack<int> s; int c[maxn], r[maxn];int head[maxn], cnt = 1, num, color[maxn];struct Node{ ???int v, nxt;} G[maxn];void insert(int u, int v) { ???G[cnt] = (Node) {v, head[u]}; head[u] = cnt++;}void tarjan(int x) { ???vis[x] = true; ???s.push(x); ???low[x] = dfn[x] = ++tim; ???for (int i = head[x]; i; i = G[i].nxt) { ???????int v = G[i].v; ???????if(!dfn[v]) { ???????????tarjan(v); ???????????low[x] = min(low[x], low[v]); ???????} else if(vis[v]) low[x] = min(low[x], dfn[v]); ???} if(dfn[x] == low[x]) { ???????tot++; ???????while(1) { ???????????int now = s.top(); ???????????s.pop(); vis[now] = false; ???????????color[now] = tot; ???????????if(now == x) break; ???????} ???}}int main() { ???scanf("%d", &n); ???for (int i = 1; i <= n; ++i) { ???????for(; ;) { ???????????int a; scanf("%d", &a); ???????????if(a == 0) break; insert(i, a); ???????} ???} ???for (int i = 1; i <= n; ++i) if(!dfn[i]) tarjan(i); ???int ans1 = 0, ans2 = 0; ???for (int u = 1; u <= n; ++u) { ???????for (int i = head[u]; i; i = G[i].nxt) { ???????????int v = G[i].v; ???????????if(color[u] != color[v]) { ???????????????c[color[u]]++; ???????????????r[color[v]]++; ???????????} ???????} ???} ???for (int i = 1; i <= tot; ++i) { ???????if(!c[i]) ans1++; ???????if(!r[i]) ans2++; ???} ????printf("%d\n", ans2); ???if(tot == 1) printf("0\n"); ???else printf("%d\n", max(ans1, ans2)); ???return 0;}
JDOJ-1833: Network of Schools 校园网
原文地址:https://www.cnblogs.com/oi-forever/p/8910077.html