WHY DO I GET TL EXCEEDED? PLZ HELP :(((
#include <iostream>
using namespace std;
struct stack {
int data;
stack* next;
};
stack* m[1001];
int n,a;
stack *st,*ce;
void push(stack* &s,int what) {
stack* tmp=new stack;
if (tmp==NULL) { cout<<"Allocation Error"<<endl; return; }
tmp->next=s;
tmp->data=what;
s=tmp;
}
int pop(stack* &s) {
stack* tmp;
int q;
if (s==NULL) { cout<<"Stack already empty"<<endl; return -1; }
tmp=s;
q=s->data;
s=s->next;
delete tmp;
return q;
}
int top(stack* s) {
if (s==NULL) { cout<<"Stack empty"<<endl; return -1; } else return s-
>data;
}
void init() {
int p;
cin>>n>>a;
for (int i=1;i<=n;i++)
for (int j=1;j<=n;j++) {
cin>>p;
p=!p;
if(i==j) p=0;
if (p) push(m[i],j);
}
}
int empty(stack* s) {
if (s==NULL) return 1; else return 0;
}
void solve() {
push(st,a);
while (st!=NULL) {
int v=top(st);
if (m[v]!=NULL) push(st,pop(m[v]));
else push(ce,pop(st));
}
}
void print() {
int prev=pop(ce);
while(ce!=NULL) {
cout<<prev<<" "<<top(ce)<<endl;
prev=pop(ce);
}
}
int main() {
st=NULL;
ce=NULL;
init();
solve();
print();
return 0;
}
...because your algorithm is very bad. Use Hungry algorithm and you'll get AC :-)))
> #include <iostream>
> using namespace std;
>
> struct stack {
> int data;
> stack* next;
> };
>
> stack* m[1001];
> int n,a;
> stack *st,*ce;
>
> void push(stack* &s,int what) {
> stack* tmp=new stack;
> if (tmp==NULL) { cout<<"Allocation Error"<<endl; return; }
> tmp->next=s;
> tmp->data=what;
> s=tmp;
> }
>
> int pop(stack* &s) {
> stack* tmp;
> int q;
> if (s==NULL) { cout<<"Stack already empty"<<endl; return -1; }
> tmp=s;
> q=s->data;
> s=s->next;
> delete tmp;
> return q;
> }
>
> int top(stack* s) {
> if (s==NULL) { cout<<"Stack empty"<<endl; return -1; } else return
s-
> >data;
> }
>
>
> void init() {
> int p;
> cin>>n>>a;
> for (int i=1;i<=n;i++)
> for (int j=1;j<=n;j++) {
> cin>>p;
> p=!p;
> if(i==j) p=0;
> if (p) push(m[i],j);
> }
> }
>
> int empty(stack* s) {
> if (s==NULL) return 1; else return 0;
> }
>
> void solve() {
> push(st,a);
> while (st!=NULL) {
> int v=top(st);
> if (m[v]!=NULL) push(st,pop(m[v]));
> else push(ce,pop(st));
> }
> }
>
> void print() {
> int prev=pop(ce);
> while(ce!=NULL) {
> cout<<prev<<" "<<top(ce)<<endl;
> prev=pop(ce);
> }
> }
>
> int main() {
> st=NULL;
> ce=NULL;
> init();
> solve();
> print();
> return 0;
> }
Re: "Hungry" == "Greedy"? :-)) (-)
> > #include <iostream>
> > using namespace std;
> >
> > struct stack {
> > int data;
> > stack* next;
> > };
> >
> > stack* m[1001];
> > int n,a;
> > stack *st,*ce;
> >
> > void push(stack* &s,int what) {
> > stack* tmp=new stack;
> > if (tmp==NULL) { cout<<"Allocation Error"<<endl; return; }
> > tmp->next=s;
> > tmp->data=what;
> > s=tmp;
> > }
> >
> > int pop(stack* &s) {
> > stack* tmp;
> > int q;
> > if (s==NULL) { cout<<"Stack already empty"<<endl; return -1; }
> > tmp=s;
> > q=s->data;
> > s=s->next;
> > delete tmp;
> > return q;
> > }
> >
> > int top(stack* s) {
> > if (s==NULL) { cout<<"Stack empty"<<endl; return -1; } else
return
> s-
> > >data;
> > }
> >
> >
> > void init() {
> > int p;
> > cin>>n>>a;
> > for (int i=1;i<=n;i++)
> > for (int j=1;j<=n;j++) {
> > cin>>p;
> > p=!p;
> > if(i==j) p=0;
> > if (p) push(m[i],j);
> > }
> > }
> >
> > int empty(stack* s) {
> > if (s==NULL) return 1; else return 0;
> > }
> >
> > void solve() {
> > push(st,a);
> > while (st!=NULL) {
> > int v=top(st);
> > if (m[v]!=NULL) push(st,pop(m[v]));
> > else push(ce,pop(st));
> > }
> > }
> >
> > void print() {
> > int prev=pop(ce);
> > while(ce!=NULL) {
> > cout<<prev<<" "<<top(ce)<<endl;
> > prev=pop(ce);
> > }
> > }
> >
> > int main() {
> > st=NULL;
> > ce=NULL;
> > init();
> > solve();
> > print();
> > return 0;
> > }
You're making me smile :))
Well,
1. I know a person who got AC with exactly that :))
2. Finding an Euler Cycle in a graph is not a bad algorithm. It isO
(|E|) operations, where E is the quantity of the edges in the graph.
And if it is 32000, calculate how much time it takes :))) Certainly,
the algorithm (having in mind that even someone got AC with that ) is
not bad........................
And.....................
What is more, I
1. Read the input(there is no way to escape from that :)))) )
2. Go through the graph once(to find the cycle) (can't solve the
problem without going through the edges........)
3. Print it (no way to escape, too :))) )
So, where is the problem? I cannot realise how can you solve it
without doing all these :))
Btw, what is your greedy?
One last thing
Well, it's possible that I'm mistaken beacuse I'm rather sleepy now,
but I think that the algorithm is not so bad :))))))))))))))))))
You are creative :)
Послано
HNT 19 фев 2002 12:06
It's only a joke. Your algorithm is rigth, but yout implemalation isn't so good (Use linked list!!!)
> Well, it's possible that I'm mistaken beacuse I'm rather sleepy
now,
> but I think that the algorithm is not so bad :))))))))))))))))))
Re: It's only a joke. Your algorithm is rigth, but yout implemalation isn't so good (Use linked list!!!)
Does it matter whether I use a linked list or a stack? To me, it does
not matter AT ALL...Because I use always the first element, so the
stack is wonderful.. Or perhaps you are joking again?