#include #include #define rep(i,b) for(int i=0;i=0;i--) #define rep1(i,b) for(int i=1;i=x;i--) #define fore(i,a) for(auto& i:a) #define rng(x) (x).begin(), (x).end() #define rrng(x) (x).rbegin(), (x).rend() #define sz(x) ((int)(x).size()) #define pb push_back #define fi first #define se second #define pcnt __builtin_popcountll using namespace std; using namespace atcoder; using ll = long long; using ld = long double; template using mpq = priority_queue, greater>; template bool chmax(T &a, const T &b) { if (a bool chmin(T &a, const T &b) { if (b ll sumv(const vector&a){ll res(0);for(auto&&x:a)res+=x;return res;} bool yn(bool a) { if(a) {cout << "Yes" << endl; return true;} else {cout << "No" << endl; return false;}} #define retval(x) {cout << #x << endl; return;} #define cout2(x,y) cout << x << " " << y << endl; #define coutp(p) cout << p.fi << " " << p.se << "\n"; #define out cout << ans << endl; #define outd cout << fixed << setprecision(20) << ans << endl; #define outm cout << ans.val() << endl; #define outv fore(yans , ans) cout << yans << "\n"; #define outdv fore(yans , ans) cout << yans.val() << "\n"; #define assertmle(x) if (!x) {vi v(3e8);} #define asserttle(x) if (!x) {while(1){}} #define coutv(v) {fore(vy , v) {cout << vy << " ";} cout << endl;} #define coutv2(v) fore(vy , v) cout << vy << "\n"; #define coutvm(v) {fore(vy , v) {cout << vy.val() << " ";} cout << endl;} #define coutvm2(v) fore(vy , v) cout << vy.val() << "\n"; using pll = pair;using pil = pair;using pli = pair;using pii = pair;using pdd = pair; using vi = vector;using vd = vector;using vl = vector;using vs = vector;using vb = vector; using vpii = vector;using vpli = vector;using vpll = vector;using vpil = vector; using vvi = vector>;using vvl = vector>;using vvs = vector>;using vvb = vector>; using vvpii = vector>;using vvpli = vector>;using vvpll = vector;using vvpil = vector; using mint = modint998244353; //using mint = modint1000000007; //using mint = dynamic_modint<0>; using vm = vector; using vvm = vector>; vector dx={1,0,-1,0,1,1,-1,-1},dy={0,1,0,-1,1,-1,1,-1}; ll gcd(ll a, ll b) { return a?gcd(b%a,a):b;} ll lcm(ll a, ll b) { return a/gcd(a,b)*b;} #define yes {cout <<"Yes"< mp; rep(i,n) fore(y , e[i]){ if (led[i] == led[y]) continue; if (mp.find({led[i],led[y]}) != mp.end()) continue; scc_g[led[i]].pb(led[y]); scc_g_rev[led[y]].pb(led[i]); from_deg[led[i]]++; to_deg[led[y]]++; mp[{led[i],led[y]}] = 0; } return; } // 計算量はO(n^2) void cnt_init(){ reach.resize(m,vb(m,false)); rep(i,m){ reach[i][i] = true; queue q; q.push(i); while(!q.empty()){ int p = q.front(); q.pop(); fore(y , scc_g[p]){ if (reach[i][y]) continue; reach[i][y] = true; q.push(y); } } rep(j,m){ if(reach[i][j]){ to_cnt[j] += sz[i]; from_cnt[i] += sz[j]; } } } return; } }; // [メンバ関数] // scc(n) : 初期化。n頂点。 // add_edge(i , j) : 頂点iから頂点jへ辺を追加。 // init() : scc実行。強連結成分を1つの頂点と見做したときのグラフ(scc_g)を作成。計算量はO(n+m)。mは辺数。 // cnt_init() : 任意の強連結成分同士の到達判定。任意の頂点への(から)到達可能な頂点の数を計算。計算量はO(n^2)。 // [メンバ変数] // n : 頂点数 // m : 強連結成分数 // e : e[i]はvector。成分分解前の、始点をiとする有向辺の終点を格納 // v : v[i]はvector。強連結成分iに属する頂点集合。 // led[i] : 頂点iが属する強連結成分番号 // scc_g[i] : 強連結成分を1つの頂点と見做したときの有向辺情報を格納、形式はeと同様 // scc_g_rev[i] : scc_gにおいて、始点と終点を入れ替えたものに当たる。 // from_deg[i] : 強連結成分iから出る辺の数 // to_deg[i] : 強連結成分iに入る辺の数 //////// 以下、計算量はO(n^2)のcnt_init()を呼ぶことで初期化される /////////////// // from_cnt[i] : iから到達可能な頂点数 // to_cnt[i] : iに到達可能な頂点数 // reach[i][i2] : iからi2に到達可能なときtrue、そうでないときfalse void solve(){ int n,m; cin>>n>>m; vvi e(n); scc sc(n); map mp2; map used; rep(i,m){ int c,d; cin>>c>>d; c--; d--; e[c].pb(d); pii p = {c,d}; used[p]++; if (mp2.count(p)) continue; sc.add_edge(c,d); mp2[p] = 0; } sc.init(); int l = sc.m; vector> s(n); auto use = [&](int i,int j)->void{ pii p = {i,j}; used[p]--; }; vb flg(n); rep(i,l){ vi v = sc.v[i]; map mp; fore(y , v) mp[y] = 0; fore(y , v){ fore(yy , e[y]){ if (mp.count(yy)==0) continue; s[y].insert(yy); } } auto step = [&](int y)->void{ vi perm; set now; int st = y; now.insert(st); perm.pb(st); while(sz(perm)){ int nxt = -1; int x = perm.back(); while(sz(s[x])){ int can = *s[x].begin(); if (flg[can]){ s[x].erase(s[x].begin()); continue; } nxt = can; break; } if (nxt==-1){ flg[x] = true; perm.pop_back(); now.erase(x); continue; } if (now.count(nxt)==0){ perm.pb(nxt); now.insert(nxt); s[x].erase(s[x].begin()); continue; } s[x].erase(s[x].begin()); x = nxt; while(perm.back()!=nxt){ use(perm.back(), x); x = perm.back(); perm.pop_back(); now.erase(x); assert(sz(perm)); if (perm.back()==nxt){ use(perm.back(), x); x = perm.back(); perm.pop_back(); now.erase(x); break; } } } }; fore(y , v){ if (flg[y]) continue; step(y); } } vpii ans; rep(i,n) fore(y , e[i]){ pii p = {i,y}; if (used[p]){ used[p]--; ans.pb({i+1, y+1}); } } cout << n << " " << sz(ans) << endl; fore(y , ans) coutp(y); return; } int main(){ ios::sync_with_stdio(false); cin.tie(0); int t = 1; //cin>>t; rep(i,t){ solve(); } return 0; }