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

Envío 3895

Problema 0x53 - Encontrar ciclos en un grafo dirigido

  • Autor: Jhon_z09
  • Fecha: 2021-04-20 21:00:15 UTC (Hace alrededor de 3 años)
Caso # Resultado Tiempo Memoria
#1
Correcto
0.006 s 1 KBi
#2
Correcto
0.006 s 1 KBi
#3
Correcto
0.007 s 1 KBi
#4
Correcto
0.003 s 2 KBi
#5
Correcto
0.006 s 1 KBi
#6
Correcto
0.008 s 1 KBi
#7
Esperando resultado...
#8
Esperando resultado...
#9
Esperando resultado...
#10
Esperando resultado...
#11
Correcto
0.004 s 1 KBi
#12
Correcto
0.003 s 2 KBi
#13
Esperando resultado...
#14
Esperando resultado...
#15
Correcto
0.005 s 5 KBi
#16
Esperando resultado...
#17
Correcto
0.003 s 2 KBi
#18
Esperando resultado...
#19
Correcto
0.003 s 2 KBi
#20
Correcto
0.003 s 2 KBi
#21
Incorrecto
0.009 s 1 KBi
#22
Correcto
0.003 s 2 KBi
#23
Esperando resultado...
#24
Esperando resultado...
#25
Esperando resultado...
#26
Correcto
0.006 s 2 KBi
#27
Esperando resultado...
#28
Correcto
0.01 s 1 KBi
#29
Esperando resultado...
#30
Correcto
0.006 s 2 KBi
#31
Esperando resultado...
#32
Esperando resultado...
#33
Correcto
0.018 s 2 KBi
#34
Esperando resultado...
#35
Esperando resultado...
Puntos totales: 49 / 100

Código

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

int main(){
    int n, m; cin >> n >> m;
    vector<vector<int>> graph(n);
   
    for(int i = 0 ; i < m; ++i){
    	int u, v; cin >> u >> v;
    	graph[u].PB(v);
	}
   

	queue<int> q;
    vector<bool> used(n);
    vector<int> d(n);
    bool f  = true;
    for(int start = 0; start < n and f; ++start){
    	if(used[start]) continue;
    	
    	q.push(start);
    	used[start] = true;
	    d[start] = 0;
		while(!q.empty() and f){
	    	int curr = q.front();
	    	q.pop();

	    	for(int adj: graph[curr]){
	    		if(used[adj] and d[adj] < d[curr]){ 
					f = false; break;
				}
				else if(adj == curr){
					f = false; break;
				}
				else if(used[adj]) continue;
				else{
		    		used[adj] = true;
		    		q.push(adj);
		    		d[adj] = d[curr] + 1;
	    		}
			}
		}
	}
    
    if (f) cout << "No";
    else cout << "Yes";
	
	return 0;
}