xt0r3's blog

By xt0r3, history, 4 weeks ago, In English,

This is my submission which gets a runtime error:

64787702

The code is the following:

#include <bits/stdc++.h>
 
using namespace std;
using ll=long long;
 
using graph= map<int, vector<int> >;
/*
struct edge {
int nodes[2];
float cost;//bármi més
};//aztán vector<edge> lesz int helyett*/
 
#define vll vector<ll>
#define pairll pair<ll, ll>
 
int main(){
ll n, m, a, b, id, k=0;
cin>>n;
vector<set<ll>> edges;
bool visited[n];
for(ll i=0; i<n; i++){
    edges.push_back(set<ll>());
    visited[i]=0;
    }
 
cin>>m;
for(ll i=0; i<m; i++){
    cin>>a>>b;
    edges[a-1].insert(b-1);
    edges[b-1].insert(a-1);
    }
 
vector<vll> components;
components.push_back(vll());
components[0].push_back(0);
queue<ll> unvisited, bfs;
for(ll i=0; i<n; i++){
unvisited.push(i);
}
id=0;
visited[0]=1;
bfs.push(id);
while(!unvisited.empty()){
    for(ll i=0; i<unvisited.size(); i++){
    if(!visited[unvisited.front()]&& edges[id].find(unvisited.front())==edges[id].end()){
    components[k].push_back(unvisited.front());
    visited[unvisited.front()]=1;
    bfs.push(unvisited.front());
    unvisited.pop();
    i--;
    }
    else{
        unvisited.push(unvisited.front());
        unvisited.pop();
        }
    }
    bfs.pop();
 
 
    if(bfs.empty()){
        while(visited[unvisited.front()]&&!unvisited.empty()){
        unvisited.pop();
        }
        if(unvisited.size()>0){
            components.push_back(vll());
            k++;
            bfs.push(unvisited.front());
            components[k].push_back(unvisited.front());
            unvisited.pop();
            }
    }
    if(!bfs.empty()){
        id=bfs.front();
    }
}
 
if(k==2){
   ll sum=components[0].size()*components[1].size()+components[1].size()*components[2].size()+components[2].size()*components[0].size();
    if(sum==m){
        ll good[n];
        for(ll j=0; j<3; j++){
            for(ll i=0; i<components[j].size(); i++){
                good[components[j][i]]=j;
                }
            }
        for(ll i=0; i<n; i++){
        cout<<good[i]+1<<" ";
        }
        cout<<endl;
        }
    else{
    cout<<-1<<endl;
    }
 
}
else{
    cout<<-1<<endl;
    }
 
 
 
 
}

when running it on my Linux machine, it throws no exceptions but gives the required answer. Can anyone explain to me why the problem exists and what to do against it?

 
 
 
 
  • Vote: I like it
  • -1
  • Vote: I do not like it