缩点写得比较抽象
#include <bits/stdc++.h>
using namespace std;
const short MAXN = 11500;
short n, n2, it, cnt, flag;
bool t[MAXN][MAXN];
bool t2[MAXN][MAXN];
short l[MAXN];
short d[MAXN];
bool b[MAXN];
bool v[MAXN];
short f[MAXN];
stack <short> s;
void tan(short &x)
{
d[x] = l[x] = ++it;
s.push(x);
b[x] = 1;
for (short i = 1; i <= n; i++)
if (t[x][i] && !v[i])
if (b[i])
l[x] = min(d[i], l[x]);
else
{
tan(i);
l[x] = min(l[x], l[i]);
}
if (d[x] == l[x])
{
n2++;
while (s.top() != x)
b[s.top()] = 0, f[s.top()] = n2, s.pop();
b[x] = 0;
s.pop();
f[x] = n2;
}
v[x] = 1;
}
int main()
{
short b;
cin >> n;
for (short i = 1; i <= n; i++)
{
cin >> b;
while (b)
{
t[i][b] = 1;
cin >> b;
}
}
for (short i = 1; i <= n; i++)
if (! v[i])
tan(i);
for (short i = 1; i <= n; i++)
for (short j = 1; j <= n; j++)
if (f[i] != f[j])
t2[f[i]][f[j]] |= t[i][j];
short ans = 0;
for (short i = 1; i <= n2; i++)
{
bool fl = 0;
for (short j = 1; j <= n2; j++)
fl |= t2[j][i];
ans += ! fl;
}
for (short i = 1; i <= n2; i++)
{
bool fl = 0;
for (short j = 1; j <= n2; j++)
fl |= t2[i][j];
cnt += ! fl;
}
cout << ans << endl;
if (n2 > 1)
cout << max(ans, cnt) << endl;
else
cout << 0 <<endl;
return 0;
}
两问都少了一