結果

問題 No.2464 To DAG
ユーザー fumofumofunifumofumofuni
提出日時 2023-09-10 21:26:56
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 740 ms / 2,000 ms
コード長 2,170 bytes
コンパイル時間 3,405 ms
コンパイル使用メモリ 209,036 KB
実行使用メモリ 39,744 KB
最終ジャッジ日時 2023-09-10 21:27:31
合計ジャッジ時間 25,184 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 367 ms
39,744 KB
testcase_03 AC 698 ms
29,712 KB
testcase_04 AC 600 ms
25,464 KB
testcase_05 AC 542 ms
24,248 KB
testcase_06 AC 498 ms
24,572 KB
testcase_07 AC 480 ms
18,108 KB
testcase_08 AC 525 ms
17,564 KB
testcase_09 AC 471 ms
14,940 KB
testcase_10 AC 439 ms
15,192 KB
testcase_11 AC 344 ms
10,600 KB
testcase_12 AC 255 ms
9,740 KB
testcase_13 AC 293 ms
9,984 KB
testcase_14 AC 472 ms
19,092 KB
testcase_15 AC 523 ms
23,288 KB
testcase_16 AC 283 ms
13,704 KB
testcase_17 AC 44 ms
5,092 KB
testcase_18 AC 187 ms
11,044 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 1 ms
4,380 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 205 ms
7,564 KB
testcase_23 AC 185 ms
7,320 KB
testcase_24 AC 174 ms
8,292 KB
testcase_25 AC 174 ms
8,256 KB
testcase_26 AC 154 ms
7,704 KB
testcase_27 AC 698 ms
30,156 KB
testcase_28 AC 740 ms
30,632 KB
testcase_29 AC 729 ms
29,796 KB
testcase_30 AC 669 ms
19,004 KB
testcase_31 AC 611 ms
18,440 KB
testcase_32 AC 613 ms
17,096 KB
testcase_33 AC 498 ms
25,492 KB
testcase_34 AC 232 ms
14,020 KB
testcase_35 AC 202 ms
8,384 KB
testcase_36 AC 195 ms
7,152 KB
testcase_37 AC 108 ms
6,584 KB
testcase_38 AC 107 ms
6,604 KB
testcase_39 AC 10 ms
14,656 KB
testcase_40 AC 10 ms
14,860 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
//#pragma GCC optimize("Ofast")

#define rep(i,n) for(ll i=0;i<n;i++)
#define repl(i,l,r) for(ll i=(l);i<(r);i++)
#define per(i,n) for(ll i=(n)-1;i>=0;i--)
#define perl(i,r,l) for(ll i=r-1;i>=l;i--)
#define fi first
#define se second
#define pb push_back
#define ins insert
#define pqueue(x) priority_queue<x,vector<x>,greater<x>>
#define all(x) (x).begin(),(x).end()
#define CST(x) cout<<fixed<<setprecision(x)
#define vtpl(x,y,z) vector<tuple<x,y,z>>
#define rev(x) reverse(x);
using ll=long long;
using vl=vector<ll>;
using vvl=vector<vector<ll>>;
using pl=pair<ll,ll>;
using vpl=vector<pl>;
using vvpl=vector<vpl>;
const ll MOD=1000000007;
const ll MOD9=998244353;
const int inf=1e9+10;
const ll INF=4e18;
const ll dy[9]={1,0,-1,0,1,1,-1,-1,0};
const ll dx[9]={0,1,0,-1,1,-1,1,-1,0};
template<class T> inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template<class T> inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
int main(){
    ll n,m;cin >> n >> m;
    vvl g(n);
    rep(i,m){
        ll a,b;cin >> a >> b;a--;b--;
        g[a].emplace_back(b);
    }
    vl itr(n);//今何番目のedgeを見ているか?
    vl vis(n);//
    auto dfs=[&](auto &self,ll v) -> ll {
        vis[v]=1;
        while(itr[v]<g[v].size()){
            ll to=g[v][itr[v]];
            if(vis[to]==1){
                g[v][itr[v]]=-1;
                itr[v]++;
                vis[v]=0;
                return to;
            }
            auto f=self(self,to);
            if(f>=0){
                g[v][itr[v]]=-1;
                if(f!=v){
                    vis[v]=0;
                    itr[v]++;
                    return f;
                }
            }
            itr[v]++;
        }
        vis[v]=0;
        return -1;
    };
    rep(i,n)dfs(dfs,i);
    vpl ans;
    rep(i,n){
        for(auto p:g[i]){
            if(p==-1)continue;
            ans.emplace_back(i,p);
        }
    }
    cout << n <<" " << ans.size() << endl;
    for(auto p:ans)cout << p.first+1 <<" " << p.second+1 << endl;

}


0