Language: C++

class AdjListNode {  
    int v;  
    int weight;  
    
public:  
    AdjListNode(int _v, int _w)  
    {  
        v = _v;  
        weight = _w;  
    }  
    int getV() { return v; }  
    int getWeight() { return weight; }  
};  
    
// Class to represent a graph using adjacency list  
// representation  
class Graph {  
    int V; // No. of vertices'  
    
    // Pointer to an array containing adjacency lists  
    list<AdjListNode>* adj;  
    
    // A function used by longestPath  
    void topologicalSortUtil(int v, bool visited[],  
                             stack<int>& Stack);  
    
public:  
    Graph(int V); // Constructor  
    ~Graph(); // Destructor 
  
    // function to add an edge to graph  
    void addEdge(int u, int v, int weight);  
    
    // Finds longest distances from given source vertex  
    void longestPath(int s);  
};  
    
Graph::Graph(int V) // Constructor  
{  
    this->V = V;  
    adj = new list<AdjListNode>[V];  
}  
  
Graph::~Graph() // Destructor  
{  
    delete [] adj;  
}  
  
  
void Graph::addEdge(int u, int v, int weight)  
{  
    AdjListNode node(v, weight);  
    adj[u].push_back(node); // Add v to u's list  
}  
    
// A recursive function used by longestPath. See below  
// link for details  
// https:// www.geeksforgeeks.org/topological-sorting/  
void Graph::topologicalSortUtil(int v, bool visited[],  
                                stack<int>& Stack)  
{  
    // Mark the current node as visited  
    visited[v] = true;  
    
    // Recur for all the vertices adjacent to this vertex  
    list<AdjListNode>::iterator i;  
    for (i = adj[v].begin(); i != adj[v].end(); ++i) {  
        AdjListNode node = *i;  
        if (!visited[node.getV()])  
            topologicalSortUtil(node.getV(), visited, Stack);  
    }  
    
    // Push current vertex to stack which stores topological  
    // sort  
    Stack.push(v);  
}  
    
// The function to find longest distances from a given vertex.  
// It uses recursive topologicalSortUtil() to get topological  
// sorting.  
void Graph::longestPath(int s)  
{  
    stack<int> Stack;  
    int dist[V];  
    
    // Mark all the vertices as not visited  
    bool* visited = new bool[V];  
    for (int i = 0; i < V; i++)  
        visited[i] = false;  
    
    // Call the recursive helper function to store Topological  
    // Sort starting from all vertices one by one  
    for (int i = 0; i < V; i++)  
        if (visited[i] == false)  
            topologicalSortUtil(i, visited, Stack);  
    
    // Initialize distances to all vertices as infinite and  
    // distance to source as 0  
    for (int i = 0; i < V; i++)  
        dist[i] = NINF;  
    dist[s] = 0;  
    
    // Process vertices in topological order  
    while (Stack.empty() == false) {  
        // Get the next vertex from topological order  
        int u = Stack.top();  
        Stack.pop();  
    
        // Update distances of all adjacent vertices  
        list<AdjListNode>::iterator i;  
        if (dist[u] != NINF) {  
            for (i = adj[u].begin(); i != adj[u].end(); ++i)  
                if (dist[i->getV()] < dist[u] + i->getWeight())  
                    dist[i->getV()] = dist[u] + i->getWeight();  
        }  
    }  
    
    // Print the calculated longest distances  
    for (int i = 0; i < V; i++)  
        (dist[i] == NINF) ? cout << "INF " : cout << dist[i] << " "; 
      
    delete [] visited; 
}  
    
// Driver program to test above functions  
int main()  
{  
    // Create a graph given in the above diagram.  
    // Here vertex numbers are 0, 1, 2, 3, 4, 5 with  
    // following mappings:  
    // 0=r, 1=s, 2=t, 3=x, 4=y, 5=z  
    Graph g(6);  
    g.addEdge(0, 1, 5);  
    g.addEdge(0, 2, 3);  
    g.addEdge(1, 3, 6);  
    g.addEdge(1, 2, 2);  
    g.addEdge(2, 4, 4);  
    g.addEdge(2, 5, 2);  
    g.addEdge(2, 3, 7);  
    g.addEdge(3, 5, 1);  
    g.addEdge(3, 4, -1);  
    g.addEdge(4, 5, -2);  
    
    int s = 1;  
    cout << "Following are longest distances from "
            "source vertex "
         << s << " \n";  
    g.longestPath(s);  
    
    return 0;  
} 

Compiler output:
a.cc:21:5: error: 'list' does not name a type
   21 |     list<AdjListNode>* adj;
      |     ^~~~
a.cc:25:30: error: 'stack' has not been declared
   25 |                              stack<int>& Stack);
      |                              ^~~~~
a.cc:25:35: error: expected ',' or '...' before '<' token
   25 |                              stack<int>& Stack);
      |                                   ^
a.cc: In constructor 'Graph::Graph(int)':
a.cc:41:5: error: 'adj' was not declared in this scope
   41 |     adj = new list<AdjListNode>[V];
      |     ^~~
a.cc:41:15: error: 'list' does not name a type
   41 |     adj = new list<AdjListNode>[V];
      |               ^~~~
a.cc:41:31: error: expected primary-expression before '>' token
   41 |     adj = new list<AdjListNode>[V];
      |                               ^
a.cc: In lambda function:
a.cc:41:35: error: expected '{' before ';' token
   41 |     adj = new list<AdjListNode>[V];
      |                                   ^
a.cc: In destructor 'Graph::~Graph()':
a.cc:46:15: error: 'adj' was not declared in this scope
   46 |     delete [] adj;
      |               ^~~
a.cc: In member function 'void Graph::addEdge(int, int, int)':
a.cc:53:5: error: 'adj' was not declared in this scope
   53 |     adj[u].push_back(node); // Add v to u's list
      |     ^~~
a.cc: At global scope:
a.cc:60:33: error: 'stack' has not been declared
   60 |                                 stack<int>& Stack)
      |                                 ^~~~~
a.cc:60:38: error: expected ',' or '...' before '<' token
   60 |                                 stack<int>& Stack)
      |                                      ^
a.cc: In member function 'void Graph::topologicalSortUtil(int, bool*, int)':
a.cc:66:5: error: 'list' was not declared in this scope
   66 |     list<AdjListNode>::iterator i;
      |     ^~~~
a.cc:66:21: error: expected primary-expression before '>' token
   66 |     list<AdjListNode>::iterator i;
      |                     ^
a.cc:66:24: error: '::iterator' has not been declared
   66 |     list<AdjListNode>::iterator i;
      |                        ^~~~~~~~
a.cc:67:10: error: 'i' was not declared in this scope
   67 |     for (i = adj[v].begin(); i != adj[v].end(); ++i) {
      |          ^
a.cc:67:14: error: 'adj' was not declared in this scope
   67 |     for (i = adj[v].begin(); i != adj[v].end(); ++i) {
      |              ^~~
a.cc:70:55: error: 'Stack' was not declared in this scope
   70 |             topologicalSortUtil(node.getV(), visited, Stack);
      |                                                       ^~~~~
a.cc:75:5: error: 'Stack' was not declared in this scope
   75 |     Stack.push(v);
      |     ^~~~~
a.cc: In member function 'void Graph::longestPath(int)':
a.cc:83:5: error: 'stack' was not declared in this scope
   83 |     stack<int> Stack;
      |     ^~~~~
a.cc:83:11: error: expected primary-expression before 'int'
   83 |     stack<int> Stack;
      |           ^~~
a.cc:95:45: error: 'Stack' was not declared in this scope
   95 |             topologicalSortUtil(i, visited, Stack);
      |                                             ^~~~~
a.cc:100:19: error: 'NINF' was not declared in this scope
  100 |         dist[i] = NINF;
      |                   ^~~~
a.cc:104:12: error: 'Stack' was not declared in this scope
  104 |     while (Stack.empty() == false) {
      |            ^~~~~
a.cc:110:9: error: 'list' was not declared in this scope; did you mean 'dist'?
  110 |         list<AdjListNode>::iterator i;
      |         ^~~~
      |         dist
a.cc:110:25: error: expected primary-expression before '>' token
  110 |         list<AdjListNode>::iterator i;
      |                         ^
a.cc:110:28: error: '::iterator' has not been declared
  110 |         list<AdjListNode>::iterator i;
      |                            ^~~~~~~~
a.cc:111:24: error: 'NINF' was not declared in this scope
  111 |         if (dist[u] != NINF) {
      |                        ^~~~
a.cc:112:18: error: 'i' was not declared in this scope
  112 |             for (i = adj[u].begin(); i != adj[u].end(); ++i)
      |                  ^
a.cc:112:22: error: 'adj' was not declared in this scope
  112 |             for (i = adj[u].begin(); i != adj[u].end(); ++i)
      |                      ^~~
a.cc:120:21: error: 'NINF' was not declared in this scope
  120 |         (dist[i] == NINF) ? cout << "INF " : cout << dist[i] << " ";
      |                     ^~~~
a.cc:120:29: error: 'cout' was not declared in this scope
  120 |         (dist[i] == NINF) ? cout << "INF " : cout << dist[i] << " ";
      |                             ^~~~
a.cc: In function 'int main()':
a.cc:145:5: error: 'cout' was not declared in this scope
  145 |     cout << "Following are longest distances from "
      |     ^~~~