Cod sursa(job #3248219)

Utilizator MagicantPlusIuoras Andrei MagicantPlus Data 11 octombrie 2024 08:34:25
Problema Lowest Common Ancestor Scor 20
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 4.62 kb
#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define ull unsigned ll
#define vec vector
#define flist forward_list
#define um unordered_map
#define us unordered_set
#define prioq priority_queue
#define all(x) x.begin(), x.end()
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define mp make_pair
#define mt make_tuple
#define ld long double
#define pll pair<ll, ll>

#define LL_MAX LLONG_MAX
#define LL_MIN LLONG_MIN
#define ULL_MAX ULLONG_MAX
#define ULL_MIN 0
#define LD_MAX LDBL_MAX
#define LD_MIN LDBL_MIN

#define nl '\n'

#define vv(type, name, n, ...) vector<vector<type>> name(n, vector<type>(__VA_ARGS__))
#define vvv(type, name, n, m, ...)                                                                                     \
    vector<vector<vector<type>>> name(n, vector<vector<type>>(m, vector<type>(__VA_ARGS__)))

// https://trap.jp/post/1224/
#define rep1(n) for(ll i=0; i<(ll)(n); ++i)
#define rep2(i,n) for(ll i=0; i<(ll)(n); ++i)
#define rep3(i,a,b) for(ll i=(ll)(a); i<(ll)(b); ++i)
#define rep4(i,a,b,c) for(ll i=(ll)(a); i<(ll)(b); i+=(c))
#define cut4(a,b,c,d,e,...) e
#define rep(...) cut4(__VA_ARGS__,rep4,rep3,rep2,rep1)(__VA_ARGS__)
#define rep1e(n) for(ll i=1; i<=(ll)(n); ++i)
#define rep2e(i,n) for(ll i=1; i<=(ll)(n); ++i)
#define rep3e(i,a,b) for(ll i=(ll)(a); i<=(ll)(b); ++i)
#define rep4e(i,a,b,c) for(ll i=(ll)(a); i<=(ll)(b); i+=(c))
#define repe(...) cut4(__VA_ARGS__,rep4e,rep3e,rep2e,rep1e)(__VA_ARGS__)
#define per1(n) for(ll i=((ll)n)-1; i>=0; --i)
#define per2(i,n) for(ll i=((ll)n)-1; i>=0; --i)
#define per3(i,a,b) for(ll i=((ll)a)-1; i>=(ll)(b); --i)
#define per4(i,a,b,c) for(ll i=((ll)a)-1; i>=(ll)(b); i-=(c))
#define per(...) cut4(__VA_ARGS__,per4,per3,per2,per1)(__VA_ARGS__)
#define per1e(n) for(ll i=((ll)n); i>=1; --i)
#define per2e(i,n) for(ll i=((ll)n); i>=1; --i)
#define per3e(i,a,b) for(ll i=((ll)a); i>=(ll)(b); --i)
#define per4e(i,a,b,c) for(ll i=((ll)a); i>=(ll)(b); i-=(c))
#define pere(...) cut4(__VA_ARGS__,per4e,per3e,per2e,per1e)(__VA_ARGS__)
#define rep_subset(i,s) for(ll i=(s); i>=0; i=(i==0?-1:(i-1)&(s)))
// #define forit(v) for (auto it = v.begin(); it != v.end(); it++)
// #define forit2(it, v) for (auto it = v.begin(); it != v.end(); it++)
// #define forrit(v) for (auto it = v.rbegin(); it != v.rend(); it++)
// #define forrit2(it, v) for (auto it = v.rbegin(); it != v.rend(); it++)

// #ifdef i_am_noob
#define bug(...) cerr << "#" << __LINE__ << ' ' << #__VA_ARGS__ << "- ", _do(__VA_ARGS__)
template<typename T> void _do(vector<vector<T>> x){cerr << "\n"; for(auto i: x) {for(auto j : i) {cerr << j << ' ';} cerr << "\n";}}
template<typename T> void _do(vector<T> x){for(auto i: x) cerr << i << ' ';cerr << "\n";}
template<typename T> void _do(set<T> x){for(auto i: x) cerr << i << ' ';cerr << "\n";}
template<typename T> void _do(unordered_set<T> x){for(auto i: x) cerr << i << ' ';cerr << "\n";}
template<typename T> void _do(T && x) {cerr << x << endl;}
template<typename T, typename ...S> void _do(T && x, S&&...y) {cerr << x << ", "; _do(y...);}
// #else
// #define bug(...) 777771449
// #endif

const ll MOD = 1e9 + 7;

const string FILENAME = "lca";
ifstream fin(FILENAME + ".in");
ofstream fout(FILENAME + ".out");

#define cin fin
#define cout fout

class DSU
{
public:
	
	ll n;
	vec<ll> parent, size;
	
	void build(ll x)
	{
		n = x;
		parent.assign(n, -1);
		size.assign(n, 1);
	}
	ll find(ll x)
	{
		if(parent[x] == -1) return x;
		return parent[x] = find(parent[x]);
	}
	void unite(ll a, ll b)
	{
		ll reprA, reprB;
		reprA = find(a);
		reprB = find(b);
		
		if(reprA == reprB) return;
		
		if(size[reprA] > size[reprB])
		{
			parent[reprB] = reprA;
			size[reprA] += size[reprB];
			size[reprB] = size[reprA];
		}
		else
		{
			parent[reprA] = reprB;
			size[reprB] += size[reprA];
			size[reprA] = size[reprB];
		}
	}
};

ll n, q;
vec<flist<ll>> adj;
vec<ll> queryAns;
vec<um<ll, ll>> P;
vec<bool> black;
DSU sets;

void dfs(ll u, ll parent)
{
	for(auto v : adj[u])
	{
		if(v == parent) continue;
		dfs(v, u);
		sets.unite(u, v);
		sets.parent[sets.find(v)] = u;
		sets.parent[u] = -1;
	}
	
	black[u] = 1;
	flist<ll> del;
	for(auto pa : P[u])
	{
		ll w = pa.fi, ind = pa.se;
		
		if(black[w])
		{
			queryAns[ind] = sets.find(w);
			del.pf(w);
		}
	}
	
	for(auto w : del) P[u].erase(w), P[w].erase(u);
}

int main()
{
	cin >> n >> q;
	adj.resize(n);
	P.resize(n);
	black.assign(n, 0);
	sets.build(n);
	queryAns.resize(q);
	
	rep(n - 1)
	{
		ll x;
		cin >> x;
		x--;
		adj[i + 1].pf(x);
		adj[x].pf(i+1);
	}
	
	rep(q)
	{
		ll a, b;
		cin >> a >> b;
		a--, b--;
		
		P[a][b] = i;
		P[b][a] = i;
	}
	
	dfs(0, -1);
	
	for(auto x : queryAns) cout << x + 1 << nl;
	
    return 0;
}