Submission #696387

# Submission time Handle Problem Language Result Execution time Memory
696387 2023-02-06T11:11:37 Z Cross_Ratio Through Another Maze Darkly (CCO21_day1problem3) C++14
0 / 25
470 ms 1048580 KB
#include <bits/stdc++.h>
#define int long long
using namespace std;
vector<int> adj[800005];
int num[800005];
void dfs(int c, int p) {
    int i, j;
    if(p == -1) {
        int n = num[c];
        vector<int> V(adj[c].size());
        int sz = adj[c].size();
        for(i=0;i<sz;i++) {
            V[i] = adj[c][(i+n)%sz];
        }
        adj[c] = V;
        num[c] = (num[c] + sz - n) % sz;
    }
    else {
        int n = -1;
        int sz = adj[c].size();
        for(i=0;i<sz;i++) {
            if(adj[c][i]==p) n = i;
        }
        vector<int> V(sz);
        for(i=0;i<sz;i++) {
            V[i] = adj[c][(i+n)%sz];
        }
        adj[c] = V;
        num[c] = (num[c] + sz - n) % sz;
    }
    for(int n : adj[c]) {
        if(n ==p) continue;
        dfs(n, c);
    }
}
vector<int> Query[50050000];
int ans[800005];
signed main() {
    cin.sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int N, Q;
    cin >> N >> Q;
    int i, j;
    for(i=0;i<N;i++) {
        int k;
        cin >> k;
        for(j=0;j<k;j++) {
            int c;
            cin >> c;
            adj[i].push_back(c-1);
        }
        num[i] = 1 % k;
    }
    dfs(0, -1);
    int ma = 0;
    for(i=0;i<Q;i++) {
        int a;
        cin >> a;
        if(a >= 2*N*N) {
            a = (a - 2*N*N) % (2*(N-1)) + 2*N*N;
        }
        ma = max(ma, a);
        Query[a].push_back(i);
    }
    int pt = 0, cnt = 0;
    while(cnt <= ma) {
        cnt++;
        int p2 = adj[pt][num[pt]];
        num[pt] = (num[pt] + 1) % adj[pt].size();
        pt = p2;
        for(int n :Query[cnt]) ans[n] = pt;
    }
    for(i=0;i<Q;i++) cout << ans[i]+1 << '\n';
}

Compilation message

Main.cpp: In function 'void dfs(long long int, long long int)':
Main.cpp:7:12: warning: unused variable 'j' [-Wunused-variable]
    7 |     int i, j;
      |            ^
# Verdict Execution time Memory Grader output
1 Runtime error 470 ms 1048576 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 459 ms 1048580 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 424 ms 1048576 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 470 ms 1048576 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -