結果

問題 No.2464 To DAG
ユーザー t98slidert98slider
提出日時 2023-09-09 11:09:51
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 193 ms / 2,000 ms
コード長 1,356 bytes
コンパイル時間 4,103 ms
コンパイル使用メモリ 176,032 KB
実行使用メモリ 58,140 KB
最終ジャッジ日時 2023-09-09 11:10:09
合計ジャッジ時間 18,219 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,384 KB
testcase_02 AC 193 ms
58,140 KB
testcase_03 AC 173 ms
23,152 KB
testcase_04 AC 156 ms
18,720 KB
testcase_05 AC 149 ms
18,372 KB
testcase_06 AC 158 ms
18,164 KB
testcase_07 AC 107 ms
13,784 KB
testcase_08 AC 132 ms
13,592 KB
testcase_09 AC 118 ms
12,080 KB
testcase_10 AC 117 ms
11,452 KB
testcase_11 AC 83 ms
8,980 KB
testcase_12 AC 65 ms
8,032 KB
testcase_13 AC 82 ms
8,512 KB
testcase_14 AC 125 ms
14,312 KB
testcase_15 AC 141 ms
18,008 KB
testcase_16 AC 70 ms
11,180 KB
testcase_17 AC 11 ms
4,600 KB
testcase_18 AC 40 ms
8,852 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 2 ms
4,384 KB
testcase_21 AC 2 ms
4,384 KB
testcase_22 AC 66 ms
7,124 KB
testcase_23 AC 66 ms
6,964 KB
testcase_24 AC 65 ms
7,884 KB
testcase_25 AC 66 ms
7,964 KB
testcase_26 AC 29 ms
6,676 KB
testcase_27 AC 162 ms
22,432 KB
testcase_28 AC 190 ms
23,404 KB
testcase_29 AC 165 ms
21,608 KB
testcase_30 AC 125 ms
14,236 KB
testcase_31 AC 112 ms
12,572 KB
testcase_32 AC 110 ms
12,996 KB
testcase_33 AC 165 ms
19,068 KB
testcase_34 AC 72 ms
11,432 KB
testcase_35 AC 54 ms
7,092 KB
testcase_36 AC 55 ms
6,752 KB
testcase_37 AC 47 ms
6,720 KB
testcase_38 AC 47 ms
6,524 KB
testcase_39 AC 10 ms
11,396 KB
testcase_40 AC 10 ms
11,260 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n, m;
    cin >> n >> m;
    vector<vector<pair<int,int>>> g(n);
    for(int i = 0; i < m; i++){
        int u, v;
        cin >> u >> v;
        u--, v--;
        g[u].emplace_back(v, 0);
    }

    vector<int> idx(n);
    vector<bool> used(n), visited(n);

    auto dfs = [&](auto dfs, int v) -> int {
        if(used[v]) return -1;
        if(visited[v]) return v;
        visited[v] = true;
        for(int &j = idx[v]; j < g[v].size(); j++){
            int u, c;
            tie(u, c) = g[v][j];
            if(c == 1) continue;
            int res = dfs(dfs, u);
            if(res != -1){
                g[v][j] = make_pair(u, 1);
                if(res != v){
                    visited[v] = false;
                    return res;
                }
            }
        }
        visited[v] = false;
        used[v] = true;
        return -1;
    };

    for(int v = 0; v < n; v++) dfs(dfs, v);

    vector<pair<int,int>> edge;
    for(int v = 0; v < n; v++){
        for(auto &&pa : g[v]){
            if(pa.second) continue;
            edge.emplace_back(v, pa.first);
        }
    }
    cout << n << " " << edge.size() << '\n';
    for(auto &&pa : edge){
        cout << pa.first + 1 << ' ' << pa.second + 1 << '\n';
    }
}
0