题目链接
tarjan求强连通分量,然后缩点,
对于任务a
是入度为零的点的个数
对于任务b
是max(入度为零的个数, 出度为零的个数)
所以可知。
ps.不知在任务b卡了多久。
下面是ac代码:
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <map>
#include <queue>
#include <stack>
#define ll long long
using namespace std;
const int N = 1e6+10;
int ver[N], ne[N], he[N];
int dfn[N], low[N], dir[N], dic[N], s[N], ins[N], part[N];
int root;
int tot, top, pcnt;
int ind, n, m;
void init()
{
memset(he, 0, sizeof(he));
memset(ne, 0, sizeof(ne));
memset(dfn, 0, sizeof(dfn));
memset(low, 0, sizeof(low));
memset(ins, 0, sizeof(ins));
memset(dir, 0, sizeof(dir));
memset(dic, 0, sizeof(dic));
ind = 0;
pcnt = 0;
top = 0;
tot = 0;
}
void add(int x, int y)
{
ver[++tot] = y;
ne[tot] = he[x];
he[x] = tot;
}
void tarjan(int x)
{
dfn[x] = low[x] = ++ind;
s[top++] = x;
ins[x] = 1;
for (int i = he[x]; i; i = ne[i])
{
int y = ver[i];
if (!dfn[y])
{
tarjan(y);
low[x] = min(low[x], low[y]);
}
else if (ins[y]) low[x] = min(low[x], dfn[y]);
}
int y;
if (low[x] == dfn[x])
{
pcnt++;
do
{
y = s[--top];
part[y] = pcnt;
ins[y] = 0;
} while(x != y);
}
}
int main()
{
while(cin >> n)
{
init();
for (int i = 1; i <= n; i++)
{
int v;
while(scanf("%d", &v), v)
{
add(i, v);
}
}
for (int i = 1; i <= n; i++)
{
if (!dfn[i]) tarjan(i);
}
for (int x = 1; x <= n; x++)
{
for (int i = he[x]; i; i = ne[i])
{
int y = ver[i];
if (part[y] != part[x])
{
dir[part[y]]++;
dic[part[x]]++;
}
}
}
/*
for (int i = 1; i <= n; i++)
{
cout << i << ":" << part[i] << endl;
}
for (int i = 1; i <= pcnt; i++)
{
cout << dir[i] << " ";
}
cout << endl;
for (int j = 1; j <= pcnt; j++)
{
cout << dic[j] << " ";
}
cout << endl;
*/
if (pcnt == 1)
{
printf("1\n0\n");
continue;
}
int cntx = 0;
int cnty = 0;
for (int i = 1; i <= pcnt; i++)
{
if (dic[i] == 0)
cntx++;
if (dir[i] == 0)
cnty++;
}
printf("%d\n%d\n", cnty, max(cntx, cnty));
}
return 0;
}