TOPIC

WA(5%) what?

ezequiel asked 2 years ago

wa 5% i dont know what error

#include <bits/stdc++.h>
#define MAX_V 30001

using namespace std;

vector<int> G[MAX_V];
vector<bool> visited(MAX_V,false);
int cycle = 0;

void dfs(int v) {

    visited[v] = true;

        for (auto i: G[v]) {
            if (!visited[i]) {
                visited[i] = true;

            }else{
                cycle = 1;
            }
        }
    }

int main()
{

    unsigned long long T, N, M, A, B;

    cin >> T;

    for (int i = 0; i < T; i++)
    {
        cycle = 0;

        cin >> N >> M;

        for (int j = 0; j < M; j++)
        {
            cin >> A >> B;

            G[A].push_back(B);
        }

        for (int k = 1; k <= N; k++)
        {
                if(!visited[k])
                dfs(k);
                 if (cycle)
                    break;
        }

        cycle ? cout << "SIM\n" : cout << "NAO\n";
        cycle = 0;
        visited.clear();
    }

    return 0;
}

Remember not post solutions. Your post may be reviewed by our moderators.

  • Glauco Acassio replied 2 months ago

    3
    2 1
    1 2
    2 2
    1 2
    2 1
    4 6
    2 3
    3 4
    4 2
    1 3
    5 6
    5 4
    

    Answer: NAO SIM SIM

    Your program: NAO NAO NAO

  • Fernando Morato replied 11 months ago

    You didn't clear your vector G. And there's another thing missing. This link can be useful: https://www.ime.usp.br/~pf/algoritmos_em_grafos/aulas/dag.html.

  • feodorv replied 2 years ago

    Documents number from 1 to N, not from 0 to N-1.