結果

問題 No.583 鉄道同好会
ユーザー kyunakyuna
提出日時 2019-08-12 05:43:10
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,785 bytes
コンパイル時間 929 ms
コンパイル使用メモリ 88,448 KB
実行使用メモリ 14,620 KB
最終ジャッジ日時 2023-10-12 17:21:18
合計ジャッジ時間 2,177 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 1 ms
4,348 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 1 ms
4,352 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 17 ms
6,388 KB
testcase_12 AC 24 ms
7,824 KB
testcase_13 AC 24 ms
8,132 KB
testcase_14 AC 23 ms
7,836 KB
testcase_15 AC 29 ms
8,352 KB
testcase_16 AC 55 ms
13,660 KB
testcase_17 AC 65 ms
14,620 KB
testcase_18 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <iostream>
#include <vector>
#include <stack>
using namespace std;

template <typename T>
struct Edge { int src, dst; T cost;
    Edge(int dst, T cost) : src(-1), dst(dst), cost(cost) { }
    Edge(int src, int dst, T cost) : src(src), dst(dst), cost(cost) { }
};
template <typename T> using Edges = vector<Edge<T>>;

template<typename T>
vector<Edge<T>> eulerian_path(Edges<T> es, int s, bool directed) {
    int V = 0;
    for (auto &e: es) V = max(V, max(e.src, e.dst) + 1);
    vector<vector<pair<Edge<T>, int>>> g(V);
    for (auto &e: es) {
        int sz_dst = g[e.dst].size();
        g[e.src].emplace_back(e, sz_dst);
        if (directed) continue;
        int sz_src = g[e.src].size() - 1;
        swap(e.src, e.dst);
        g[e.src].emplace_back(e, sz_src);
    }
    vector<Edge<T>> ord;
    stack<pair<int, Edge<T>>> st;
    st.emplace(s, Edge<T>(-1, -1, 0));
    while (!st.empty()) {
        int idx = st.top().first;
        if (g[idx].empty()) {
            if (ord.empty() || ord.back().src == st.top().second.dst) {
                ord.emplace_back(st.top().second);
            }
            st.pop();
        } else {
            auto e = g[idx].back(); g[idx].pop_back();
            if (e.second == -1) continue;
            if (!directed) g[e.first.dst][e.second].second = -1;
            st.emplace(e.first.dst, e.first);
        }
    }
    reverse(begin(ord), end(ord));
    if (ord.size() != es.size()) return {};
    return ord;
}

int main() {
    int n, m; cin >> n >> m;
    Edges<int> es;
    while (m--) {
        int s, t; cin >> s >> t;
        es.emplace_back(s, t, 0);
    }
    if (eulerian_path(es, 0, false).size()) {
        cout << "YES" << endl;
    } else {
        cout << "NO" << endl;
    }
    return 0;
}
0