template<typenameT>classDistance{std::map<T,int>d;std::queue<T>que;public:// s から終端にたどり着くまでの距離template<typenameF>intshortest_path(constF&f,constT&s){if(d.count(s))returnd[s];if(!f(s).size())returnd[s]=0;intres=1e9;for(constT&to:f(s))res=std::min(res,shortest_path(f,to)+1);returnd[s]=res;}// s からの最短距離が MAX 以下のものを列挙template<typenameF>std::vector<std::vector<T>>from_root(constF&f,constT&s,intMAX=10){std::vector<std::vector<T>>level(MAX+1);d[s]=0;que.push(s);while(que.size()){Tv=que.front();que.pop();intD=d[v];for(constT&to:f(v)){if(d.count(to))continue;d[to]=D+1;if(D+1<MAX)que.push(to);}}for(constauto&[key,val]:d)level[val].push_back(key);returnlevel;}template<typenameF>std::vector<std::vector<T>>from_root(constF&f,conststd::vector<T>&s,intMAX=10){std::vector<std::vector<T>>level(MAX+1);for(constT&v:s){d[v]=0;que.push(v);}while(que.size()){Tv=que.front();que.pop();intD=d[v];for(constT&to:f(v)){if(d.count(to))continue;d[to]=D+1;if(D+1<MAX)que.push(to);}}for(constauto&[key,val]:d)level[val].push_back(key);returnlevel;}};
#line 1 "library/local/Distance.hpp"
template<typenameT>classDistance{std::map<T,int>d;std::queue<T>que;public:// s から終端にたどり着くまでの距離template<typenameF>intshortest_path(constF&f,constT&s){if(d.count(s))returnd[s];if(!f(s).size())returnd[s]=0;intres=1e9;for(constT&to:f(s))res=std::min(res,shortest_path(f,to)+1);returnd[s]=res;}// s からの最短距離が MAX 以下のものを列挙template<typenameF>std::vector<std::vector<T>>from_root(constF&f,constT&s,intMAX=10){std::vector<std::vector<T>>level(MAX+1);d[s]=0;que.push(s);while(que.size()){Tv=que.front();que.pop();intD=d[v];for(constT&to:f(v)){if(d.count(to))continue;d[to]=D+1;if(D+1<MAX)que.push(to);}}for(constauto&[key,val]:d)level[val].push_back(key);returnlevel;}template<typenameF>std::vector<std::vector<T>>from_root(constF&f,conststd::vector<T>&s,intMAX=10){std::vector<std::vector<T>>level(MAX+1);for(constT&v:s){d[v]=0;que.push(v);}while(que.size()){Tv=que.front();que.pop();intD=d[v];for(constT&to:f(v)){if(d.count(to))continue;d[to]=D+1;if(D+1<MAX)que.push(to);}}for(constauto&[key,val]:d)level[val].push_back(key);returnlevel;}};