Submission #1100239

# Submission time Handle Problem Language Result Execution time Memory
1100239 2024-10-13T10:42:07 Z vjudge1 Birthday gift (IZhO18_treearray) C++17
0 / 100
1 ms 592 KB
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

const int N = 100;
int a[N], n, m, q;
vector<int> g[N];
int depth[N], parent[N][20];


void dfs(int v, int p) {
    parent[v][0] = p;
    for (int i = 1; i < 20; i++) {
        parent[v][i] = parent[parent[v][i-1]][i-1];
    }
    for (int to : g[v]) {
        if (to != p) {
            depth[to] = depth[v] + 1;
            dfs(to, v);
        }
    }
}


int lca(int u, int v) {
    if (depth[u] < depth[v]) swap(u, v);
    for (int i = 19; i >= 0; i--) {
        if (depth[u] - (1 << i) >= depth[v]) {
            u = parent[u][i];
        }
    }
    if (u == v) return u;
    for (int i = 19; i >= 0; i--) {
        if (parent[u][i] != parent[v][i]) {
            u = parent[u][i];
            v = parent[v][i];
        }
    }
    return parent[u][0];
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    
    cin >> n >> m >> q;
    for (int i = 1; i < n; i++) {
        int u, v;
        cin >> u >> v;
        g[u].push_back(v);
        g[v].push_back(u);
    }
    for (int i = 1; i <= m; i++) {
        cin >> a[i];
    }

    
    depth[1] = 0;
    dfs(1, 0);

    while (q--) {
        int type;
        cin >> type;
        if (type == 1) {
           
            int pos, v;
            cin >> pos >> v;
            a[pos] = v;
        } else {
            int l, r, v;
            cin >> l >> r >> v;
            bool found = false;
            for (int x = l; x <= r; x++) {
                for (int y = x; y <= r; y++) {
                    if (lca(a[x], a[y]) == v) {
                        cout << x << " " << y << "\n";
                        found = true;
                        break;
                    }
                }
                if (found) break;
            }
            if (!found) {
                cout << "-1 -1\n";
            }
        }
    }

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB n=5
2 Runtime error 1 ms 592 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB n=5
2 Runtime error 1 ms 592 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB n=5
2 Runtime error 1 ms 592 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB n=5
2 Runtime error 1 ms 592 KB Execution killed with signal 11
3 Halted 0 ms 0 KB -