#include <iostream>
#include <cstring>
#include <cstdio>
#define maxn 10000 + 5
#define maxm 10000 + 5
using namespace std;
struct edge {
int from, to, next;
edge() {}
edge(register const int &_from, register const int &_to, register const int &_next) {
from = _from;
to = _to;
next = _next;
}
} e[maxm], ed[maxm];
int head[maxn], k;
int dfn[maxn], low[maxn], tot;
int stack[maxn], top, vis[maxn];
int col[maxn], cnt;
int ind[maxn], outd[maxn];
int n;
inline void add(register const int &u, register const int &v) {
e[k] = edge(u, v, head[u]);
head[u] = k++;
}
inline void tarjan(register const int &u) {
dfn[u] = low[u] = ++tot;
stack[++top] = u;
vis[u] = true;
for(register int i = head[u]; ~i; i = e[i].next) {
register int v = e[i].to;
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]) {
register int v;
cnt++;
do {
v = stack[top--];
col[v] = cnt;
vis[v] = false;
} while(u != v);
}
}
int main() {
memset(head, -1, sizeof head);
scanf("%d", &n);
for(register int i = 1, v; i <= n; i++) {
while(scanf("%d", &v) == 1 && v) {
add(i, v);
}
}
for(register int i = 1; i <= n; i++) if(!dfn[i]) {
tarjan(i);
}
for(register int i = 0; i < k; i++) {
register int u = col[e[i].from], v = col[e[i].to];
if(u != v) {
outd[u]++;
ind[v]++;
}
}
register int cnt_ind = 0, cnt_outd = 0;
for(register int i = 1; i <= cnt; i++) {
if(!ind[i]) cnt_ind++;
if(!outd[i]) cnt_outd++;
}
if(cnt == 1) printf("1\n0\n");
else printf("%d\n%d\n", cnt_ind, max(cnt_ind, cnt_outd));
return 0;
}
手机扫一扫
移动阅读更方便
你可能感兴趣的文章