제출 #441651

#제출 시각아이디문제언어결과실행 시간메모리
441651OzyFrom Hacks to Snitches (BOI21_watchmen)C++17
5 / 100
2443 ms104788 KiB
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for (int i = (a); i <= (b); i++)
#define repa(i,a,b) for (int i = (a); i >= (b); i--)
#define debug(a) cout << #a << " = " << a << endl
#define debugsl(a) cout << #a << " = " << a << ", "
#define lli long long int

#define MAX 100000
#define pos first
#define tiempo second

lli n,m,a,b,k,x,res;
vector<lli> hijos[MAX+2];
lli visitados[127][MAX+2];
queue< pair<lli,lli> > cola;
pair<lli,lli> act;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    cin >> n >> m;
    rep(i,1,m) {
        cin >> a >> b;
        hijos[a].push_back(b);
        hijos[b].push_back(a);
    }

    cin >> k;
    rep(i,1,k) {
        cin >> a;
        x = a;
        rep(j,0,a-1) {
            cin >> b;
            visitados[j][b] = -1;
        }
    }

    cola.push({1,0});
    res = -1;
    while (!cola.empty()) {
        act = cola.front();
        cola.pop();

        a = act.tiempo % x;
        if (visitados[a][act.pos] != 0) continue;
        if (act.pos == n) {res = act.tiempo; break;}
        visitados[a][act.pos] = 1;

        b = (a+1)%x;
        if(visitados[b][act.pos] == 0) cola.push({act.pos, act.tiempo+1});

        for (auto h : hijos[act.pos]){
            if (visitados[b][h] != 0) continue;
            if (visitados[a][h] == -1 && visitados[b][act.pos] == -1) continue;
            cola.push({h,act.tiempo+1});
        }
    }

    if (res == -1) cout << "impossible";
    else cout << res;
}
#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...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...