Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 20114 | Accepted: 7915 |
Description
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
Output
Sample Input
5 2 4 3 0 4 5 0 0 0 1 0
Sample Output
1 2
Source
#include <cstdio> #include <stack> #include <cstring> #include <iostream> #include <algorithm> using namespace std; const int maxn = 50010; int n,m,head[maxn],to[maxn],nextt[maxn],tot = 1,pre[maxn],low[maxn],scc[maxn],cnt,du[maxn],dfs_clock; int ans,numm,num[maxn],num1,num2,ru[maxn],chu[maxn],ru2[maxn],chu2[maxn],num3; stack <int> s; void add(int x,int y) { to[tot] = y; nextt[tot] = head[x]; head[x] = tot++; } void tarjan(int u) { s.push(u); pre[u] = low[u] = ++dfs_clock; for (int i = head[u];i;i = nextt[i]) { int v = to[i]; if (!pre[v]) { tarjan(v); low[u] = min(low[u],low[v]); } else if (!scc[v]) low[u] = min(low[u],pre[v]); } if (pre[u] == low[u]) { cnt++; while(1) { int t = s.top(); s.pop(); scc[t] = cnt; num[cnt]++; if(t == u) break; } } } void init() { tot = 1; memset(head,0,sizeof(head)); memset(ru,0,sizeof(ru)); memset(chu,0,sizeof(chu)); memset(chu2,0,sizeof(chu2)); memset(ru2,0,sizeof(ru2)); memset(pre,0,sizeof(pre)); memset(scc,0,sizeof(scc)); memset(low,0,sizeof(low)); dfs_clock = 0; cnt = 0; num1 = num2 = num3 = 0; } int main() { while (scanf("%d",&n) != EOF) { init(); int p; for (int i = 1; i <= n; i++) { while (scanf("%d",&p) && p != 0) { add(i,p); chu2[i]++; ru2[p]++; } } for (int i = 1; i <= n; i++) if (!pre[i]) tarjan(i); for (int i = 1; i <= n; i++) for (int j = head[i];j;j = nextt[j]) { int v = to[j]; if (scc[i] != scc[v]) { chu[scc[i]]++; ru[scc[v]]++; } } for (int i = 1; i <= cnt; i++) { if (!ru[i]) num1++; } for (int i = 1; i <= n; i++) { if (!ru2[i]) num2++; if (!chu2[i]) num3++; } if (cnt != 1) printf("%d\n%d\n",num1,max(num2,num3)); else printf("1\n0\n"); } return 0; }