結果

問題 No.2464 To DAG
ユーザー Astral__Astral__
提出日時 2023-09-08 22:50:41
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 826 ms / 2,000 ms
コード長 7,535 bytes
コンパイル時間 2,955 ms
コンパイル使用メモリ 225,764 KB
実行使用メモリ 89,736 KB
最終ジャッジ日時 2023-09-08 22:51:22
合計ジャッジ時間 29,900 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 664 ms
89,736 KB
testcase_03 AC 757 ms
38,256 KB
testcase_04 AC 705 ms
38,440 KB
testcase_05 AC 639 ms
38,344 KB
testcase_06 AC 630 ms
38,536 KB
testcase_07 AC 530 ms
24,508 KB
testcase_08 AC 610 ms
25,048 KB
testcase_09 AC 548 ms
21,972 KB
testcase_10 AC 619 ms
21,540 KB
testcase_11 AC 383 ms
16,720 KB
testcase_12 AC 326 ms
14,112 KB
testcase_13 AC 377 ms
16,528 KB
testcase_14 AC 510 ms
25,924 KB
testcase_15 AC 575 ms
31,340 KB
testcase_16 AC 302 ms
18,572 KB
testcase_17 AC 46 ms
5,344 KB
testcase_18 AC 204 ms
12,396 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 1 ms
4,380 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 402 ms
17,980 KB
testcase_23 AC 418 ms
17,564 KB
testcase_24 AC 412 ms
17,592 KB
testcase_25 AC 421 ms
17,600 KB
testcase_26 AC 137 ms
10,092 KB
testcase_27 AC 826 ms
38,324 KB
testcase_28 AC 788 ms
38,260 KB
testcase_29 AC 772 ms
38,380 KB
testcase_30 AC 757 ms
22,812 KB
testcase_31 AC 756 ms
19,864 KB
testcase_32 AC 732 ms
19,592 KB
testcase_33 AC 541 ms
33,032 KB
testcase_34 AC 252 ms
18,620 KB
testcase_35 AC 312 ms
18,076 KB
testcase_36 AC 317 ms
17,976 KB
testcase_37 AC 243 ms
17,480 KB
testcase_38 AC 245 ms
17,440 KB
testcase_39 AC 15 ms
24,132 KB
testcase_40 AC 15 ms
24,120 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#define PPque priority_queue<tuple<ll, ll, ll>, vector<tuple<ll, ll, ll>>, greater<tuple<ll, ll, ll>>>
#define Pque  priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>>
#define pque priority_queue<int, vector<int>, greater<int>>
#define umap unordered_map
#define uset unordered_set
#define rep(i, s, f) for(ll i = s; i <= f; i++)
#define per(i, s, f) for(ll i = s; i >= f; i--)
#define all0(x) (x).begin() ,(x).end()
#define all(x) (x).begin() + 1, (x).end()
#define vvvi vector<vector<vector<int>>>
#define vvvl vector<vector<vector<ll>>>
#define vvvc vector<vector<vector<char>>>
#define vvvd vector<vector<vector<double>>>
#define vvvm vector<vector<vector<mint>>>
#define vvi vector<vector<int>>
#define vvl vector<vector<ll>>
#define vvs vector<vector<string>>
#define vvc vector<vector<char>>
#define vvp vector<vector<pair<ll, ll>>>
#define vvb vector<vector<bool>>
#define vvd vector<vector<double>>
#define vvm vector<vector<mint>>
#define vp vector<pair<ll, ll>>
#define vi vector<int>
#define vl vector<ll>
#define vs vector<string>
#define vc vector<char>
#define vb vector<bool>
#define vd vector<double>
#define vm vector<mint>
#define P pair<ll, ll>
#define TU tuple<ll, ll, ll>
#define rrr(l, r) mt()%(r-l+1)+l
#define ENDL '\n'

#define ull unsigned long long
typedef long long ll;
using namespace std;

////////////////////////////////////////////////////////////////////////////////////////////////////////////
//これが本当の組み込み関数ってね(笑)

template <typename T>
T or_less(vector<T> &A, T x) { //x以下で最大要素の添字 前提: sort済み 存在しない: -1
  return distance(A.begin(), upper_bound(A.begin(), A.end(), x)-1);
}
template <typename T>
T under(vector<T> &A, T x) { //x未満の最大要素の添字 前提: sort済み 存在しない: -1
  return distance(A.begin(), lower_bound(A.begin(), A.end(), x)-1);
}
template <typename T>
T or_more(vector<T> &A, T x) { //x以上で最小要素の添字  前提: sort済み 存在しない: N .   //distanceのA.beginは添字を出すために常にA.begin() NG: A.begin() + 1
  return distance(A.begin(), lower_bound(A.begin(), A.end(), x));
}
template <typename T>
T over(vector<T> &A, T x) { //xより大きい最小要素の添字前提: sort済み 存在しない: N
  return distance(A.begin(), upper_bound(A.begin(), A.end(), x));
}
void compress(vector<ll> &A) {//小さい順に順位、大きい順にしたいならreverseはNG最後に変換
    vector<ll> temp = A;
    sort(temp.begin()+1, temp.end());
    for (int i = 1; i <= int(A.size()-1); i++) {
        A.at(i) = distance(temp.begin(), lower_bound(temp.begin()+1, temp.end(), A.at(i)));
    }
}
//////////////////////////////////////////////////////////////////////
//数学系
///////////////////////////////////////////////////////////////////////
ll round(ll x, ll i) {return ll(x + 5 * pow(10, i-1))/ll(pow(10, i)) * ll(pow(10, i));}
ll nto10(string S, ll base) {
  ll res = 0;
  reverse(all0(S));
  while(!S.empty()) {
    ll num = S.back() - '0';
    if(num < 0 || num > 9) num = 9 + S.back() - 'a' + 1;
    res = res * base + num;
    S.pop_back();
  }
  return res;
}
string toN(ll N, ll base) {
  if(N == 0) return "0";
  string ans ="";
  ll MOD = abs(base);
  while(N != 0) {
    ll first = N % MOD;
    while(first < 0) first += MOD;
    ans += to_string(first);
    N -= first;
    N /= base;
  }
  reverse(all0(ans));
  return ans;
}
vp insu_bunkai(ll N) {
  vp res;
  for (int i = 2; i * i <= N; i++) {
    ll cnt = 0;
    while(N % i == 0) {
      cnt++;
      N /= i;
    }
    if(cnt != 0) res.push_back(P(i, cnt));
  }
  if(N != 1) res.push_back(P(N, 1));
  return res;
}
struct comb_fast {//must素数
  vl fac;
  vl facinv;
  vl inv;
  ll mod_comb;
  comb_fast (ll n, ll mod) {
    mod_comb = mod;
    fac.assign(n+1, 1);
    facinv.assign(n+1, 1);
    inv.assign(n+1, 1);
    rep(i, 2, n) {
      fac.at(i) = fac.at(i-1) * i % mod_comb;
      inv.at(i) = mod_comb - inv.at(mod_comb%i) * (mod_comb/i)%mod_comb;
      facinv.at(i) = facinv.at(i-1) * inv.at(i) % mod_comb;
    }
  }
  ll get(ll n, ll k) {
    if(n < k) return 0;
    if(n < 0 || k < 0) return 0;
    return fac.at(n) * (facinv.at(k) * facinv.at(n-k)%mod_comb)%mod_comb;
  }
};
vvl comb_slow(ll n, ll mod) {//計算にO(N^2) 読み取りにO(1)
  vvl v(n+1, vl(n+1, 0));
  rep(i, 0, v.size() - 1) {v.at(i).at(0) = 1;v.at(i).at(i) = 1;}
  rep(i, 1, v.size()-1) { rep(j, 1, i) { v.at(i).at(j) = v.at(i-1).at(j-1) + v.at(i-1).at(j);v.at(i).at(j) %= mod;}}
  return v;
}

double KAKUDO(double rad){return rad * 180 / 3.141592653589793;}
double RAD(double kakudo) {return kakudo / 180 * 3.141592653589793;}
ll extgcd (ll a, ll b,  ll &x, ll &y) {
  if(b == 0) { x = 1;y = 0;return a;}
  ll d = extgcd(b, a%b, y, x);
  y -= a/b * x;
  return d;
}
template <typename T>
T ceil(T a, T b) {
  assert(b != 0);
  if(a % b == 0) return a / b;
  if((a <= 0 && b < 0) || (a >= 0 && b > 0)) return a/b + 1;
  else return a / b;
}
template <typename T>
T floor(T a, T b) {
  assert(b != 0);
  if(a % b == 0) return a / b;
  if((a <= 0 && b < 0) || (a >= 0 && b > 0)) return a/b;
  else return a/b - 1;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//グローバル変数を置くところ(情報工学意識高め)
//#pragma GCC optimize("O3")
//#pragma GCC optimize("unroll-loops")
ll int_max = 1001001001;
ll ll_max = 1001001001001001001LL;
const double pi = 3.141592653589793;
vl dx{0, 1, 0, -1, 0, 1, 1, -1, -1};//右から時計回り
vl dy{0, 0, -1, 0, 1, 1, -1, -1, 1};
//#pragma GCC optimize ("-O3")
//const ll mod = 1000000007;
//const ll mod = 998244353;


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void solve() {
  ll N, M;
  cin >> N >> M;
  vector<multiset<ll>> G(N+1, multiset<ll>());
  rep(i, 1, M) {
    ll u, v;
    cin >> u >> v;
    G.at(u).insert(v);
  }
  vvl ans(N+1, vl());


  auto dfs = [&](auto dfs, ll now, set<ll> & visited) -> ll {
    if(visited.count(now)) {
     
      return now;
    }
     if(G.at(now).empty()) return -1;
    visited.insert(now);
   
    auto itr = G.at(now).begin();
    while(itr != G.at(now).end()) {
      ll d = dfs(dfs, *itr, visited);
      if(d != -1) {
        G.at(now).erase(itr++);
        if(d != now) {
          visited.erase(now);
          return d;
        }
      }
      else {
        ans.at(now).push_back(*itr);
        G.at(now).erase(itr++);
      }
    }
    visited.erase(now);

    return -1;
  };

  

  rep(i, 1, N) {

      set<ll> visited;
      dfs(dfs, i, visited);
    
  }

  ll cnt = 0;
    rep(i,1, N) {
    for(auto to : ans.at(i)) {
      cnt++;
    }
  }

  cout << N << " " << cnt << endl;

  rep(i,1, N) {
    for(auto to : ans.at(i)) {
      cout << i << " "<< to << endl;
    }
  }






}
//座標を2で割った時の”切り捨て側(左側)”を求めるには 誤:(x / 2) マイナスの時!!! 正:floor<ll>(x, 2);
//stringでの数字の下から1桁目は 正:S.at(N-1)  誤:S.at(0)
//if(S.at(i) == 1 ← charなのに1...?
// modは取りましたか...?(´・ω・`)



int main() {
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  cout << fixed << setprecision(15);
  solve();
  return 0;
}
0