#include<iostream>
#include<sstream>
#include<string>
#include<vector>
#include<queue>
#include<unordered_map>
#include<set>
using namespace std;
int main(void) {
vector<vector<int>> arr;
string input;
while (getline(cin, input)) {
if (input.size() > 0) {
vector<int> v;
for (int i = 0; i < input.size(); ++i) {
if (input[i]>='0' && input[i] <='9') v.push_back(input[i]-'0');
}
arr.push_back(v);
}
}
int num = arr[0][0];
int des = arr[1][0];
vector<int> induce(num, 0);
unordered_map<int, vector<int>> M;
for (int i = 2; i < arr.size(); ++i) {
induce[i - 2] = arr[i].size() - 1;
for (int j = 1; j < arr[i].size(); ++j) {
M[arr[i][j]].push_back(i-2);
}
}
queue<int> Q;
for (int i = 0; i < num; ++i) {
if (induce[i] == 0) Q.push(i);
}
bool can = false;
while (!Q.empty()) {
int curr = Q.front();
Q.pop();
if (curr == des) {
can = true;
break;
}
for (auto v : M[curr]) {
induce[v] --;
if (induce[v] == 0) Q.push(v);
}
}
if (can) {
set<int> S;
queue<int> q;
for (int i = 1; i < arr[des + 2].size(); ++i) {
if (!S.count(arr[des + 2][i])) {
q.push(arr[des + 2][i]);
S.insert(arr[des + 2][i]);
}
}
while (!q.empty()) {
int curr = q.front(); q.pop();
for (int i = 1; i < arr[curr + 2].size(); ++i) {
if (!S.count(arr[curr + 2][i])) {
q.push(arr[curr + 2][i]);
S.insert(arr[curr + 2][i]);
}
}
}
auto tmp = S.end();
tmp--;
for (auto itr = S.begin(); itr != tmp; ++itr) cout << *itr << ",";
cout << *tmp;
}
else
cout << -1;
}
有大佬帮忙看看问题出在哪里吗?
#华为笔试#