万能的dfs

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
#define MAX 1000
int N, M;
struct edge {
	int id;//边的唯一id
	int d;//目标顶点
};
vector< vector<edge> >g(MAX);
bool ola_exist =false;//欧拉回路不存在
int edgenum;//还剩下的没走的边
vector<bool>visited;
int start;
//从k点开始画
void dfs(int k) {
	if (edgenum == 0) {
		if (k == start)ola_exist = true;//找到了欧拉回路
		return;
	}
	
	for (int i = 0; i < g[k].size(); i++) {
		if (ola_exist)return;
		if (!visited[g[k][i].id]) {
			visited[g[k][i].id] = true;
			edgenum--;
			dfs(g[k][i].d);
			edgenum++;
			visited[g[k][i].id] = false;
		}
	}
}
int main() {
	while (cin >> N) {
		for(int i=0;i<g.size();i++)g[i].clear();
		ola_exist = false;
		vector<edge>no; g.push_back(no);//没有0号节点
		if (N == 0)break;
		cin >> M; edgenum = M;
		for (int i = 1; i <= M; i++) {
			int a, b;
			cin >> a >> b;
			edge e; e.id = i;
			e.d = b; g[a].push_back(e);
			e.d = a; g[b].push_back(e);
		}
		//对visited数组进行初始化
		for (int i = 0; i <= M; i++)
			visited.push_back(false);
		for (int i = 1; i < N; i++) {
			if (ola_exist)break;
			start = i;
			dfs(i);
		}
		cout << ((ola_exist)?1:0) << endl;

	}
	return 0;
}