Submission #435046

#TimeUsernameProblemLanguageResultExecution timeMemory
435046OzyKeys (IOI21_keys)C++17
37 / 100
651 ms17132 KiB
#include <vector>
#include <bits/stdc++.h>

using namespace std;

#define lli long long
#define rep(i, a, b) for(lli i = (a); i <= (b); ++i)
#define repa(i, a, b) for(lli i = (a); i >= (b); --i)

#define nl "\n"
#define debugsl(x) cout << #x << " = " << x << ", "
#define debug(x) debugsl(x) << nl
#define debugarr(x, a, b) cout << #x << " = ["; rep(ii, a, b) cout << x[ii] << ", "; cout << "]" << nl

#define MAX_N 2002

#define destino first
#define tipo second

int minimo = MAX_N + 1;
int tamano[MAX_N]; // TAMAÑO DEL CONJUNTO ALCANZABLE DESDE EL NODO i


std::vector<int> find_reachable(std::vector<int> r, std::vector<int> u, std::vector<int> v, std::vector<int> c) {
	std::vector<int> ans(r.size(), 1);

	// CONSTRUYE EL GRAFO PARA PODER HACER LA BUSQUEDA DE FORMA SENCILLA SIN TENER QUE RECORRER TODAS LAS ARISTAS
	vector<pair<int, int> > hijos[MAX_N];
	rep(i, 0, u.size() - 1){
	    hijos[u[i]].emplace_back(v[i], c[i]);
	    hijos[v[i]].emplace_back(u[i], c[i]);
	}

	// INTENTA RESOLVER PARA CADA NODO INICIAL
	rep(n, 0, r.size() - 1){
	    int alcanzados = 0;
	    vector<int> visitado(r.size(), 0); // PERMITE SABER QUE NODOS YA FUERON VISITADOS
	    vector<int> llave(r.size(), 0); // PERMITE SABER QUE LLAVES YA SE TIENEN
	    vector<queue<int> > aristas (r.size()); // PERMITE GUARDAR LAS ARISTAS QUE SE ACTIVARAN SI SE ENCUENTRA UN TIPO DE LLAVE
	    queue<int> q;

	    visitado[n] = 1; // VISITA EL NODO ACTUAL
	    alcanzados = 1;
	    q.push(n);
	    while (!q.empty()){
            int nodo = q.front();
            q.pop();
            for (auto h : hijos[nodo]){ // REVISA TODOS LOS NODOS A LOS QUE ESTA CONECTADO
                if (llave[h.tipo]){ // SI YA TIENE EL TIPO DE LLAVE NECESARIO
                    if (!visitado[h.destino]){ // Y EL NODO NO HA SIDO VISITADO, ENTONCES VISITALO Y AGREGALO A LA COLA
                        visitado[h.destino] = 1;
                        ++alcanzados;
                        q.push(h.destino);
                    }
                    // SI YA FUE VISITADO NO ES NECESARIO HACER NADA
                }
                else { // SI NO TIENE ESE TIPO DE LLAVE TODAVIA
                    if (!visitado[h.destino]){ // Y EL NODO AUN NO HA SIDO VISITADO
                        aristas[h.tipo].push(h.destino); // GUARDA EL NODO COMO ALCANZABLE POTENCIAL POR SI SE LLEGA A ENCONTRAR UNA LLAVE DE ESE TIPO.
                    }
                }
            }

            // RECOGE LA LLAVE DEL CUARTO Y MARCALA COMO ACTIVA
            if (!llave[r[nodo]]){ // SI NO HABIA ENCONTRADO ESE TIPO DE LLAVE
                llave[r[nodo]] = 1;
                while (!aristas[r[nodo]].empty()){ // SACA TODOS LOS NODOS A LOS QUE SE PODIA LLEGAR SI SE ENCONTRABA ESE TIPO DE LLAVE
                    int nod = aristas[r[nodo]].front();
                    aristas[r[nodo]].pop();
                    if (!visitado[nod]){
                        visitado[nod] = 1;
                        ++alcanzados;
                        q.push(nod);
                    }
                }
            }
	    }

	    minimo = min(minimo, alcanzados);
	    tamano[n] = alcanzados;
	}

	rep(i, 0, r.size() - 1) ans[i] = (tamano[i] == minimo) ? 1 : 0;

	return ans;
}

Compilation message (stderr)

keys.cpp: In function 'std::vector<int> find_reachable(std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>)':
keys.cpp:7:41: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    7 | #define rep(i, a, b) for(lli i = (a); i <= (b); ++i)
      |                                         ^
keys.cpp:29:2: note: in expansion of macro 'rep'
   29 |  rep(i, 0, u.size() - 1){
      |  ^~~
keys.cpp:7:41: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    7 | #define rep(i, a, b) for(lli i = (a); i <= (b); ++i)
      |                                         ^
keys.cpp:35:2: note: in expansion of macro 'rep'
   35 |  rep(n, 0, r.size() - 1){
      |  ^~~
keys.cpp:7:41: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    7 | #define rep(i, a, b) for(lli i = (a); i <= (b); ++i)
      |                                         ^
keys.cpp:83:2: note: in expansion of macro 'rep'
   83 |  rep(i, 0, r.size() - 1) ans[i] = (tamano[i] == minimo) ? 1 : 0;
      |  ^~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...