Language: C++

#include<bits/stdc++.h>
#define endl '\n'

using namespace std;

int vertices, aristas, a, b;
vector<int> grafo[100005];
int indegree[100005];
vector<int> orden_topologico;
int dp[100005];

bool dame_orden_topologico()
{
    queue<int> cola;
    for(int i = 1; i <= vertices; i++)
    {
        if(indegree[i] == 0)
            cola.push(i);
    }

    while(!cola.empty())
    {
        int elemento = cola.front();
        cola.pop();

        orden_topologico.push_back(elemento);

        for(int i = 0; i < grafo[elemento].size(); i++)
        {
            int temp = grafo[elemento][i];
            indegree[temp] -= 1;
            if(indegree[temp] == 0)
                cola.push(temp);
        }
    }

    // siempre es un DAG
    return true;
}

bool tiene_aristas(int v)
{
    return grafo[v].size() != 0;
}

void imprimir()
{
    cout << "orden topologico" << endl;
    for(int i = 0; i < orden_topologico.size(); i++)
    {
        cout << orden_topologico[i] << " ";
    }
    cout << endl;

    cout << "dp" << endl;
    for(int i = 1; i <= vertices; i++)
    {
        cout << dp[i] << " ";
    }
    cout << endl;
}

int metodo()
{
    for(int k = 0; k < orden_topologico.size(); k++)
    {
        int v = orden_topologico[k];
        for(int i = 0; i < grafo[v].size(); i++)
        {
            int temp = grafo[v][i];
            dp[temp] = max(dp[v] + 1, dp[temp]);
        }
    }
}

int get_max()
{
    int maximo = INT_MIN;
    for(int i = 0; i <= vertices; i++)
    {
        maximo = max(maximo, dp[i]);
    }
    return maximo;
}

int main()
{
    ios_base :: sync_with_stdio(0);
    cin.tie(0);

    cin >> vertices >> aristas;

    for(int i = 0; i < aristas; i++)
    {
        cin >> a >> b;

        grafo[a].push_back(b);
        indegree[b] += 1;
    }

    dame_orden_topologico();
    metodo();
    //imprimir();

    int resp = get_max();

    cout << resp << endl;
}
#include<bits/stdc++.h>
#define endl '\n'

using namespace std;

int vertices, aristas, a, b;
vector<int> grafo[100005];
int indegree[100005];
vector<int> orden_topologico;
int dp[100005];

bool dame_orden_topologico()
{
    queue<int> cola;
    for(int i = 1; i <= vertices; i++)
    {
        if(indegree[i] == 0)
            cola.push(i);
    }

    while(!cola.empty())
    {
        int elemento = cola.front();
        cola.pop();

        orden_topologico.push_back(elemento);

        for(int i = 0; i < grafo[elemento].size(); i++)
        {
            int temp = grafo[elemento][i];
            indegree[temp] -= 1;
            if(indegree[temp] == 0)
                cola.push(temp);
        }
    }

    // siempre es un DAG
    return true;
}

bool tiene_aristas(int v)
{
    return grafo[v].size() != 0;
}

void imprimir()
{
    cout << "orden topologico" << endl;
    for(int i = 0; i < orden_topologico.size(); i++)
    {
        cout << orden_topologico[i] << " ";
    }
    cout << endl;

    cout << "dp" << endl;
    for(int i = 1; i <= vertices; i++)
    {
        cout << dp[i] << " ";
    }
    cout << endl;
}

int metodo()
{
    for(int k = 0; k < orden_topologico.size(); k++)
    {
        int v = orden_topologico[k];
        for(int i = 0; i < grafo[v].size(); i++)
        {
            int temp = grafo[v][i];
            dp[temp] = max(dp[v] + 1, dp[temp]);
        }
    }
}

int get_max()
{
    int maximo = INT_MIN;
    for(int i = 0; i <= vertices; i++)
    {
        maximo = max(maximo, dp[i]);
    }
    return maximo;
}

int main()
{
    ios_base :: sync_with_stdio(0);
    cin.tie(0);

    cin >> vertices >> aristas;

    for(int i = 0; i < aristas; i++)
    {
        cin >> a >> b;

        grafo[a].push_back(b);
        indegree[b] += 1;
    }

    dame_orden_topologico();
    metodo();
    //imprimir();

    int resp = get_max();

    cout << resp << endl;
}



Compiler output:
a.cc: In function 'int metodo()':
a.cc:74:1: warning: no return statement in function returning non-void [-Wreturn-type]
   74 | }
      | ^
a.cc: At global scope:
a.cc:114:5: error: redefinition of 'int vertices'
  114 | int vertices, aristas, a, b;
      |     ^~~~~~~~
a.cc:6:5: note: 'int vertices' previously declared here
    6 | int vertices, aristas, a, b;
      |     ^~~~~~~~
a.cc:114:15: error: redefinition of 'int aristas'
  114 | int vertices, aristas, a, b;
      |               ^~~~~~~
a.cc:6:15: note: 'int aristas' previously declared here
    6 | int vertices, aristas, a, b;
      |               ^~~~~~~
a.cc:114:24: error: redefinition of 'int a'
  114 | int vertices, aristas, a, b;
      |                        ^
a.cc:6:24: note: 'int a' previously declared here
    6 | int vertices, aristas, a, b;
      |                        ^
a.cc:114:27: error: redefinition of 'int b'
  114 | int vertices, aristas, a, b;
      |                           ^
a.cc:6:27: note: 'int b' previously declared here
    6 | int vertices, aristas, a, b;
      |                           ^
a.cc:115:13: error: redefinition of 'std::vector<int> grafo [100005]'
  115 | vector<int> grafo[100005];
      |             ^~~~~
a.cc:7:13: note: 'std::vector<int> grafo [100005]' previously declared here
    7 | vector<int> grafo[100005];
      |             ^~~~~
a.cc:116:5: error: redefinition of 'int indegree [100005]'
  116 | int indegree[100005];
      |     ^~~~~~~~
a.cc:8:5: note: 'int indegree [100005]' previously declared here
    8 | int indegree[100005];
      |     ^~~~~~~~
a.cc:117:13: error: redefinition of 'std::vector<int> orden_topologico'
  117 | vector<int> orden_topologico;
      |             ^~~~~~~~~~~~~~~~
a.cc:9:13: note: 'std::vector<int> orden_topologico' previously declared here
    9 | vector<int> orden_topologico;
      |             ^~~~~~~~~~~~~~~~
a.cc:118:5: error: redefinition of 'int dp [100005]'
  118 | int dp[100005];
      |     ^~
a.cc:10:5: note: 'int dp [100005]' previously declared here
   10 | int dp[100005];
      |     ^~
a.cc:120:6: error: redefinition of 'bool dame_orden_topologico()'
  120 | bool dame_orden_topologico()
      |      ^~~~~~~~~~~~~~~~~~~~~
a.cc:12:6: note: 'bool dame_orden_topologico()' previously defined here
   12 | bool dame_orden_topologico()
      |      ^~~~~~~~~~~~~~~~~~~~~
a.cc:149:6: error: redefinition of 'bool tiene_aristas(int)'
  149 | bool tiene_aristas(int v)
      |      ^~~~~~~~~~~~~
a.cc:41:6: note: 'bool tiene_aristas(int)' previously defined here
   41 | bool tiene_aristas(int v)
      |      ^~~~~~~~~~~~~
a.cc:154:6: error: redefinition of 'void imprimir()'
  154 | void imprimir()
      |      ^~~~~~~~
a.cc:46:6: note: 'void imprimir()' previously defined here
   46 | void imprimir()
      |      ^~~~~~~~
a.cc:171:5: error: redefinition of 'int metodo()'
  171 | int metodo()
      |     ^~~~~~
a.cc:63:5: note: 'int metodo()' previously defined here
   63 | int metodo()
      |     ^~~~~~
a.cc: In function 'int metodo()':
a.cc:182:1: warning: no return statement in function returning non-void [-Wreturn-type]
  182 | }
      | ^
a.cc: At global scope:
a.cc:184:5: error: redefinition of 'int get_max()'
  184 | int get_max()
      |     ^~~~~~~
a.cc:76:5: note: 'int get_max()' previously defined here
   76 | int get_max()
      |     ^~~~~~~
a.cc:194:5: error: redefinition of 'int main()'
  194 | int main()
      |     ^~~~
a.cc:86:5: note: 'int main()' previously defined here
   86 | int main()
      |     ^~~~