#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);
while (q--) {
ll k;
cin >> k;
fill(ptr + 1, ptr + n + 1, 0);
fill(vis + 1, vis + n + 1, 0);
int u = 1, cnt = 0;
do {
if (!vis[u]) vis[u] = 1, cnt++;
u = G[u][++ptr[u] %= G[u].size()];
} while (--k && cnt < n);
if (!k) { cout << u << "\n"; continue; }
k %= 2 * n - 2;
while (k--) u = G[u][++ptr[u] %= G[u].size()];
cout << u << "\n";
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
19028 KB |
Output is correct |
2 |
Execution timed out |
9030 ms |
19796 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
19028 KB |
Output is correct |
2 |
Correct |
166 ms |
19124 KB |
Output is correct |
3 |
Correct |
3602 ms |
19168 KB |
Output is correct |
4 |
Correct |
407 ms |
19288 KB |
Output is correct |
5 |
Correct |
453 ms |
19276 KB |
Output is correct |
6 |
Correct |
621 ms |
19196 KB |
Output is correct |
7 |
Correct |
1081 ms |
19468 KB |
Output is correct |
8 |
Correct |
3240 ms |
19196 KB |
Output is correct |
9 |
Execution timed out |
9049 ms |
19216 KB |
Time limit exceeded |
10 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
19540 KB |
Output is correct |
2 |
Correct |
75 ms |
21700 KB |
Output is correct |
3 |
Execution timed out |
9036 ms |
24512 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
19028 KB |
Output is correct |
2 |
Execution timed out |
9030 ms |
19796 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |