AcWing 1191. 家谱树
原题链接
简单
作者:
_empty
,
2020-03-16 18:28:10
,
所有人可见
,
阅读 868
topsort裸题复习
#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
using namespace std;
const int N = 110;
int d[N];
int q[N]; int hh = 0, tt = -1;
vector<int> h[N];
int n;
void add(int a, int b)
{
h[a].push_back(b);
}
void topsort()
{
for(int i = 1; i <= n; i++)
if(d[i] == 0) q[++tt] = i;
while(hh <= tt)
{
int t = q[hh++];
for(int i = 0; i < h[t].size(); i++)
{
int j = h[t][i];
d[j]--;
if(d[j] == 0) q[++tt] = j;
}
}
}
int main()
{
int x;
cin >> n;
for(int i = 1; i <= n; i++)
{
while(cin >> x, x)
{
add(i, x);
d[x]++;
}
}
topsort();
for(int i = 0; i < n; i++)
cout << q[i] << ' ';
return 0;
}