#include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef __int64 ll; const int N=200025; int max(int a,int b){return a>b?a:b;} int min(int a,int b){return a0 || (cmpr==0 && max(a,b)>max(c,d))) a=c,b=d; } void bfs(int n) { memset(dep,0,sizeof(dep)); memset(circ,0,sizeof(circ)); int tai=0,hed=0,i,u,v; for(i=0;ipto[v][0]) { pto[v][1]=pto[v][0]; } dep[v][0]=dep[u][0]+1; pto[v][0]=pto[u][0]; } else if(dep[v][0]==dep[u][0]+1 && pto[v][0]>pto[u][0]) { pto[v][0]=pto[u][0]; } else if(dep[v][1]pto[u][0]) { pto[v][1]=pto[u][0]; } if(circ[v]==circ[u]) { isvalid(cto[v][0],cto[v][1],cto[u][0],cto[u][1]); } else if(circ[v]=2;u=to[v]) { lop[lpl++]=u; for(v=head[u];v!=-1 && inu[to[v]]<2;v=nxt[v]); if(v==-1)break; inu[u]--; } lop[lpl]=lop[0]; for(i=0;itmp) { ans=tmp; bestu=cto[lop[i]][0]; bestt=cto[lop[i]][1]; } //notice:The loop can walk without 1 edge if(pmax1!=99999999) { tmp=2*n-(i+dep[lop[i]][0]+max1)-2; if(ans==tmp) { isvalid(bestu,bestt,pto[lop[i]][0],pto[lop[pmax1]][0]); } else if(ans>tmp) { ans=tmp; bestu=pto[lop[i]][0]; bestt=pto[lop[pmax1]][0]; } } if(max1pto[lop[i]][0]) { pmax1=i; } if(pmax2!=99999999) { tmp=2*n-(lpl-i+dep[lop[lpl-i-1]][0]+max2)-2; if(ans==tmp) { isvalid(bestu,bestt,pto[lop[lpl-i-1]][0],pto[lop[pmax2]][0]); } else if(ans>tmp) { ans=tmp; bestu=pto[lop[lpl-i-1]][0]; bestt=pto[lop[pmax2]][0]; } } if(max2pto[lop[lpl-i-1]][0]) { pmax2=lpl-i-1; } } printf("Case #%d: %d %d %d\n",h,ans,min(bestu,bestt)+1,max(bestu,bestt)+1); } return 0; }