Cod sursa(job #3363418)

Utilizator Carnu_EmilianCarnu Emilian Carnu_Emilian Data 17 august 2026 20:07:12
Problema Lowest Common Ancestor Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 3.51 kb
#pragma GCC optimize("Ofast")
// #pragma GCC target("avx2")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
class InParser
{
    vector<char> str;
    int ptr;
    ifstream fin;

    char getChar()
    {
        if (ptr == int(str.size()))
        {
            fin.read(str.data(), str.size());
            ptr = 0;
        }
        return str[ptr++];
    }

    template<class T>
    T getInt()
    {
        char chr = getChar();
        while (!isdigit(chr) && chr != '-')
            chr = getChar();
        int sgn = +1;
        if (chr == '-')
        {
            sgn = -1;
            chr = getChar();
        }
        T num = 0;
        while (isdigit(chr))
        {
            num = num * 10 + chr - '0';
            chr = getChar();
        }
        return sgn * num;
    }

public:
    InParser(const char* name) : str(1e5), ptr(str.size()), fin(name) { }
    ~InParser()
    {
        fin.close();
    }

    template<class T>
    friend InParser& operator>>(InParser& in, T& num)
    {
        num = in.getInt<T>();
        return in;
    }
};

class OutParser
{
    vector<char> str;
    int ptr;
    ofstream fout;

    void putChar(char chr)
    {
        if (ptr == int(str.size()))
        {
            fout.write(str.data(), str.size());
            ptr = 0;
        }
        str[ptr++] = chr;
    }

    template<class T>
    void putInt(T num)
    {
        if (num < 0)
        {
            putChar('-');
            num *= -1;
        }
        if (num > 9)
            putInt(num / 10);
        putChar(num % 10 + '0');
    }

public:
    OutParser(const char* name) : str(1e5), ptr(0), fout(name) { }
    ~OutParser()
    {
        fout.write(str.data(), ptr);
        fout.close();
    }

    template<class T>
    friend OutParser& operator<<(OutParser& out, const T num)
    {
        out.putInt(num);
        return out;
    }

    friend OutParser& operator<<(OutParser& out, const char chr)
    {
        out.putChar(chr);
        return out;
    }

    friend OutParser& operator<<(OutParser& out, const char* str)
    {
        for (int i = 0; str[i]; i++)
            out.putChar(str[i]);
        return out;
    }
};

InParser fin("lca.in");
OutParser fout("lca.out");

const int N = 1e5 + 5;
int n, q, ans[N], t[N];
bool vis[N];
vector<int> adj[N];
vector<pair<int, int>> queries[N];

int Find(int x)
{
    int root, y;
    root = x;
    while (t[root] != 0)
    {
        root = t[root];
    }
    while (t[x] != 0)
    {
        y = t[x];
        t[x] = root;
        x = y;
    }
    // cout << x << ' ' << root << endl;
    return root;
}

void Union(int a, int b)
{
    a = Find(a);
    b = Find(b);
    t[a] = b;
}

void DFS(int node)
{
    vis[node] = true;
    for (int i : adj[node])
    {
        if (!vis[i])
        {
            DFS(i);
            Union(i, node);
        }
    }
    // cout << node <<": ";
    for (pair<int, int> &p : queries[node])
    {
        // cout << p.first << " ";
        if (vis[p.first])
        {
            ans[p.second] = Find(p.first);

        }

    }
    // cout << endl;
}

int main()
{
    fin >> n >> q;
    for (int i = 2; i <= n; i++)
    {
        int x;
        fin >> x;
        adj[x].push_back(i);
        adj[i].push_back(x);
    }
    for (int i = 1; i <= q; i++)
    {
        int a, b;
        fin >> a >> b;
        // cout << "a, b: " << a << ", " << b << "\n";
        queries[a].push_back({b, i});
        queries[b].push_back({a, i});
    }
    DFS(1);
    for (int i = 1; i <= q; i++)
        fout << ans[i] << "\n";
    return 0;
}