結果

問題 No.583 鉄道同好会
ユーザー tokusakuraitokusakurai
提出日時 2020-12-09 13:04:35
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 34 ms / 2,000 ms
コード長 4,607 bytes
コンパイル時間 2,942 ms
コンパイル使用メモリ 228,808 KB
実行使用メモリ 8,292 KB
最終ジャッジ日時 2023-10-19 04:50:14
合計ジャッジ時間 3,864 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 8 ms
4,384 KB
testcase_12 AC 10 ms
4,892 KB
testcase_13 AC 10 ms
4,840 KB
testcase_14 AC 10 ms
4,896 KB
testcase_15 AC 13 ms
4,896 KB
testcase_16 AC 23 ms
6,440 KB
testcase_17 AC 26 ms
6,460 KB
testcase_18 AC 34 ms
8,292 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < n; i++)
#define rep2(i, x, n) for(int i = x; i <= n; i++)
#define rep3(i, x, n) for(int i = x; i >= n; i--)
#define each(e, v) for(auto &e: v)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sz(x) (int)x.size()
using ll = long long;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
const int MOD = 1000000007;
//const int MOD = 998244353;
const int inf = (1<<30)-1;
const ll INF = (1LL<<60)-1;
template<typename T> bool chmax(T &x, const T &y) {return (x < y)? (x = y, true) : false;};
template<typename T> bool chmin(T &x, const T &y) {return (x > y)? (x = y, true) : false;};

struct io_setup{
    io_setup(){
        ios_base::sync_with_stdio(false);
        cin.tie(NULL);
        cout << fixed << setprecision(15);
    }
} io_setup;

struct Union_Find_Tree{
    vector<int> data;
    const int n;
    
    Union_Find_Tree(int n) : n(n){
        data.assign(n, -1);
    }
    
    int root(int x){
        if(data[x] < 0) return x;
        return data[x] = root(data[x]);
    }

    int operator [] (int i) {return root(i);}
    
    bool unite(int x, int y){
        x = root(x), y = root(y);
        if(x == y) return false;
        if(data[x] > data[y]) swap(x, y);
        data[x] += data[y], data[y] = x;
        return true;
    }
    
    int size(int x) {return -data[root(x)];}
    
    bool same(int x, int y) {return root(x) == root(y);}
    
    void clear() {fill(all(data), -1);}
};

struct Eulerian_Trail{
    struct edge{
        int to, id;
        edge(int to, int id) : to(to), id(id) {}
    };

    vector<vector<edge>> es;
    vector<pii> list;
    vector<bool> used_e, used_v;
    vector<int> deg;
    int m;
    const int n;

    Eulerian_Trail(int n) : n(n){
        es.resize(n), used_v.resize(n), deg.resize(n);
        m = 0;
    }

    void add_edge(int from, int to, bool directed = false){
        list.eb(from, to);
        es[from].eb(to, m);
        if(directed) deg[from]++, deg[to]--;
        else es[to].eb(from, m), deg[from]++, deg[to]++;
        m++;
    }

    pii get_edge(int id) {return list[id];}

    vector<int> trace(int s){
        stack<edge> st;
        vector<int> ret;
        st.emplace(s, -1);
        while(!st.empty()){
            int now = st.top().to;
            used_v[now] = true;
            if(es[now].empty()){
                ret.eb(st.top().id);
                st.pop();
            }
            else{
                auto e = es[now].back();
                es[now].pop_back();
                if(used_e[e.id]) continue;
                used_e[e.id] = true, st.emplace(e);
            }
        }
        ret.pop_back();
        reverse(all(ret));
        return ret;
    }

    vector<vector<int>> eulerian_trail(bool directed = false){
        vector<vector<int>> ret;
        if(directed){
            each(e, deg) if(e != 0) return {};
        }
        else{
            each(e, deg) if(e&1) return {};
        }
        used_e.assign(m, false);
        rep(i, n){
            if(es[i].empty() || used_v[i]) continue;
            ret.eb(trace(i));
        }
        return ret;
    }

    vector<vector<int>> semi_eulerian_trail(bool directed = false){
        Union_Find_Tree uf(n);
        each(e, list) uf.unite(e.first, e.second);
        vector<vector<int>> group(n);
        rep(i, n){
            group[uf[i]].eb(i);
        }
        vector<vector<int>> ret;
        used_e.assign(m, false);
        each(vs, group){
            if(vs.empty()) continue;
            int s = -1, t = -1;
            if(directed){
                each(u, vs){
                    if(abs(deg[u]) > 1) return {};
                    else if(deg[u] == 1){
                        if(s != -1) return {};
                        s = u;
                    }
                }
            }
            else{
                each(u, vs){
                    if(deg[u]&1){
                        if(s == -1) s = u;
                        else if(t == -1) t = u;
                        else return {};
                    }
                }
            }
            ret.eb(trace(s == -1? vs.front() : s));
            if(ret.back().empty()) ret.pop_back();
        }
        return ret;
    }
};

int main(){
    int V, E;
    cin >> V >> E;

    Eulerian_Trail G(V);
    while(E--){
        int u, v; cin >> u >> v;
        G.add_edge(u, v);
    }

    cout << (G.semi_eulerian_trail().size() == 1? "YES" : "NO") << '\n';
}
0