fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. ios_base::sync_with_stdio(0);
  6. cin.tie(0);
  7. int t;
  8. cin >> t;
  9. while (t--) {
  10. int n;
  11. cin >> n;
  12. vector<int> m[n];
  13. for (int i = 0; i < n; i++) {
  14. int x;
  15. cin >> x;
  16. for (int j = 0; j < x; j++) {
  17. int y;
  18. cin >> y;
  19. m[i].push_back(y);
  20. }
  21. }
  22.  
  23. bool possible[50001] = {false};
  24. vector<int> ans;
  25.  
  26. for (int i = n - 1; i >= 0; i--) {
  27. bool found = false;
  28. for (auto itr : m[i]) {
  29. if (!found && !possible[itr]) {
  30. found = true;
  31. possible[itr] = true;
  32. ans.push_back(itr);
  33. } else
  34. possible[itr] = true;
  35. }
  36. }
  37.  
  38. if (ans.size() == n) {
  39. reverse(ans.begin(), ans.end());
  40. for (auto itr : ans)
  41. cout << itr << " ";
  42. } else
  43. cout << "-1";
  44. cout << endl;
  45. }
  46. }
  47.  
Success #stdin #stdout 0.01s 5300KB
stdin
3
3
4
1 2 4 8
3
2 9 1
2
1 4
2
2
1 2
2
2 1
4
4
1 2 3 4
1
1
1
4
1
3
stdout
8 2 1 
-1
2 1 4 3