Hi, I hope somebody can help me here :-(... this program compiles well and after it crashes.

#include "TStack.h"
#include <math.h>
#include <stdio.h>
#include <vector>
using namespace std;

 //FILE *f=fopen("input.txt","r");
 //FILE *g=fopen("output.txt","w");
 void main(){
	 int n;
	 scanf("%d", &n);
	 vector <vector<int> > g (n, vector<int> (n));
    vector <int> deg (n);
	 
	 for (int i=0; i<n; ++i)
		 for (int j=0; j<n; ++j)
			 deg[i]+=g[i][j];
	 int first=0;
	 while (!deg[first]) ++first;
	 int v1=-1, v2=-1;
	 bool bad=false;
	 for (int i=0; i<n; ++i)
		 if (deg[i] & 1)
			 if (v1==-1)
				 v1=i;
			 else if (v2==-1)
				 v2=i;
			 else bad=true;
		 if (v1!=-1){
			 ++g[v1][v2];
			 ++g[v2][v1];
		 }
	 TStack st;
	 st.push (first);
	 vector<int> res;
	 while (!st.empty()){
		 int v=st.pop();
		 int i;
		 for (i=0; i<n; ++i)
			 if (g[v][i]) break;
		 if (i==n){
			 res.push_back(v);
			 st.pop();
		 }
		 else{
			 --g[v][i];
			 --g[i][v];
			 st.push (i);
		 }
	 }
	 if (v1!=-1)
		 for (size_t i=0; i+1<res.size(); ++i)
			 if (res[i]==v1 && res[i+1]==v2 || res[i]==v2 && res[i+1] ==v1){
				 vector<int> res2;
				 for (size_t j=i+1; j<res.size(); ++j)
					 res2.push_back (res[j]);
				 for (size_t j=1; j<=1; ++j)
					 res2.push_back (res[j]);
				 res=res2;
				 break;
			 }
		 for (int i=0; i<n; ++i)
			 for (int j=0; j<n; ++j)
				 if (g[i][j])
				    bad=true;
		 if (bad) puts ("-1");

 }

I went through the code several times but I don't find any errors... somebody has a hint?

Thanks all!

Salem commented: Congrats on using code tags on your first attempt - keep it up! +24

You need to learn to debug yourself. use print statements so you can just exactly when it crashed. Use a debugger :shock: step through it line by line until it crashes. Also is the C++ or C?

and from now on NEVER user void main() always use int main() and return 0;

Chris

1. Well done on using code tags, not many 1-posters manage to do that.

2. Here is your code, indented with what I think it actually means.

#include "TStack.h"
#include <math.h>
#include <stdio.h>
#include <vector>
using namespace std;

//FILE *f=fopen("input.txt","r");
//FILE *g=fopen("output.txt","w");
void main(){
    int n;
    scanf("%d", &n);
    vector <vector<int> > g (n, vector<int> (n));
    vector <int> deg (n);
    
    for (int i=0; i<n; ++i)
        for (int j=0; j<n; ++j)
            deg[i]+=g[i][j];

    int first=0;
    while (!deg[first]) ++first;

    int v1=-1, v2=-1;
    bool bad=false;
    for (int i=0; i<n; ++i)
        if (deg[i] & 1)
            if (v1==-1)
                v1=i;
            else if (v2==-1)
                v2=i;
            else bad=true;

    if (v1!=-1){
        ++g[v1][v2];
        ++g[v2][v1];
    }

    TStack st;
    st.push (first);
    vector<int> res;
    while (!st.empty()){
        int v=st.pop();
        int i;
        for (i=0; i<n; ++i)
            if (g[v][i]) break;

        if (i==n){
            res.push_back(v);
            st.pop();
        }
        else{
            --g[v][i];
            --g[i][v];
            st.push (i);
        }
    }

    if (v1!=-1)
        for (size_t i=0; i+1<res.size(); ++i)
            if (res[i]==v1 && res[i+1]==v2 || res[i]==v2 && res[i+1] ==v1){
                vector<int> res2;
                for (size_t j=i+1; j<res.size(); ++j)
                    res2.push_back (res[j]);
                for (size_t j=1; j<=1; ++j)
                    res2.push_back (res[j]);
                res=res2;
        break;
        }

    for (int i=0; i<n; ++i)
        for (int j=0; j<n; ++j)
            if (g[i][j])
                bad=true;
    if (bad) puts ("-1");
}

I say "think", because there is a woeful lack of { } to make what you want explicit. There are some very long statements (apparently), and even very small edits will radically alter the flow of the code.

> deg+=g[j];
What is g initialised to?

> while (!deg[first]) ++first;
What keeps first within the bounds of the vector?

> if (v1!=-1)
v2 can still be -1 though?

> void main
main returns an int

Just a sample of possible things which could be wrong.

Uhmm... First enclose code used by for, while, if, etc. in brakets }{ . It's hard to spot where things end and begin without them.

And where does it crash? Have you tried breakpoints in a debugger, or even just putting couts/printf in the code to see where you are?

Why are you using things like scanf, ands puts?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.