P14002 题解
DaiRuiChen007 · · 题解
Problem Link
先从树的情况开始,直接 dfs,
考虑特殊部分分:图是点仙人掌且起点不在环上。
考虑朴素的算法在什么情况下出错,对于一个环,我们从
我们要在
如果这个点是
然后考虑边仙人掌的情况,此时会有两个相交的环,由于我们在
所以在
但对于起点
时间复杂度
代码:
#include<bits/stdc++.h>
using namespace std;
pair<int,int> navigate(int c,vector<int>g) {
array<int,4> f={0,0,0,0},p;
for(int i=0;i<(int)g.size();++i) ++f[g[i]],p[g[i]]=i;
if(f[3]) return {f[2]>1?2:0,p[3]}; //judge direction
if(f[0]) return {c==3?1:2,p[0]}; //go right direction(c=3) or expand
if(f[2]==1) return {1,p[2]}; //revisit the cycle
if(f[2]==2) {
if(c==0||c==2) return {3,p[2]}; //now check the direction
if(c==3) return {1,find(g.begin(),g.end(),2)-g.begin()}; //go right direction
}
return {-1,-1};
}