This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
const long long inf = (long long) 1e18 + 10;
const int inf1 = (int) 1e9 + 10;
#define int long long
#define dbl long double
#define endl '\n'
#define sc second
#define fr first
#define mp make_pair
#define pb push_back
#define all(x) x.begin(), x.end()
const int maxn = 8e4+10;
const int lg = 60;
int n, q, q0, rt[2*maxn], pt[maxn];
vector<int> g[maxn];
map<int,int> id[maxn];
vector<pair<int,int>> dp[lg+1][maxn];
void solve() {
cin >> n >> q;
q0 = 2*n;
for(int i = 1; i <= n; i++) {
int cnt; cin >> cnt;
for(int j = 0; j < cnt; j++) {
int v; cin >> v;
g[i].pb(v);
}
pt[i] = 0;
}
rt[0] = 1;
for(int i = 1; i <= q0; i++) {
pt[rt[i-1]] = (pt[rt[i-1]]+1)%g[rt[i-1]].size();
rt[i] = g[rt[i-1]][pt[rt[i-1]]];
}
for(int i = 1; i <= n; i++) {
int cnt = g[i].size();
vector<int> newg;
for(int j = pt[i]; j < g[i].size(); j++) newg.pb(g[i][j]);
for(int j = 0; j < pt[i]; j++) newg.pb(g[i][j]);
g[i] = newg;
for(int j = 0; j < cnt; j++) {
int v = g[i][j];
id[i][v] = j;
}
for(int j = 0; j < cnt; j++) {
// cout << j << " " << (j+1)%cnt << " " << g[i].size() << " " << g[i][(j+1)%cnt] << endl;
dp[0][i].pb(mp(g[i][(j+1)%cnt],i));
}
}
for(int b = 1; b <= lg; b++) {
for(int u = 1; u <= n; u++) {
for(int i = 0; i < g[u].size(); i++) {
int v = g[u][i];
auto aux = dp[b-1][u][i];
int u1 = aux.fr;
int v1 = aux.sc;
dp[b][u].pb(dp[b-1][u1][id[u1][v1]]);
}
}
}
while(q--) {
int k; cin >> k;
if(k <= q0) {
cout << rt[k] << endl;
continue;
}
int u = 1;
int v = g[u][0];
for(int b = 0; b <= lg; b++) {
if((1LL<<b)&k) {
auto aux = dp[b][u][id[u][v]];
u = aux.fr;
v = aux.sc;
}
}
cout << u << endl;
}
}
int32_t main() {
ios::sync_with_stdio(false); cin.tie(0);
// freopen("in.in", "r", stdin);
// freopen("out.out", "w", stdout);
int tt = 1;
// cin >> tt;
while(tt--) {
solve();
}
}
Compilation message (stderr)
Main.cpp: In function 'void solve()':
Main.cpp:47:30: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
47 | for(int j = pt[i]; j < g[i].size(); j++) newg.pb(g[i][j]);
| ~~^~~~~~~~~~~~~
Main.cpp:63:30: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
63 | for(int i = 0; i < g[u].size(); i++) {
| ~~^~~~~~~~~~~~~
Main.cpp:64:21: warning: unused variable 'v' [-Wunused-variable]
64 | int v = g[u][i];
| ^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |