답안 #1016696

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1016696 2024-07-08T10:33:42 Z amine_aroua Prize (CEOI22_prize) C++17
0 / 100
1829 ms 362320 KB
#include <bits/stdc++.h>
using namespace std;
int n, k , q,t;
vector<vector<int>> adj[2];
int root[2];
vector<int> depth[2];
vector<int> dist[2];
vector<vector<int>> up[2];
void dfs(int i,int x)
{
    for(auto u : adj[i][x])
    {
        depth[i][u] = depth[i][x] + 1;
        up[i][0][u] = x;
        for(int j = 1 ; j < 20 ; j++)
        {
            up[i][j][u] = up[i][j - 1][up[i][j - 1][u]];
        }
        dfs(i , u);
    }
}
void lift(int i , int &u , int x)
{
    for(int j = 0 ; j < 20 ; j++)
        if((1<<j) & x)
            u = up[i][j][u];
}
int lca(int i ,int u , int v)
{
    if(depth[i][u] < depth[i][v])
        swap(u , v);
    int x = depth[i][u] - depth[i][v];
    lift(i , u , x);
    if(u == v)
        return u;
    for(int j = 19 ; j >= 0 ; j--)
    {
        int nu = up[i][j][u] , nv = up[i][j][v];
        if(nu == nv)
            continue;
        u = nu , v = nv;
    }
    return up[i][0][u];
}
void askO(int a , int b)
{
    cout<<"? "<<a<<" "<<b<<endl;
}
vector<int> askI(int a , int b)
{
    vector<int> ret(4);
    for(auto &x : ret)
        cin>>x;
    return ret;
}
int main() {
    cin.tie(0);
    cout.tie(0);
    cin>>n>>k>>q>>t;
    for(int i = 0 ; i < 2 ; i++)
    {
        adj[i].assign(n +1 , {});
        depth[i].assign(n + 1 , 0);
        dist[i].assign(n + 1 , 0);
        up[i].assign(20 , vector<int>(n + 1,  0));
    }
    for(int i = 0 ; i < 2 ; i++)
    {
        for(int j = 1; j <= n ; j++)
        {
            int x;
            cin>>x;
            if(x == -1)
            {
                root[i] = j;
                continue;
            }
            adj[i][x].push_back(j);
        }
        dfs(i , root[i]);
    }
    vector<int> subset;
    queue<int> Q;
    Q.push(root[0]);
    int cnt = 0;
    while (cnt < k)
    {
        int tp = Q.front();
        Q.pop();
        subset.push_back(tp);
        cnt++;
        for(auto u : adj[0][tp])
            Q.push(u);
    }
    cout<<subset[0];
    for(auto node : subset)
    {
        cout<<" "<<node;
    }
    cout<<endl;
    for(auto node : subset)
    {
        if(node != root[0])
        {
            askO(root[0] , node);
        }
    }
    cout<<"!"<<endl;
    for(auto node : subset)
    {
        if(node != root[0])
            dist[0][node] = askI(root[0] , node)[1];
    }
    vector<pair<int ,int>> queries;
    while(t--)
    {
        int u , v;
        cin>>u>>v;
        queries.push_back({u , v});
    }
    for(auto [u , v] : queries)
    {
        cout<<dist[0][u] + dist[0][v] - 2 * dist[0][lca(0 , u , v)]<<" "<<dist[0][u] + dist[0][v] - 2 * dist[0][lca(0 , u , v)]<<endl;
    }
}
# 결과 실행 시간 메모리 Grader output
1 Runtime error 688 ms 180964 KB Execution killed with signal 13
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 714 ms 181228 KB Execution killed with signal 13
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 669 ms 181228 KB Execution killed with signal 13
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1651 ms 362320 KB Execution killed with signal 13
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1829 ms 361532 KB Execution killed with signal 13
2 Halted 0 ms 0 KB -