And UVa11077 's analysis is very similar.
We fixed the shoes on our left foot, and then we considered the shoes of the right foot as a substitution decomposition.
For a circular section with a length of L, swap to the correct position at least L-1 times.
1#include <cstdio>2#include <cstring>3#include <map>4 using namespacestd;5 6 BOOLvis[10000+Ten];7 8 intMain ()9 {Ten //freopen ("In.txt", "R", stdin); One A intT; -scanf"%d", &T); - while(t--) the { - intN, a, B; -map<int,int>m; -scanf"%d", &n); + for(inti =0; I < n; i++) - { +scanf"%d%d", &a, &b); AM[a] =b; at } - intAns =0; -map<int,int>:: iterator It; -memset (Vis,false,sizeof(Vis)); - for(It = M.begin (); It! = M.end (); it++) - { in inti = it->First ; - if(!Vis[i]) to { + intCNT =0; - intj =i; the Do * { $cnt++;Panax NotoginsengVIS[J] =true; -j =M[j]; the} while(J! =i); +Ans + = cnt-1; A } the } +printf"%d\n", ans); - } $ $ return 0; -}
code June
UVa 11330 (decomposition of the replacement cycle) Andy's Shoes