题目描述
blablabla
样例
blablabla
算法1
(暴力枚举) $O(n^2)$
blablabla
时间复杂度
参考文献
C++ 代码
#include<iostream>
#include<queue>
#include<vector>
#include<cstring>
using namespace std;
const int N = 110, M = N * N / 2;
int h[N], e[M], ne[M], idx;
int din[N];
int n;
vector<int> res;
void add(int a, int b)
{
e[idx] = b, ne[idx] = h[a], h[a] = idx ++;
}
void topsort()
{
queue<int> q;
for (int i = 1; i <= n; i ++)
if (!din[i]) q.push(i);
while(q.size())
{
auto t = q.front();
q.pop();
res.push_back(t);
for (int i = h[t]; ~i; i = ne[i])
{
int j = e[i];
if (-- din[j] == 0) q.push(j);
}
}
}
int main()
{
cin >> n;
memset(h, -1, sizeof h);
for (int i = 1; i <= n; i ++)
{
int b;
while(cin >> b, b)
{
add(i, b);
din[b] ++;
}
}
topsort();
for (int i = 0; i < n; i ++) cout << res[i] << " ";
return 0;
}
算法2
(暴力枚举) $O(n^2)$
blablabla
时间复杂度
参考文献
C++ 代码
blablabla