## 【算法笔记】非递归DFS

void DFS(int w, int f){
l[w] = ++tot; dep[w] = dep[f]+1;
if (to[i] != f) DFS(to[i], w);
r[w] = tot; fa[w][0] = f;
}


inline void BFS(){
BUF[1] = 1; dep[1] = 1;
for (int fro=1,bak=1;bak<=fro;bak++){
int w = BUF[bak];
if (!dep[to[i]]) dep[to[i]] = dep[w]+1,
fa[to[i]][0] = w, BUF[++fro] = to[i];
}
}
}

inline void DFS(){
for (int i=1;i<=n;i++) now[i] = head[i];
int w = 1; tot = 1;
while (w) {
if (!now[w]) {
r[w] = tot;
w = fa[w][0];
} else {
if (!l[w]) l[w] = ++tot;
int tmp = to[now[w]]; now[w] = nxt[now[w]];
if (dep[tmp] != dep[w]+1) continue;
else w = tmp;
}
}
}


## 【Tricks】手动扩栈

G++:
int size = 256 << 20; // 256MB
char *p = (char*)malloc(size) + size;
__asm__("movl %0, %%esp\n" :: "r"(p));

C++:


———— 2016.8.28 更新 ————-

const int main_stack=16;
int main()
{
__asm__("movl %%esp, (%%eax);\n"::"a"(my_stack):"memory");
__asm__("movl %%eax, %%esp;\n"::"a"(my_stack+sizeof(my_stack)-main_stack):"%esp");
my_main();
__asm__("movl (%%eax), %%esp;\n"::"a"(my_stack):"%esp");
return 0;
}


—————————— UPD 2017.7.3 ——————————