You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
53 lines
1.4 KiB
C++
53 lines
1.4 KiB
C++
#include <cstdio>
|
|
inline int min(int a, int b) { return a < b ? a : b; }
|
|
const int maxn = 110, maxm = 10100;
|
|
int head[maxn], next[maxm], to[maxm], ecnt, f[maxn], g[maxn];
|
|
inline void addEdge(int f, int t)
|
|
{
|
|
ecnt++;
|
|
next[ecnt] = head[f];
|
|
head[f] = ecnt;
|
|
to[ecnt] = t;
|
|
}
|
|
int dfn[maxn], low[maxn], stk[maxn], scc[maxn], scccnt, top, idx;
|
|
void tarjan(int x)
|
|
{
|
|
dfn[x] = low[x] = ++idx;
|
|
stk[top++] = x;
|
|
for (int i = head[x]; i; i = next[i])
|
|
if (!dfn[to[i]])
|
|
tarjan(to[i]), low[x] = min(low[x], low[to[i]]);
|
|
else if (!scc[to[i]])
|
|
low[x] = min(low[x], dfn[to[i]]);
|
|
if (dfn[x] == low[x])
|
|
{
|
|
scccnt++;
|
|
do
|
|
scc[stk[--top]] = scccnt;
|
|
while (stk[top] != x);
|
|
}
|
|
}
|
|
int main()
|
|
{
|
|
int n;
|
|
scanf("%d", &n);
|
|
for (int i = 1, x; i <= n; i++)
|
|
for (scanf("%d", &x); x; scanf("%d", &x))
|
|
addEdge(i, x);
|
|
for (int i = 1; i <= n; i++)
|
|
if (!dfn[i]) tarjan(i);
|
|
for (int i = 1; i <= n; i++)
|
|
for (int j = head[i]; j; j = next[j])
|
|
if (scc[i] != scc[to[j]])
|
|
f[scc[i]]++, g[scc[to[j]]]++;
|
|
int ans1 = 0, ans2 = 0;
|
|
if (scccnt == 1)
|
|
printf("1\n0");
|
|
else
|
|
{
|
|
for (int i = 1; i <= scccnt; i++)
|
|
ans1 += f[i] == 0, ans2 += g[i] == 0;
|
|
printf("%d\n%d", ans2, ans1 > ans2 ? ans1 : ans2);
|
|
}
|
|
return 0;
|
|
} |