Language: C++

#include <iostream>
#include <cstdio>
#include <vector>
#include <forward_list>
#include <map>
#include <utility>

#define MAXN 100000

using namespace std;

int pre[MAXN] = {-1};
int st[MAXN];

int search[MAXN][MAXN];

vector<pair<int,int>> vp;
map<pair<int,int>,int> mans;

void swap(int &i,int &j){
    int t = i;
    i = j;
    j = t;
}


class Edge{
public:
    int s;
    int t;
    Edge(int si,int ti):s(si),t(ti){;}
};

class Graph{
public:
    int V;
    vector<forward_list<int>> adj;
    Graph(int v):V(v){
        adj.resize(v);
        for(int i = 0; i < V;i++) {st[i] = i;pre[i] = -1;}
        }
    void addEdge(int i,int j){adj[i].push_front(j);}
    void tarjanR(Edge e);
};

int root(int v){
    while(st[v] != v) v = st[v];
    return v;
}

void Graph::tarjanR(Edge e){
    int t = e.t;
    int s = e.s;
    pre[t] = 0;

    for(auto a = adj[t].begin();a != adj[t].end();++a){
        int v = *a;
        if(pre[v] == -1){
            tarjanR(Edge(t,v));
        }
    }
    for(int i = 0; i < V;i++){
        int mi = i,ma = t;
        if(mi > ma) swap(mi,ma);
        pair<int,int> p = make_pair(mi,ma);
        if(pre[i] != -1 && mans.find(p) != mans.end() && mans[p] == -1){
            mans[p] = root(i);
        }
    }
    st[t] = s;
}


int main()
{
    int V,j,q,tt;
    scanf("%d",&V);
    Graph G(V);
    for(int i = 0; i < V; i++){
        scanf("%d",&j);
        for(int k = 0; k < j;k++)
        {
            scanf("%d",&tt);
            G.addEdge(i,tt);
        }
    }

    int s,t;

    scanf("%d",&q);
    for(int i = 0; i < q;i++){
        scanf("%d %d",&s,&t);
        if(s > t){swap(s,t);}
        vp.push_back(make_pair(s,t));
        mans[make_pair(s,t)] = -1;
    }

    G.tarjanR(Edge(0,0));

    for(int i = 0; i < q;i++){
        printf("%d\n",mans[vp[i]]);
    }

    return 0;
}



Compiler output:
/tmp/cc4zow0o.o: in function `Graph::tarjanR(Edge)':
a.cc:(.text+0x1da): relocation truncated to fit: R_X86_64_PC32 against symbol `mans' defined in .bss section in /tmp/cc4zow0o.o
a.cc:(.text+0x1f4): relocation truncated to fit: R_X86_64_PC32 against symbol `mans' defined in .bss section in /tmp/cc4zow0o.o
a.cc:(.text+0x225): relocation truncated to fit: R_X86_64_PC32 against symbol `mans' defined in .bss section in /tmp/cc4zow0o.o
a.cc:(.text+0x25e): relocation truncated to fit: R_X86_64_PC32 against symbol `mans' defined in .bss section in /tmp/cc4zow0o.o
/tmp/cc4zow0o.o: in function `main':
a.cc:(.text+0x3cd): relocation truncated to fit: R_X86_64_PC32 against symbol `vp' defined in .bss section in /tmp/cc4zow0o.o
a.cc:(.text+0x3fa): relocation truncated to fit: R_X86_64_PC32 against symbol `mans' defined in .bss section in /tmp/cc4zow0o.o
a.cc:(.text+0x459): relocation truncated to fit: R_X86_64_PC32 against symbol `vp' defined in .bss section in /tmp/cc4zow0o.o
a.cc:(.text+0x46b): relocation truncated to fit: R_X86_64_PC32 against symbol `mans' defined in .bss section in /tmp/cc4zow0o.o
/tmp/cc4zow0o.o: in function `__static_initialization_and_destruction_0()':
a.cc:(.text+0x4d7): relocation truncated to fit: R_X86_64_PC32 against symbol `vp' defined in .bss section in /tmp/cc4zow0o.o
a.cc:(.text+0x4f0): relocation truncated to fit: R_X86_64_PC32 against symbol `vp' defined in .bss section in /tmp/cc4zow0o.o
a.cc:(.text+0x509): additional relocation overflows omitted from the output
collect2: error: ld returned 1 exit status