Link to problem: http://www.usaco.org/index.php?page=viewproblem2&cpid=644
I need help debugging this code. Sorry, if I am not allowed to ask for debugging help. I have been trying to debug this for the past three days with no results. The program only works for the first, third and fifth testcases.
My logic is to store the graph in terms of adjacency lists. Every time a cow is removed, I remove it from all the adjacency lists. Then I loop through all lists and make sure they do not have length 0. If they do the answer is no.
This is my code:
#include <iostream>
#include <vector>
#include <array>
#include <algorithm>
using namespace std;
int main() {
//freopen("closing.in", "r", stdin);
//freopen("closing.out", "w", stdout);
int N,M;
cin>>N>>M;
vector<vector<int>> adj_list(N+1);
for(int i=1;i<=N;i++){
vector<int> Empty;
adj_list[i]=Empty;
}
int input1,input2;
for(int i=0;i<M;i++){
//Fill Adjancey Lists
cin>>input1>>input2;
adj_list[input1].push_back(input2);
adj_list[input2].push_back(input1);
}
// for(int i=0;i<N;i++){
// cout<<i+1<<" ";
// for(int x=0; x < adj_list[i].size(); x++){
// cout<<adj_list[i][x]<<" ";
// }
// cout<<endl;
// }
vector<int> Cows;
for(int i=1;i<=N;i++){
Cows.push_back(i);
}
bool flag1=false;
for(int i=0;i<Cows.size();i++){
if(adj_list[Cows[i]].size()==0){
flag1=true;
break;
}
}
if(flag1==true){
cout<<"NO"<<endl;
}else{
cout<<"YES"<<endl;
}
for(int i=0;i<N-2;i++){
int eliminate;
cin>>eliminate;
for(int x=0;x<adj_list[eliminate].size();x++){
//Remove the cow that is being eliminated from all adjaceny lists
adj_list[adj_list[eliminate][x]].erase(remove(adj_list[adj_list[eliminate][x]].begin(), adj_list[adj_list[eliminate][x]].end(), eliminate), adj_list[adj_list[eliminate][x]].end());
}
Cows.erase(remove(Cows.begin(), Cows.end(), eliminate), Cows.end());
bool flag=false;
for (int y = 0; y < Cows.size(); y++) {
if (adj_list[Cows[y]].size() == 0) {
flag = true;
}
}
if(flag==true){
cout<<"NO"<<endl;
}else{
cout<<"YES"<<endl;
}
}
//Last cows is always a yes
cout<<"YES";
return 0;
}
Can someone please help me find the mistake?
Thanks