Advertisement
kdzhr

1106

Sep 20th, 2020
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.96 KB | None | 0 0
  1. // Timus 1106
  2.  
  3. #include <iostream>
  4. #include <vector>
  5. #include <set>
  6. #include <cmath>
  7.  
  8. #define all(a) a.begin(), a.end()
  9. #define rall(a) a.rbegin(), a.rend()
  10.  
  11. // #define file_io
  12. // #define many_tasks
  13. #define int int32_t
  14. #define ll int64_t
  15.  
  16. #define uint uint32_t
  17. #define ull uint64_t
  18.  
  19. #define db double
  20. #define ld long double
  21.  
  22.  
  23. void ios() {
  24.     std::ios_base::sync_with_stdio(false);
  25.     std::cin.tie(nullptr);
  26.     std::cout.tie(nullptr);
  27. }
  28.  
  29. void dfs(std::vector<std::vector<int>> &g, std::vector<int> &color, int v) {
  30.     if (color[v] == 0) color[v] = 1;
  31.     for (auto &u : g[v]) {
  32.         if (color[u] == 0) {
  33.             color[u] = 3 - color[v];
  34.             dfs(g, color, u);
  35.         }
  36.     }
  37. }
  38.  
  39. //-----------------------------------------------------------------------------------------------//
  40.  
  41. void solve() {
  42.     int n;
  43.     std::cin >> n;
  44.     std::vector<std::vector<int>> g(n);
  45.     for (int i = 0; i < n; ++i) {
  46.         int a;
  47.         while (std::cin >> a) {
  48.             if (a == 0) break;
  49.             g[i].push_back(a - 1);
  50.         }
  51.     }
  52.     std::vector<int> res;
  53.     std::vector<int> color(n, 0);
  54.     for (int i = 0; i < n; ++i) {
  55.         if (color[i] == 0) {
  56.             dfs(g, color, i);
  57.         }
  58.     }
  59.  
  60.     for (int i = 0; i < n; ++i) {
  61.         if (color[i] == 1) {
  62.             res.push_back(i + 1);
  63.         } else if (color[i] == 0) {
  64.             std::cout << 0;
  65.             return;
  66.         }
  67.     }
  68.     std::cout << res.size() << '\n';
  69.     for (auto &elem : res) {
  70.         std::cout << elem << ' ';
  71.     }
  72.  
  73. }
  74.  
  75. //-----------------------------------------------------------------------------------------------//
  76.  
  77. int main() {
  78. #ifdef file_io
  79.     freopen("C:\\file_io\\input.txt", "r", stdin);
  80.     freopen("C:\\file_io\\output.txt", "w", stdout);
  81. #endif
  82.     ios();
  83. #ifdef many_tasks
  84.     size_t t;
  85.     std::cin >> t;
  86.     while (t-- > 0) {
  87.         solve();
  88.     }
  89. #else
  90.     solve();
  91. #endif
  92.     return 0;
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement