Submission #1229552

#TimeUsernameProblemLanguageResultExecution timeMemory
1229552LaMatematica14From Hacks to Snitches (BOI21_watchmen)C++20
0 / 100
436 ms40240 KiB
#include <bits/stdc++.h>
using namespace std;

int main() {
    int N, M; cin >> N >> M;
    vector<vector<int>> adj(N+1);
    for (int i = 0; i < M; i++) {
        int a, b; cin >> a >> b;
        adj[a].push_back(b);
        adj[b].push_back(a);
    }
    int K; cin >> K;
    vector<int> mod(K, -1), rem(N+1, -1), wat(N+1, -1);
    for (int i = 0; i < K; i++) {
        int l; cin >> l; mod[i] = l;
        for (int j = 0; j < l; j++) {
            int a; cin >> a;
            wat[a] = i;
            rem[a] = j;
        }
    }
    vector<set<int>> vis(N+1);
    vector<int> been(N+1, 0);
    vector<int> best(N+1, -1000000000);
    priority_queue<pair<int, int>> pq;
    vis[1].insert(0);
    pq.push({0, 1});
    while (!pq.empty()) {
        int a = pq.top().second; 
        best[a] = max(best[a], pq.top().first);
        pq.pop();
        if (been[a] > 2) continue;
        while (vis[a].size() > 2) vis[a].erase(prev(vis[a].end()));
        for (int pos : vis[a]) {
            been[a]++;
            for (auto x : adj[a]) {
                if (wat[x] >= 0 && ((pos+1)%mod[wat[x]] == rem[x])) continue; 
                if (wat[a] >= 0 && wat[a] == wat[x] && (pos+1)%mod[wat[a]] == rem[a]) continue;
                vis[x].insert(pos+1);
                pq.push({-pos-1, x});
            }
            pos++;
            if (wat[a] >= 0 && pos%mod[wat[a]] == rem[a]) continue;
            for (auto x : adj[a]) {
                if (wat[x] >= 0 && ((pos+1)%mod[wat[x]] == rem[x])) continue; 
                if (wat[a] >= 0 && wat[a] == wat[x] && (pos+1)%mod[wat[a]] == rem[a]) continue;
                vis[x].insert(pos+1);
                pq.push({-pos-1, x});
            }
            pos++;
            if (wat[a] >= 0 && pos%mod[wat[a]] == rem[a]) continue;
            for (auto x : adj[a]) {
                if (wat[x] >= 0 && ((pos+1)%mod[wat[x]] == rem[x])) continue; 
                if (wat[a] >= 0 && wat[a] == wat[x] && (pos+1)%mod[wat[a]] == rem[a]) continue;
                vis[x].insert(pos+1);
                pq.push({-pos-1, x});
            }
        }
    }
    if (best[N] == -1000000000) cout << "impossible\n";
    else cout << -best[N] << "\n";
}
#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...