제출 #435989

#제출 시각아이디문제언어결과실행 시간메모리
435989ocarimaKeys (IOI21_keys)C++17
37 / 100
2588 ms244220 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 300002

#define destino first
#define tipo second

vector<pair<int, int> > hijos[MAX_N];
vector<int> nodosciclo[MAX_N];
int visitado[MAX_N], ciclo[MAX_N], minimo, origen, alcance;
vector<pair<int, int> > largociclo;
stack<int> s;
bool ciclodoble, existehoja;

void dfs(int nodo){
    int h;
    visitado[nodo] = 1;
    s.push(nodo);
    for (auto hh : hijos[nodo]){
        h = hh.destino;
        if (visitado[h] == 0) dfs(h);
        else if (visitado[h] == 1){ // SE ENCONTRO UN CICLO
            while (s.top() != h){
                ciclo[s.top()] = h;
                nodosciclo[h].push_back(s.top());
                visitado[s.top()] = 3;
                s.pop();
            }
            nodosciclo[h].push_back(h);
            ciclo[h] = h;
            visitado[h] = 3;
            s.pop();

            largociclo.emplace_back(nodosciclo[h].size(), h);
        }
        else if (visitado[h] == 3){ // SE UNIO A UN CICLO QUE YA EXISTE
            while (!s.empty() && visitado[s.top()] == 3){
                ciclo[s.top()] = ciclo[h];
                nodosciclo[h].push_back(s.top());
                visitado[s.top()] = 3;
                s.pop();
            }
        }
    }
    visitado[nodo] = 2;
    if (!s.empty() && s.top() == nodo) s.pop();
}

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 UN GRAFO DIRIGIDO UNICAMENTE CON LAS ARISTAS QUE SE PUEDEN USAR CON LA LLAVE DE CADA CUARTO
    rep(i, 0, u.size() - 1){
        if (c[i] == r[u[i]]) hijos[u[i]].emplace_back(v[i], c[i]); // SI LA LLAVE EN EL CUARTO u PUEDE ABRIR ESA PUERTA, AGREGALA AL GRAFO DE u -> v
        if (c[i] == r[v[i]]) hijos[v[i]].emplace_back(u[i], c[i]); // SI LA LLAVE EN EL CUARTO v PUEDE ABRIR ESA PUERTA, AGREGALA AL GRAFO DE v -> u
    }

    // VERIFICA SI HAY ALGUN CUARTO CON ALCANCE 0
    existehoja = false;
    rep(cuarto, 0, r.size() - 1) if (hijos[cuarto].size() == 0){
        existehoja = true;
        break;
    }
    if (existehoja){
        rep(cuarto, 0, r.size() - 1) if (hijos[cuarto].size() > 0) ans[cuarto] = 0;
        return ans;
    }

    // DENTRO DEL GRAFO DIRIGIDO ANTERIOR UNA HOJA REPRESENTA UN CUARTO QUE NO PUEDE LLEGAR A NINGUN OTRO Y POR LO TANTO SERIA MINIMO
    // SI NO HAY NINGUNA HOJA, ENTONCES BUSCAMOS CICLOS QUE NO BIFURQUEN, ES DECIR, QUE PUEDEN ENTRAR ARISTAS, PERO NO DEBE SALIR NINGUNA.
    // EL CUARTO CON ALCANCE MINIMO PERTENECE A ALGUNO DE ESTOS CICLOS.
    rep(cuarto, 0, r.size() - 1) ciclo[cuarto] = -1;
    rep(cuarto, 0, r.size() - 1) if (!visitado[cuarto]) dfs(cuarto);

    rep(cuarto, 0, r.size() - 1){
        if (ciclo[cuarto] == cuarto){
            debugsl(cuarto);
            debugarr(nodosciclo[cuarto], 0, nodosciclo[cuarto].size() - 1);
        }
    }

    sort(largociclo.begin(), largociclo.end());
    minimo = r.size();

    // AGREGA TODAS LAS ARISTAS PARA PODER VER EL ALCANCE
    rep(cuarto, 0, r.size() - 1) hijos[cuarto].clear();
    rep(i, 0, c.size() - 1){
        hijos[u[i]].emplace_back(v[i], c[i]);
        hijos[v[i]].emplace_back(u[i], c[i]);
    }

    // PARA CADA CICLO REVISA EL ALCANCE QUE TIENE
    for (auto c : largociclo){
        if (c.first > minimo) continue;
        origen = c.second;
        alcance = 0;

        debugsl(minimo);
        debug(origen);

        vector<int> vis(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;

	    vis[origen] = 1; // VISITA EL NODO ACTUAL
	    alcance = 1;
	    q.push(origen);
	    while (!q.empty()){
            int nodo = q.front();
            q.pop();
            debugsl(origen);
            debug(nodo);
            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 (!vis[h.destino]){ // Y EL NODO NO HA SIDO VISITADO, ENTONCES VISITALO Y AGREGALO A LA COLA
                        vis[h.destino] = 1;
                        ++alcance;
                        q.push(h.destino);
                    }
                    // SI YA FUE VISITADO NO ES NECESARIO HACER NADA
                }
                else { // SI NO TIENE ESE TIPO DE LLAVE TODAVIA
                    if (!vis[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
                debugsl("agrega llave");
                debugsl(nodo);
                debug(r[nodo]);
                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();
                    debug(nod);
                    aristas[r[nodo]].pop();
                    if (!vis[nod]){

                        vis[nod] = 1;
                        ++alcance;
                        q.push(nod);
                    }
                }
            }
	    }

        debug(alcance);

        while(!q.empty()) q.pop();
        if (alcance < minimo){
            minimo = alcance;
            rep(i, 0, r.size() - 1){
                if (vis[i] == 1) ans[i] = 1;
                else ans[i] = 0;
            }
        }
        else if (alcance == minimo){
            rep(i, 0, r.size() - 1) if (vis[i] == 1) ans[i] = 1;
        }
        debugarr(ans, 0, r.size() - 1);
    }

	return ans;
}

컴파일 시 표준 에러 (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:65:5: note: in expansion of macro 'rep'
   65 |     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:72:5: note: in expansion of macro 'rep'
   72 |     rep(cuarto, 0, r.size() - 1) if (hijos[cuarto].size() == 0){
      |     ^~~
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:77:9: note: in expansion of macro 'rep'
   77 |         rep(cuarto, 0, r.size() - 1) if (hijos[cuarto].size() > 0) ans[cuarto] = 0;
      |         ^~~
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:84:5: note: in expansion of macro 'rep'
   84 |     rep(cuarto, 0, r.size() - 1) ciclo[cuarto] = -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:85:5: note: in expansion of macro 'rep'
   85 |     rep(cuarto, 0, r.size() - 1) if (!visitado[cuarto]) dfs(cuarto);
      |     ^~~
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:87:5: note: in expansion of macro 'rep'
   87 |     rep(cuarto, 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:98:5: note: in expansion of macro 'rep'
   98 |     rep(cuarto, 0, r.size() - 1) hijos[cuarto].clear();
      |     ^~~
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:99:5: note: in expansion of macro 'rep'
   99 |     rep(i, 0, c.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:167:13: note: in expansion of macro 'rep'
  167 |             rep(i, 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:173:13: note: in expansion of macro 'rep'
  173 |             rep(i, 0, r.size() - 1) if (vis[i] == 1) ans[i] = 1;
      |             ^~~
#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...