CSES - Aalto Competitive Programming 2024 - wk7 - Mon - Results
Submission details
Task:Snakeless path
Sender:Nallue
Submission time:2024-10-21 17:49:43 +0300
Language:C++ (C++11)
Status:COMPILE ERROR

Compiler report

input/code.cpp: In function 'void BFS(const std::vector<std::set<long long int> >&, std::vector<long long int>&)':
input/code.cpp:20:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::set<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   20 |         while(idx!=adj.size()-1){
      |               ~~~^~~~~~~~~~~~~~
input/code.cpp:40:29: error: passing 'const value_type' {aka 'const std::set<long long int>'} as 'this' argument discards qualifiers [-fpermissive]
   40 |                 adj[x].erase(it);
      |                 ~~~~~~~~~~~~^~~~
In file included from /usr/include/c++/11/set:61,
                 from input/code.cpp:5:
/usr/include/c++/11/bits/stl_set.h:654:7: note:   in call to 'std::set<_Key, _Compare, _Alloc>::iterator std::set<_Key, _Compare, _Alloc>::erase(std::set<_Key, _Compare, _Alloc>::const_iterator) [with _Key = long long int; _Compare = std::less<long long int>; _Alloc = std::allo...

Code

#include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <set>
 
using namespace std;
 
void BFS(const vector<set<long long>>& adj, vector<long long>& distances){
    queue<long long> q;
    q.push(0);
    distances[0] = 0;
 
    while(not q.empty()){
        long long x = q.front();
        q.pop();
        //cout<<q.size()<<endl;

        long long idx=0;
        while(idx!=adj.size()-1){
            long long setint;
            if(adj[x].begin()==adj[x].end()){
                setint = adj.size()-1;
            }
            else setint = *adj[x].begin();


            while(idx!=setint){
                
                if(distances[idx]<0){
                    distances[idx]=distances[x]+1;
                    q.push(idx);
                }
                idx += 1;

            }
            if(adj[x].begin()!=adj[x].end()){
                set<long long>::const_iterator it = adj[x].begin();
                //it = adj[x].begin();
                adj[x].erase(it);
            }
            

        }
        
    }
}
 
void Gpath(const vector<set<long long>>& adj, const vector<long long>& distances, long long ini, stack<long long>& path){
 
    int x=ini;
    path.push(x);
 
    while (distances[x]!=0){
 
        for(unsigned long i=0;i<adj.size(); i++){
            //cout << "."<<endl;
            if(adj[x].find(i) != adj[x].end())continue;
 
            if(distances[i]!=-1 and distances[i]<distances[x]){
                path.push(i);
                x=i;
                break;
            }
        }
    }
}
 
 
int main(){
    int n,m;
    cin >> n >> m;
    vector<set<int>> adj(n);

 
    for(int i=0;i<n;i++)adj[i].insert(i); //[i]=false;
 
    for(int i=0;i<m;i++){
        int a,b;
        cin >> a >> b;
        adj[a-1].insert(b-1);
        adj[b-1].insert(a-1);
    }
 
    vector<int> distances(n,-1);
 
    BFS(adj, distances);
 
    //cout << "hola" << endl;
 
    if(distances[n-1]==-1){
        cout<<0<<endl;
        return 0;
    }
 
    stack<int> path;
 
    Gpath(adj,distances,n-1,path);
 
    //cout << "adeu" << endl;
 
    cout<<path.size()<<endl;
    while(not path.empty()){
        cout<<path.top()+1;
        if(path.top()!=n-1)cout<<" ";
        path.pop();
    }
    cout <<endl;
 
 
}