{ VI resu,resv; graph g; int @n,@m; VI u(m),v(m); rd((u,v)(m)); g.setDirectEdge(n+1,m,u.data(),v.data()); VI ss(n+1); int sn=g.scc(ss.data()); // VVI vvi(sn); vector vvi(sn); rep(i,1,n+1){ vvi[ss[i]].push_back(i); } rep(j,sn){ int n=vvi[j].size(); if(n>1){ rep(k,n-1){ resu.push_back(vvi[j][k]); resv.push_back(vvi[j][k+1]); } resu.push_back(vvi[j][n-1]); resv.push_back(vvi[j][0]); } } unionFind uf; uf.walloc(n+1,1); rep(i,m){ int su=ss[u[i]]; int sv=ss[v[i]]; if(su!=sv){ uf(su,sv); } } vector vv(sn); rep(i,sn){ vv[uf(i)].push_back(i); } rep(i,sn){ int n=vv[i].size(); if(n>1){ sort(vv[i].begin(),vv[i].end()); rep(j,n-1){ resu.push_back(vvi[vv[i][j]][0]); resv.push_back(vvi[vv[i][j+1]][0]); } } } wt(int(resu.size())); rep(i,resu.size()){ wt(resu[i],resv[i]); } }