제출 #576334

#제출 시각아이디문제언어결과실행 시간메모리
576334eecsThrough Another Maze Darkly (CCO21_day1problem3)C++17
4 / 25
5437 ms1048576 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int maxn = 800010; int n, q, ptr[maxn], fa[maxn]; bool vis[maxn]; vector<int> G[maxn]; int main() { ios::sync_with_stdio(0), cin.tie(0); cin >> n >> q; for (int i = 1, k; i <= n; i++) { cin >> k; while (k--) { int c; cin >> c, G[i].push_back(c); } } auto dfs = [&](auto self, int u) -> void { for (int v : G[u]) if (v ^ fa[u]) { fa[v] = u, self(self, v); } }; dfs(dfs, 1); vector<int> tmp; ll step = 0; for (int u = 1, cnt = 0; cnt < n || u > 1; step++) { if (!vis[u]) vis[u] = 1, cnt++; tmp.push_back(u = G[u][++ptr[u] %= G[u].size()]); } int u = 1; vector<int> res; for (int i = 0; i < 2 * n - 2; i++) { res.push_back(u), u = G[u][++ptr[u] %= G[u].size()]; } while (q--) { ll k; cin >> k; if (k < step) cout << tmp[k - 1] << "\n"; else cout << res[(k - step) % (2 * n - 2)] << "\n"; } return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...