CSES - NOI 2024 - Results
Submission details
Task:Anime Shops
Sender:Akseli Sahari
Submission time:2024-03-06 18:41:09 +0200
Language:C++ (C++11)
Status:COMPILE ERROR

Compiler report

input/code.cpp: In function 'int main()':
input/code.cpp:20:9: error: 'q' was not declared in this scope
   20 |         q.push(x);
      |         ^
input/code.cpp:47:9: error: return-statement with no value, in function returning 'int' [-fpermissive]
   47 |         return;
      |         ^~~~~~

Code

#include <bits/stdc++.h>

using namespace std;

int n, m, k;
int p[202020];
int z[202020];
int dist[202020];
vector<int> v[202020];

int ans[202020];

int main(){
    cin >> n >> m >> k;
    for(int i = 0; i < k; i++){
        int x;
        cin >> x;
        p[x] = 1;
        z[x] = x;
        q.push(x);
    }
    for(int i = 0; i < m; i++){
        int a, b;
        cin >> a >> b;
        v[a].push_back(b);
        v[b].push_back(a);
    }

    if(n <= 1000){
        for(int i = 1; i<= n; i++) dist[i] = 1e9;
        queue<pair<int,int>> q;
        for(auto i : p){
            q.push({i, 0});
            z[i] = i;
            while(!q.empty()){
                int x = q.front().first; int s = q.front().second;
                q.pop();
                if(x != i) dist[x] = min(dist[x], s);
                z[x] = i;
                for(auto j : v[x]){
                    if(z[j] == i) continue;
                    q.push({j, s + 1});
                }
            }
        }
        for(int i = 1; i <= n; i++) cout << (dist[i] == 1e9 ? -1 : dist[i]) << " ";
        return;
    }
    queue<int> q;   
    while(!q.empty()){
        int x = q.front(); q.pop();
        for(auto i : v[x]){
            if(z[i]){
                if(z[x] == z[i]) continue;
                if(p[x] && p[i]) {
                    ans[x] = 1;
                    ans[i] = 1;
                    continue;
                } 
                if(p[i]){
                    if(!ans[i]) ans[i] = ans[x] + 1;
                    if(!ans[z[x]]) ans[z[x]] = ans[x] + 1;
                    continue;
                }
                if(p[x]){
                    if(!ans[x]) ans[x] = ans[i] + 1;
                    if(!ans[z[i]]) ans[z[i]] = ans[i] + 1;
                    continue;
                }
                if(!ans[z[x]]) ans[z[x]] = ans[x] + ans[i] + 1;
                if(!ans[z[i]]) ans[z[i]] = ans[x] + ans[i] + 1;
                continue;
            }
            z[i] = z[x];
            if(p[x]) ans[i] = 1;
            else ans[i] = ans[x] + 1;
            q.push(i);
        }
    }
    for(int i = 1; i <= n; i++) cout << (ans[i] ? ans[i] : -1) << " ";
    cout << endl;
}