Why is it RE? I've already tried implementing it with a stack.
Code: Select all
#include <bits/stdc++.h>
using namespace std;
int N;
vector<vector<int>> adjMat;
int toInt(string &str){
int cnt = 0;
for(auto itr = str.rbegin(); itr!=str.rend(); ++itr){
cnt *= 10;
cnt += *itr - '0';
}
// cout << "transformed " << str << " to " << cnt << endl;
return cnt;
}
vector<bool> cycle;
vector<int> trace;
bool found;
int tabs = -1;
void printTabs(){
for(int i = 0; i<tabs;i++) printf("\t");
}
void f(int start, int node, int len){
if(found) return;
tabs++;
// printTabs();printf("Got to %d %d %d\n", start, node, len);
if(node == start){
if(len == N){
found = true;
trace.push_back(node);
for(int i = 0; i<N+1; i++){
if(i) printf(" ");
printf("%d", trace[i]+1);
}
printf("\n");
trace.pop_back();
tabs--;
return;
} else if(len){
tabs--;
return;
}
}
if(cycle[node]){
tabs--;
return;
}
cycle[node] = true;
trace.push_back(node);
// printTabs();printf("reached %d\n", node);
for(int i = 0; i<N; i++){
if(i == node) continue;
if(adjMat[node][i]){
f(start, i, len+1);
}
}
trace.pop_back();
cycle[node] = false;
tabs--;
}
class package{
public:
int start, node, len;
int loopCtr;
package(){
start = node = len = -1;
loopCtr = 0;
}
package(int S, int N, int L){
start = S;
node = N;
len = L;
loopCtr = 0;
}
package(int S, int N, int L, int ctr){
start = S;
node = N;
len = L;
loopCtr = ctr;
}
bool operator== (const package& other) const{
return start == other.start && node == other.node && len == other.len;
}
bool operator!= (const package& other) const{
return start == other.start && node == other.node && len == other.len;
}
};
stack<package> st;
// void run(int start, int node, int len){
// if(found) return;
// st.push(state(start, node, len));
// state solved;
// // while(!st.empty()){
// // state s = st.top();
// // if(s.start == s.node){
// // if(s.len == N){
// // found = true;
// // // Found soln
// // trace.push_back(node);
// // for(int i = 0; i<N+1; i++){
// // if(i) printf(" ");
// // printf("%d", trace[i]+1);
// // }
// // printf("\n");
// // trace.pop_back();
// // solved = s;
// // st.pop();
// // continue;
// // } else if(s.len){
// // solved = s;
// // st.pop();
// // continue;
// // }
// // }
// // if(cycle[s.node] && !s.loopCtr){
// // solved = s;
// // st.pop();
// // continue;
// // }
// // cycle[s.node] = true;
// // trace.push_back(s.node);
// // package next(s.start, s.loopCtr, s.len+1);
// // // end of recusion, return
// // if(s.loopCtr == N){
// // solved = s;
// // trace.pop_back();
// // cycle[s.node] = false;
// // st.pop();
// // continue;
// // }
// // // Skip same node
// // if(s.loopCtr == s.node){
// // st.pop_back();
// // next.loopCtr++;
// // st.push(next);
// // continue;
// // }
// // // Skip not adj
// // if(!adjMat[s.node][s.loopCtr]){
// // st.pop_back();
// // next.loopCtr++;
// // st.push(next);
// // continue;
// // }
// // if(solved != next){
// // st.push(next);
// // continue;
// // } else {
// // st.pop_back();
// // next.loopCtr++;
// // st.push(next);
// // continue;
// // }
// }
// }
void run(int start, int node, int len){
if(found) return;
st.push(package(start, node, len));
package solved;
while(!st.empty()){
package s = st.top();
st.pop();
if(found) continue;
// printTabs();printf("%d %d %d %d\n", s.start, s.node, s.len, s.loopCtr);
if(s.node == s.start){
if(s.len == N){
found = true;
trace.push_back(s.node);
for(int i = 0; i<N+1; i++){
if(i) printf(" ");
printf("%d", trace[i]+1);
}
printf("\n");
trace.pop_back();
tabs--;
continue;
} else if(len){
tabs--;
continue;
}
}
if(s.loopCtr == 0){
tabs++;
if(cycle[s.node]){
continue;
}
cycle[s.node] = true;
trace.push_back(s.node);
}
if(s.loopCtr == N){
trace.pop_back();
cycle[s.node] = false;
tabs--;
continue;
}
package next = s;
next.loopCtr++;
st.push(next);
if(s.node == s.loopCtr){
continue;
}
if(adjMat[s.node][s.loopCtr]){
st.push(package(s.start, s.loopCtr, s.len+1));
continue;
}
}
}
int main(){
while(!cin.eof()){
cin >> N;
adjMat.assign(N+50, vector<int>(N+50, 0));
cycle.assign(N+50, false);
found = false;
string raw;
while(!cin.eof()){
cin >> raw;
if(raw == "%"){
// cout <<"breaking" << endl;
break;
}
int b;
cin >> b;
int a = toInt(raw);
// cout << "got " << raw << " " << b << endl;
a--;
b--;
adjMat[a][b] = adjMat[b][a] =1 ;
}
for(int start = 0; start<N; start++){
trace.clear();
run(start, start, 0);
}
if(!found){
cout << "N\n";
}
}
return 0;
}