답안 #1044126

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1044126 2024-08-05T07:30:44 Z 정희우(#11008) Prize (CEOI22_prize) C++17
0 / 100
2128 ms 357100 KB
#include<iostream>
#include<algorithm>
#include<vector>

using namespace std;
using lint = long long;
using vint = vector<int>;
using pii = pair<int,int>;

const int MAX_N=1000010;
const int BN=20;

int n,k,q1,q2;
vint edge[2][MAX_N];
int root[2];
int depth[2][MAX_N];
int ac[2][MAX_N][BN];
vint vert;
int idx[MAX_N];
lint dist[2][MAX_N];

void fillac(int v,int p,int t)
{
    depth[t][v]=depth[t][p]+1;
    ac[t][v][0]=p;
    for(int i=1;i<BN;i++)
        ac[t][v][i]=ac[t][ac[t][v][i-1]][i-1];
    for(auto v0 : edge[t][v])
        if(v0!=p)
            fillac(v0,v,t);
}

void dfs(int v,int p,int t,int &c)
{
    if(t)
        idx[v]=c++;
    else if(vert.size()==k)
        return;
    else
        vert.push_back(v);
    for(auto v0 : edge[t][v])
        if(v0!=p)
            dfs(v0,v,t,c);
}

int lca(int u,int v,int t)
{
    if(depth[t][u]>depth[t][v])swap(u,v);
    for(int i=BN-1;i>=0;i--)
        if(depth[t][ac[t][v][i]]>=depth[t][u])
            v=ac[t][v][i];
    if(u==v)return v;
    for(int i=BN-1;i>=0;i--)
        if(ac[t][v][i]==ac[t][u][i])
        {
            v=ac[t][v][i];
            u=ac[t][u][i];
        }
    return ac[t][v][0];
}

bool comp(const int &i,const int &j)
{
    return idx[i]<idx[j];
}

int main()
{
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    cin >> n >> k >> q1 >> q2;
    for(int t=0;t<2;t++)
        for(int i=1;i<=n;i++)
        {
            int p;
            cin >> p;
            if(p==-1)
            {
                root[t]=i;
                continue;
            }
            edge[t][i].push_back(p);
            edge[t][p].push_back(i);
        }
    for(int t=0;t<2;t++)
    {
        int c=0;
        fillac(root[t],root[t],t);
        dfs(root[1],root[1],t,c);
    }
    sort(vert.begin(),vert.end(),comp);
    for(auto v : vert)
        cout << v << ' ';
    cout << '\n';
    for(int i=0;i<k-1;i++)
        cout << "? " << vert[i] << ' ' << vert[i+1] << '\n';
    cout << "!\n" << flush;
    for(int i=0;i<k-1;i++)
    {
        lint dup,ddown;
        int u=vert[i],v=vert[i+1];
        for(int t=0;t<2;t++)
        {
            cin >> dup >> ddown;
            int l=lca(u,v,t);
            dist[t][l]=dist[t][u]-dup;
            dist[t][v]=dist[t][u]-dup+ddown;
        }
    }
    for(int i=0;i<q2;i++)
    {
        int u,v;
        cin >> u >> v;
        for(int t=0;t<2;t++)
        {
            int l=lca(u,v,t);
            cout << dist[t][u]+dist[t][v]-dist[t][l]*2 << ' ';
        }
        cout << '\n';
    }
    return 0;
}

Compilation message

Main.cpp: In function 'void dfs(int, int, int, int&)':
Main.cpp:37:24: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   37 |     else if(vert.size()==k)
      |             ~~~~~~~~~~~^~~
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 698 ms 213688 KB Time limit exceeded (wall clock)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 993 ms 212132 KB Time limit exceeded (wall clock)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 769 ms 209708 KB Time limit exceeded (wall clock)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1801 ms 353816 KB Time limit exceeded (wall clock)
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 2128 ms 357100 KB Time limit exceeded (wall clock)
2 Halted 0 ms 0 KB -