为什么在链式前向星搭配当前弧优化时 head 不能初始化为 -1。
具体的对于倒着找无向图欧拉回路的以下两种实现(来源于 UOJ 评测记录 #589619 与 UOJ 评测记录 #589608):
// Code 1
int head[N],edge[M << 1],fail[M << 1],idx[M << 1],tot;
void add(int x,int y,int z) {
edge[++ tot] = y,idx[tot] = z,fail[tot] = head[x],head[x] = tot;
}
void dfs(int x) {
for (int &i = head[x]; ~i; i = fail[i]) {
int v = edge[i],w = idx[i];
if (vis[i]) continue;
vis[i] = vis[i ^ 1] = 1;
dfs(v);
ans[++ cnt] = - w;
}
}
void Init(){
memset(head,-1,sizeof head);
tot = -1;
}
// Code 2
int head[N],edge[M << 1],fail[M << 1],idx[M << 1],tot;
void add(int x,int y,int z) {
edge[++ tot] = y,idx[tot] = z,fail[tot] = head[x],head[x] = tot;
}
void dfs(int x) {
for (int &i = head[x]; i; i = fail[i]) {
int v = edge[i],w = idx[i];
if (vis[i]) continue;
vis[i] = vis[i ^ 1] = 1;
dfs(v);
ans[++ cnt] = - w;
}
}
void Init(){
memset(head,0,sizeof head);
tot = 1;
}
其中第一种前向星的实现方式并不能顺利 AC(在 cf 上提示 out of bounds,记录)