跳到正文

个人记录

jklhhklhlkjhkj

SEF_711约 1 分钟
原文
文章信息与版本

文章信息

收录
2026年7月4日
更新
2026年7月4日
篇幅
约 1 分钟

版本

  1. v1当前
C++
#include <bits/stdc++.h>
#define int long long
using namespace std;

const int kN = 105;
int n;
int in[kN];
vector<int> g[kN];
stack<int> stk;
signed main() {
	cin >> n;
	for (int i = 1; i <= n; i++) {
		int x;
		while (cin >> x && x != 0) {
			g[i].push_back(x);
			in[x]++;
		}
	}
	for (int i = 1; i <= n; i++) {
		if (in[i] == 0) {
			stk.push(i);
		}
	}
	while (stk.empty() == 0) {
		int cur = stk.top();
		stk.pop();
		cout << cur << " ";
		for (auto v : g[cur]) {
			in[v]--;
			if (in[v] == 0) {
				stk.push(v);
			}
		}
	}
	return 0;
}
原文