Language: C++

#include <bits/stdc++.h>
using namespace std;

class Graph
{
  public:
    struct Edge
    {
        long long to;
        long long distance;
        Edge(long long t, long long d)
        {
            to = t;
            distance = d;
        }
        bool operator<(const Edge &e) const
        {
            return distance < e.distance;
        }
        bool operator<=(const Edge &e) const
        {
            return distance <= e.distance;
        }
        bool operator>(const Edge &e) const
        {
            return distance > e.distance;
        }
        bool operator>=(const Edge &e) const
        {
            return distance >= e.distance;
        }
        bool operator==(const Edge &e) const
        {
            return distance == e.distance;
        }
    };
    vector<vector<long long>> adjacency_matrix;
    long long node;
    long long initial_value;
    vector<list<Edge>> edge;

    Graph(long long N)
    {
        setNode(N);
        initializeEdgeList();
    }

    Graph(long long N, long long init)
    {
        setNode(N);
        initializeEdgeList();
        setInitialValue(init);
    }

    void setNode(long long N)
    {
        node = N + 1;
    }

    void setInitialValue(long long init)
    {
        initial_value = init;
    }

    void initializeAdjacencyMatrix()
    {
        adjacency_matrix = vector<vector<long long>>(
            node, vector<long long>(node, initial_value));
    }

    void initializeAdjacencyMatrix(long long init)
    {
        setInitialValue(init);
        initializeAdjacencyMatrix();
    }

    void initializeEdgeList()
    {
        edge = vector<list<Edge>>(node);
    }

    void updateAdjacencyMatrix(long long from, long long to, long long distance)
    {
        adjacency_matrix[from][to] = distance;
    }

    void addEdge(long long from, long long to, long long distance)
    {
        edge[from].push_back({to, distance});
    }

    // O(V^3)
    // CAUTION: Use adjacency matrix
    void runFloydWarshall()
    {
        for (int i = 0; i < node; i++)
        {
            for (int j = 0; j < node; j++)
            {
                for (int k = 0; k < node; k++)
                {
                    adjacency_matrix[j][k] = min(
                        adjacency_matrix[j][k],
                        adjacency_matrix[j][i] + adjacency_matrix[i][k]);
                }
            }
        }
    }

    // O(VE)
    // CAUTION: Use edge list
    vector<long long> runBellmanFord(long long from)
    {
        vector<long long> distance(node, initial_value);
        distance[from] = 0;

        for (long long i = 0; i < node - 1; i++)
        {
            for (long long j = 1; j < node; j++)
            {
                for (auto itr = edge[j].begin(); itr != edge[j].end(); itr++)
                {
                    if (distance[itr->to] > distance[j] + itr->distance)
                    {
                        distance[itr->to] = distance[j] + itr->distance;
                    }
                }
            }
        }

        for (long long i = 1; i < node; i++)
        {
            for (auto itr = edge[i].begin(); itr != edge[i].end(); itr++)
            {
                if (distance[itr->to] > distance[i] + itr->distance)
                {
                    distance[0]--;
                }
            }
        }
        return distance;
    }

    // O(E + VlogV)
    // CAUTION: Use edge list
    vector<long long> runDijkstra(long long from)
    {
        vector<long long> distance(node, initial_value);
        priority_queue<Edge, vector<Edge>, greater<Edge>> queue;
        queue.push(Edge(from, 0));
        while (!queue.empty())
        {
            long long d = queue.top().distance;
            long long to = queue.top().to;
            queue.pop();
            if (distance[to] == initial_value)
            {
                distance[to] = d;
                for (auto itr = edge[to].begin(); itr != edge[to].end(); itr++)
                {
                    if (distance[itr->to] == initial_value)
                    {
                        queue.push({itr->to, itr->distance + d});
                    }
                }
            }
        }
        return distance;
    }

    // O(ElogV)
    // CAUTION: Use adjacency matrix
    long long runPrim()
    {
        vector<bool> is_visited(node, false);
        long long cost = 0;
        priority_queue<Edge> queue;
        queue.push(Edge(1, 0));
        while (!queue.empty())
        {
            long long distance = queue.top().distance;
            long long to = queue.top().to;
            queue.pop();
            if (!is_visited[to])
            {
                cost += distance;
                is_visited[to] = true;
                for (auto itr = edge[to].begin(); itr != edge[to].end(); itr++)
                {
                    queue.push({itr->to, itr->distance});
                }
            }
        }
        return cost;
    }

    long long runKruskal()
    {
    }
};

int main()
{
    int N;
    cin >> N;

    Graph g(N, -1);
    int a;
    for (int i = 1; i <= N; i++)
    {
        cin >> a;
        g.addEdge(i, a, 1);
    }
    vector<int> distance = g.runDijkstra(1);
    cout << distance[2] << endl;
}


Compiler output:
a.cc: In member function 'long long int Graph::runKruskal()':
a.cc:199:5: warning: no return statement in function returning non-void [-Wreturn-type]
  199 |     }
      |     ^
a.cc: In function 'int main()':
a.cc:214:41: error: conversion from 'vector<long long int>' to non-scalar type 'vector<int>' requested
  214 |     vector<int> distance = g.runDijkstra(1);
      |                            ~~~~~~~~~~~~~^~~