#include using namespace std; pair,vector>> FunctionalGraph(const vector &A){ //N頂点N辺有向出次数全て1のグラフでreturn{cycle所属番号(-1は未所属),cycle頂点集合}. int N = A.size(),nowc = 0; vector belong(N,-1); vector> cycle; vector already(N),visited(N); for(int i=0; i now = {pos}; belong.at(pos) = nowc; pos = A.at(pos); while(pos != memo) now.push_back(pos),belong.at(pos) = nowc,pos = A.at(pos); cycle.emplace_back(now),nowc++; } return {belong,cycle}; } int main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); int N; cin >> N; vector P(N); for(auto &p : P) cin >> p,p--; vector> D(N); for(int i=1; i answer(N); for(auto &c : cycle){ if(c.size() == 1) continue; int g = 0; for(int i=1; i