基环树找环
有向图:
void getloop(){
int x=1;
memset(cir,false,sizeof(cir));
while (!cir[x]) cir[x]=true,x=pre[x];
lis[0]=0;
memset(cir,false,sizeof(cir));
while (!cir[x])
lis[++lis[0]]=x,cir[x]=true,x=pre[x];
}
无向图:
void getloop(int u,int fu){
vis[u]=1;
for(re int e=head[u];e;e=nxt[e]){
int v=to[e];
if(flag[e]||flag[e^1]) continue;
flag[e]=flag[e^1]=1;
if(vis[v]){
loop[++tot]=v;
mark[v]=1;
int tmp=w[e];
for(re int x=u;x!=v;x=pre[x]){
loop[++tot]=x;mark[x]=1;sum[tot]=tmp;
tmp=prew[x];
}sum[1]=tmp;
}
pre[v]=u;prew[v]=w[e];
getloop(v,u);
}
}
Q:为什么要分开?
A:对于有向图而言,如果用无向图的写法,万一第一次就遍历的是叶子节点,不就凉了吗…………