█████████ ████ ███░░░░░███ ░░███ ███ ░░░ ██████ ███████ ██████ ██████ ░███ ███░░███ ███░░███ ███░░███ ███░░███ ░███ ░███ ░███░███ ░███ ░███████ ░███ ░███ ░░███ ███░███ ░███░███ ░███ ░███░░░ ░███ ░███ ░░█████████ ░░██████ ░░████████░░██████ ░░██████ ░░░░░░░░░ ░░░░░░ ░░░░░░░░ ░░░░░░ ░░░░░░

Envío 6016

Problema 0x53 - Encontrar ciclos en un grafo dirigido

  • Autor: jarangolp
  • Fecha: 2022-05-02 15:38:31 UTC (Hace casi 2 años)
Caso # Resultado Tiempo Memoria
#1
Correcto
0.022 s 6 KBi
#2
Correcto
0.056 s 6 KBi
#3
Correcto
0.025 s 6 KBi
#4
Incorrecto
0.077 s 7 KBi
#5
Correcto
0.026 s 6 KBi
#6
Incorrecto
0.023 s 6 KBi
#7
Incorrecto
0.018 s 7 KBi
#8
Incorrecto
0.027 s 6 KBi
#9
Incorrecto
0.049 s 6 KBi
#10
Incorrecto
0.025 s 6 KBi
#11
Correcto
0.023 s 6 KBi
#12
Correcto
0.062 s 19 KBi
#13
Incorrecto
0.021 s 6 KBi
#14
Correcto
0.02 s 6 KBi
#15
Incorrecto
0.022 s 6 KBi
#16
Correcto
0.025 s 6 KBi
#17
Incorrecto
0.022 s 6 KBi
#18
Correcto
0.022 s 6 KBi
#19
Correcto
0.048 s 6 KBi
#20
Correcto
0.018 s 6 KBi
#21
Correcto
0.02 s 6 KBi
#22
Correcto
0.029 s 6 KBi
#23
Correcto
0.024 s 6 KBi
#24
Correcto
0.081 s 6 KBi
#25
Correcto
0.028 s 6 KBi
#26
Correcto
0.026 s 8 KBi
#27
Error en tiempo de ejecución (NZEC)
Exited with error status 1
/box/script.js:41
    const [u,v] = dataArray[i].split(' ').map(n => Number(n));
                               ^

TypeError: Cannot read property 'split' of undefined
    at ReadStream.<anonymous> (/box/script.js:41:32)
    at ReadStream.emit (events.js:210:5)
    at addChunk (_stream_readable.js:309:12)
    at readableAddChunk (_stream_readable.js:290:11)
    at ReadStream.Readable.push (_stream_readable.js:224:10)
    at internal/fs/streams.js:191:12
    at FSReqCallback.wrapper [as oncomplete] (fs.js:470:5)
0.026 s 10 KBi
#28
Correcto
0.06 s 11 KBi
#29
Error en tiempo de ejecución (NZEC)
Exited with error status 1
/box/script.js:41
    const [u,v] = dataArray[i].split(' ').map(n => Number(n));
                               ^

TypeError: Cannot read property 'split' of undefined
    at ReadStream.<anonymous> (/box/script.js:41:32)
    at ReadStream.emit (events.js:210:5)
    at addChunk (_stream_readable.js:309:12)
    at readableAddChunk (_stream_readable.js:290:11)
    at ReadStream.Readable.push (_stream_readable.js:224:10)
    at internal/fs/streams.js:191:12
    at FSReqCallback.wrapper [as oncomplete] (fs.js:470:5)
0.073 s 13 KBi
#30
Correcto
0.03 s 8 KBi
#31
Correcto
0.086 s 14 KBi
#32
Error en tiempo de ejecución (NZEC)
Exited with error status 1
/box/script.js:41
    const [u,v] = dataArray[i].split(' ').map(n => Number(n));
                               ^

TypeError: Cannot read property 'split' of undefined
    at ReadStream.<anonymous> (/box/script.js:41:32)
    at ReadStream.emit (events.js:210:5)
    at addChunk (_stream_readable.js:309:12)
    at readableAddChunk (_stream_readable.js:290:11)
    at ReadStream.Readable.push (_stream_readable.js:224:10)
    at internal/fs/streams.js:191:12
    at FSReqCallback.wrapper [as oncomplete] (fs.js:470:5)
0.026 s 10 KBi
#33
Error en tiempo de ejecución (NZEC)
Exited with error status 1
/box/script.js:41
    const [u,v] = dataArray[i].split(' ').map(n => Number(n));
                               ^

TypeError: Cannot read property 'split' of undefined
    at ReadStream.<anonymous> (/box/script.js:41:32)
    at ReadStream.emit (events.js:210:5)
    at addChunk (_stream_readable.js:309:12)
    at readableAddChunk (_stream_readable.js:290:11)
    at ReadStream.Readable.push (_stream_readable.js:224:10)
    at internal/fs/streams.js:191:12
    at FSReqCallback.wrapper [as oncomplete] (fs.js:470:5)
0.039 s 11 KBi
#34
Error en tiempo de ejecución (NZEC)
Exited with error status 1
/box/script.js:41
    const [u,v] = dataArray[i].split(' ').map(n => Number(n));
                               ^

TypeError: Cannot read property 'split' of undefined
    at ReadStream.<anonymous> (/box/script.js:41:32)
    at ReadStream.emit (events.js:210:5)
    at addChunk (_stream_readable.js:309:12)
    at readableAddChunk (_stream_readable.js:290:11)
    at ReadStream.Readable.push (_stream_readable.js:224:10)
    at internal/fs/streams.js:191:12
    at FSReqCallback.wrapper [as oncomplete] (fs.js:470:5)
0.033 s 11 KBi
#35
Error en tiempo de ejecución (NZEC)
Exited with error status 1
/box/script.js:41
    const [u,v] = dataArray[i].split(' ').map(n => Number(n));
                               ^

TypeError: Cannot read property 'split' of undefined
    at ReadStream.<anonymous> (/box/script.js:41:32)
    at ReadStream.emit (events.js:210:5)
    at addChunk (_stream_readable.js:309:12)
    at readableAddChunk (_stream_readable.js:290:11)
    at ReadStream.Readable.push (_stream_readable.js:224:10)
    at internal/fs/streams.js:191:12
    at FSReqCallback.wrapper [as oncomplete] (fs.js:470:5)
0.025 s 10 KBi
Puntos totales: 58 / 100

Código

const adj = new Map();
let visited = [], recStack = [];

function isCyclic(N) {
  visited = new Array(N).fill(false);
  recStack = new Array(N).fill(false);
  for (let i = 0; i < N; i++) {
    if(isCyclicUtil(i, visited, recStack))
      return true;
    
  }
  return false;
}


function isCyclicUtil(i,visited,recStack) {
  if (recStack[i])
    return true;

  if (visited[i])
    return false;

  visited[i] = true;
  recStack[i] = true;
  const children = adj[i] || [];

  for (let c=0; c < children.length; c++)
    if (isCyclicUtil(children, visited, recStack))
      return true;

    recStack[i] = false;

  return false;
}

process.stdin.on('data', data => {
  const dataArray = data.toString().split('\n');
  if (dataArray.length === 0) return;
  const [N, M] = dataArray[0].split(' ').map(n => Number(n));
  for (let i = 1; i < M; i++) {
    const [u,v] = dataArray[i].split(' ').map(n => Number(n));
    adj[u] = adj[u] || [];
    adj[u].push(v);
  }
 
  const resp = isCyclic(N) ? 'Yes' : 'No';
  console.log(resp)
})