Advertisement
tuki2501

VOSTRAVL.cpp

Nov 11th, 2021
1,058
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.94 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int N = 1000005;
  5.  
  6. int n, m, s, k;
  7. vector<int> adj[N];
  8. int tra[N], vst[N], id = 0;
  9. vector<int> sta;
  10.  
  11. void dfs(int i, int p) {
  12.   sta.push_back(i);
  13.   vst[i] = ++id;
  14.   for (auto &j : adj[i]) {
  15.     if (vst[j]) {
  16.       if (j != p && vst[j] < vst[i]) {
  17.         sta.push_back(j);
  18.         sta.push_back(i);
  19.       }
  20.       continue;
  21.     }
  22.     dfs(j, i);
  23.     sta.push_back(i);
  24.   }
  25. }
  26.  
  27. int main() {
  28.   cin.tie(0)->sync_with_stdio(0);
  29.   cin >> n >> m >> s >> k;
  30.   for (int i = 1; i <= m; i++) {
  31.     int u, v;
  32.     cin >> u >> v;
  33.     adj[u].push_back(v);
  34.     adj[v].push_back(u);
  35.   }
  36.   for (int i = 1; i <= k; i++) {
  37.     cin >> tra[i];
  38.   }
  39.   dfs(s, -1);
  40.   for (int i = 1; i <= k; i++) {
  41.     if (!vst[tra[i]]) {
  42.       cout << "NIE" << '\n';
  43.       return 0;
  44.     }
  45.   }
  46.   cout << "TAK" << '\n';
  47.   cout << sta.size() << ' ';
  48.   for (auto &i : sta) cout << i << ' ';
  49.   cout << '\n';
  50. }
  51.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement