O(n+m)
int h[N], e[M], ne[M], idx; //memset(h, -1, sizeof h);
int din[N]; //入度
int q[N]; //res
void add(int a, int b)
{
din[b]++;
e[idx] = b;
ne[idx] = h[a];
h[a] = idx++;
}
bool topsort() //检查是否为有向无环图
{
int hh = 0, tt = -1;
for (int i = 1; i <= n; i++) //起点
{
if (!din[i])
{
q[++tt] = i;
}
}
while (hh <= tt)
{
int t = q[hh++];
for (int i = h[t]; ~i; i = ne[i])
{
int j = e[i];
din[j]--;
if (!din[j])
{
q[++tt] = j;
}
}
}
return tt == n - 1;
}