版本比较
拓扑排序
正文
增加 10 行删除 10 行
27 行未改动28293031323334353637383940414243444546474849505152535455565758596061626364656627 行未改动stack <int> sta;//也可以用queueint main(void) {cin >> n;for (int i = 1; i <= n; i ++) {int A;while(1) {cin >> A;if(!A) break;a[i][++ out[i]] = A;in[A] ++;}}for (int i = 1; i <= n; i ++) {if(!in[i]) {sta.push(i);}}while (sta.size()) {int t = sta.top();cout << t << ' ';sta.pop();for (int i = 1; i <= out[t]; i ++) {in[a[t][i]] --;if(!in[a[t][i]]) sta.push(a[t][i]);}}return 0;}```27 行未改动28293031323334353637383940414243444546474849505152535455565758596061626364656627 行未改动stack <int> sta;//也可以用queueint main(void) {cin >> n;for (int i = 1; i <= n; i ++) {int A;while(1) {cin >> A;if(!A) break;a[i][++ out[i]] = A;in[A] ++;}}for (int i = 1; i <= n; i ++) {if(!in[i]) {sta.push(i);}}while (sta.size()) {int t = sta.top();cout << t << ' ';sta.pop();for (int i = 1; i <= out[t]; i ++) {in[a[t][i]] --;if(!in[a[t][i]]) sta.push(a[t][i]);}}return 0;}```