結果

問題 No.2674 k-Walk on Bipartite
ユーザー 👑 eoeoeoeo
提出日時 2024-03-12 00:10:31
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 221 ms / 2,000 ms
コード長 10,482 bytes
コンパイル時間 2,668 ms
コンパイル使用メモリ 223,432 KB
実行使用メモリ 26,360 KB
最終ジャッジ日時 2024-03-13 19:35:27
合計ジャッジ時間 6,529 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 2 ms
6,676 KB
testcase_04 AC 2 ms
6,676 KB
testcase_05 AC 2 ms
6,676 KB
testcase_06 AC 3 ms
6,676 KB
testcase_07 AC 103 ms
20,132 KB
testcase_08 AC 140 ms
19,856 KB
testcase_09 AC 73 ms
19,784 KB
testcase_10 AC 196 ms
23,152 KB
testcase_11 AC 122 ms
17,792 KB
testcase_12 AC 192 ms
22,592 KB
testcase_13 AC 70 ms
17,836 KB
testcase_14 AC 21 ms
12,544 KB
testcase_15 AC 212 ms
24,772 KB
testcase_16 AC 133 ms
21,792 KB
testcase_17 AC 165 ms
19,072 KB
testcase_18 AC 43 ms
13,440 KB
testcase_19 AC 89 ms
19,684 KB
testcase_20 AC 78 ms
19,412 KB
testcase_21 AC 167 ms
20,352 KB
testcase_22 AC 221 ms
26,360 KB
testcase_23 AC 2 ms
6,676 KB
testcase_24 AC 2 ms
6,676 KB
testcase_25 AC 2 ms
6,676 KB
testcase_26 AC 2 ms
6,676 KB
testcase_27 AC 2 ms
6,676 KB
testcase_28 AC 2 ms
6,676 KB
testcase_29 AC 2 ms
6,676 KB
testcase_30 AC 2 ms
6,676 KB
testcase_31 AC 2 ms
6,676 KB
testcase_32 AC 2 ms
6,676 KB
testcase_33 AC 2 ms
6,676 KB
testcase_34 AC 2 ms
6,676 KB
testcase_35 AC 1 ms
6,676 KB
testcase_36 AC 2 ms
6,676 KB
testcase_37 AC 2 ms
6,676 KB
testcase_38 AC 2 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#ifdef MY_DEBUG
#define dbg(...) debug_out(__VA_ARGS__)
#define dbgn(var) debug_out(#var, "=", var)
#else
#define dbg(...)
#define dbgn(...)
#endif
void debug_out();
template <class Head, class... Tail>
void debug_out(Head, Tail...);
template <class T>
bool chmax(T&, const T&);
template <class T>
bool chmin(T&, const T&);
template <class T>
T pwr(T, ll);
template <class T>
T squ(T x) { return x * x; }
ll fact(ll);
ll comb(ll, ll);
ll ctoll(char);
char lltoc(ll);
bool flg(ll b, ll i) { return ((b) >> (i)) & 1LL; }  // flg(0b010, 1LL) -> true
const ll LINF = (ll)1e18 + 7;
const double EPS = 1e-9;
const int MAX_DUBUG_SIZE = 10;

// 重み付きUnionFindライブラリで重みをxorにしたやつ
struct weighted_union_find {
    vector<ll> par, rank, siz;  // par: parent, rank: depth, siz: size of group
    vector<bool> diff_weight;
    ll group_num;  // 連結成分の個数

    weighted_union_find(ll n) : par(n, -1), rank(n, 0), siz(n, 1), diff_weight(n, false), group_num(n) {}

    ll root(ll x) {
        if (par[x] == -1) {
            return x;
        } else {
            ll root_id = root(par[x]);
            diff_weight[x] = diff_weight[x] ^ diff_weight[par[x]];
            par[x] = root_id;  // 経路圧縮、帰りがけに根に直接つなぐ
            return par[x];
        }
    }

    bool is_same(ll x, ll y) { return root(x) == root(y); }

    bool weight(ll x) {
        root(x);
        return diff_weight[x];
    }

    // unite by rank, x の根の下に y の根をつなぐ
    // w = weight(y) ^ weight(x) を満たすように
    // xとyの値が同じ <-> w = false
    bool unite(ll x, ll y, bool w) {
        // x と y の値が等しかったら y の根を x の根にポテンシャル0ではってyはそのまま、x と y の値が違うときは y の根を
        // x の根にポテンシャル1ではって y を反転させる
        w = w ^ weight(x);
        w = w ^ weight(y);

        ll rx = root(x), ry = root(y);
        if (rx == ry) {
            return false;
        }

        if (rank[rx] < rank[ry]) {
            swap(rx, ry);
        }

        if (rank[rx] == rank[ry]) {
            rank[rx]++;
        }

        par[ry] = rx;
        siz[rx] += siz[ry];
        siz[ry] = 0;
        diff_weight[ry] = w;
        group_num--;
        return true;
    }

    ll size(ll x) { return siz[root(x)]; }

    bool diff(ll x, ll y) { return weight(y) ^ weight(x); }
};

struct edge {
    ll to;
    ll w;
    // edge v;
    // v.to, v.w でメンバ変数を呼び出す
    edge(ll to, ll w) : to(to), w(w) {}
    // graph g(n); で n 頂点のグラフを宣言
    // graph[from].push_back(edge(to, w)) の形で使う
};

using graph = vector<vector<edge>>;

void solve([[maybe_unused]] int test) {
    ll n, m;
    cin >> n >> m;
    ll s, t, k;
    cin >> s >> t >> k;
    s--, t--;
    graph g(n);  // 0-index, 無向グラフに直し忘れない。
    vector<ll> a(m), b(m);
    for (ll i = 0; i < m; i++) {
        cin >> a[i] >> b[i];
        a[i]--, b[i]--;
        g[a[i]].push_back(edge(b[i], 1));
        g[b[i]].push_back(edge(a[i], 1));
    }

    weighted_union_find wuf(n);
    for (ll i = 0; i < m; i++) {
        wuf.unite(a[i], b[i], true);
    }

    vector<ll> dist(n, LINF);
    priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> q;
    dist[s] = 0;
    q.push({0, s});
    while (q.size()) {
        auto [d, v] = q.top();
        q.pop();

        // 以下でdは使わないのでこの行はなくてもいいがあった方が速い
        if (d > dist[v]) continue;

        for (auto& x : g[v]) {
            auto [to, w] = x;
            if (chmin(dist[to], dist[v] + w)) {
                q.emplace(dist[to], to);
            }
        }
    }

    dbg(dist);

    // N = 1 のとき
    if (n == 1) {
        cout << "No" << '\n';
        return;
    }

    // N = 2 のとき
    if (n == 2) {
        if (s == t) {
            if (g[s].size() == 0) {
                if (k % 2 == 0) {
                    cout << "Unknown" << '\n';
                } else {
                    cout << "No" << '\n';
                }
            } else {
                if (k % 2 == 0) {
                    cout << "Yes" << '\n';
                } else {
                    cout << "No" << '\n';
                }
            }
        } else {
            if (g[s].size() == 0) {
                if (k % 2 == 0) {
                    cout << "No" << '\n';
                } else {
                    cout << "Unknown" << '\n';
                }
            } else {
                if (k % 2 == 0) {
                    cout << "No" << '\n';
                } else {
                    cout << "Yes" << '\n';
                }
            }
        }
        return;
    }

    // 以下 n >= 3

    // s = t のとき
    if (s == t and g[s].size() == 0) {
        if (k % 2 == 0) {
            cout << "Unknown" << '\n';
        } else {
            cout << "No" << '\n';
        }
        return;
    }

    // 非連結ならUnknown
    if (!wuf.is_same(s, t)) {
        cout << "Unknown" << '\n';
        return;
    }

    // 色が違ってkが偶数 or 同色でkが奇数 ならNo
    if ((wuf.diff(s, t) and k % 2 == 0) or (!wuf.diff(s, t) and k % 2 == 1)) {
        cout << "No" << '\n';
        return;
    }

    // sとtの最短経路がk以下ならYes
    if (dist[t] <= k) {
        cout << "Yes" << '\n';
        return;
    }

    // それ以外はUnknown
    cout << "Unknown" << '\n';
}

int main() {
    cin.tie(nullptr), ios::sync_with_stdio(false);
    cout << fixed << setprecision(12), cerr << fixed << setprecision(12);

    int testcase_size = 1;
    // cin >> testcase_size;

    for (int _t = 0; _t < testcase_size; _t++) {
        solve(_t);
    }
}

/*



























-----------------------------------MY_FUNCTIONS------------------------------------ */
template <class First, class Second>
ostream& operator<<(ostream& os, const pair<First, Second>& pp) {
    return os << "{" << pp.first << "," << pp.second << "}";
}

template <class T>
ostream& operator<<(ostream& os, const vector<T>& V) {
    if (V.empty()) return os << "[]";
    os << "[";
    for (ll i = 0; i < (ll)V.size(); i++) {
        os << V[i] << (i == int(V.size() - 1) ? "]" : ",");
    }
    return os;
}

template <class T>
ostream& operator<<(ostream& os, const vector<vector<T>>& VV) {
    if (VV.empty()) return os << "[[]]";
    os << "[\n";
    for (auto&& V : VV) {
        os << V << "\n";
    }
    os << "]";
    return os;
}

template <class T>
ostream& operator<<(ostream& os, const vector<vector<vector<T>>>& VVV) {
    if (VVV.empty()) return os << "[[[]]]";
    os << "["
       << "\n";
    int cnt = 0;
    for (auto&& VV : VVV) {
        os << cnt++ << VV << "\n\n";
    }
    os << "]";
    return os;
}

template <class T>
ostream& operator<<(ostream& os, const set<T>& SS) {
    if (SS.empty()) return os << "[]";
    os << "[";
    auto ii = SS.begin();
    for (; ii != SS.end(); ii++) os << *ii << (ii == prev(SS.end()) ? "]" : ",");
    return os;
}

template <class Key, class Tp>
ostream& operator<<(ostream& os, const map<Key, Tp>& MM) {
    if (MM.empty()) return os << "[{:}]";
    os << "[";
    auto ii = MM.begin();
    for (; ii != MM.end(); ii++)
        os << "{" << ii->first << ":" << ii->second << "}" << (ii == prev(MM.end()) ? "]" : ",");
    return os;
}

void debug_out() { cerr << endl; }

void debug_out_vl(vector<ll> V) {
    const int MAX_SIZE = min((int)V.size(), MAX_DUBUG_SIZE);

    cerr << "\033[33m";
    if (V.empty()) {
        cerr << "[]" << endl;
        return;
    }
    cerr << "[";
    for (int i = 0; i < MAX_SIZE; i++) {
        if (V[i] == LINF)
            cerr << "INF";
        else
            cerr << V[i];

        if (i == (int)V.size() - 1)
            cerr << "]\n";
        else if (i == MAX_DUBUG_SIZE - 1)
            cerr << ",...\n";
        else
            cerr << ",";
    }
    return;
}

void debug_out_vvl(vector<vector<ll>> VV) {
    cerr << "\033[33m";
    if (VV.empty()) {
        cerr << "[[]]" << endl;
        return;
    }
    cerr << "[\n";

    int MAX_ROW = min((int)VV.size(), MAX_DUBUG_SIZE);
    for (int i = 0; i < MAX_ROW; i++) {
        const int MAX_COLUMN = min((int)VV[i].size(), MAX_DUBUG_SIZE);
        if (VV[i].empty()) {
            cerr << "[]" << endl;
            continue;
        }
        cerr << "[";
        for (int j = 0; j < MAX_COLUMN; j++) {
            if (VV[i][j] == LINF)
                cerr << "INF";
            else
                cerr << VV[i][j];

            if (j == (int)VV[i].size() - 1)
                cerr << "]\n";
            else if (j == MAX_DUBUG_SIZE - 1)
                cerr << ",...\n";
            else
                cerr << ",";
        }

        if (i != (int)VV.size() - 1 and i == MAX_DUBUG_SIZE - 1) {
            cerr << ":\n:\033[m\n";
            return;
        }
    }

    cerr << "]\033[m\n";
    return;
}

template <class Head, class... Tail>
void debug_out(Head H, Tail... T) {
    if constexpr (std::is_same_v<Head, vector<ll>>) {
        debug_out_vl(H);
    } else if constexpr (std::is_same_v<Head, vector<vector<ll>>>) {
        debug_out_vvl(H);
    } else {
        cerr << "\033[33m" << H << "\033[m ";
    }

    debug_out(T...);
}

template <class T>
bool chmax(T& a, const T& b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

template <class T>
bool chmin(T& a, const T& b) {
    if (b < a) {
        a = b;
        return true;
    }
    return false;
}

template <class T>
T pwr(T x, ll n) {
    T res = 1;
    for (int i = 0; i < n; i++) {
        res *= x;
    }
    return res;
}

ll fact(ll n) {
    ll res = 1;
    for (ll i = 1; i <= n; i++) res *= i;
    return res;
}

ll comb(ll n, ll r) {  // comb(60, 30)までオーバーフローなし
    ll res = 1;
    for (int i = 1; i <= r; i++) {
        res *= n--;
        res /= i;
    }
    return res;
}

ll ctoll(char c) {
    if (c < '0' or '9' < c) {
        cerr << "\n\033[33m ctoll に '0'~'9' 以外の文字が入りました。\033[m\n"
             << endl;
    }
    return ll(c - '0');
}

char lltoc(ll n) {
    if (n < 0 or 9 < n) {
        cerr << "\n\033[33m lltoc に 0 ~ 9 以外の数字が入りました。\033[m\n"
             << endl;
    }
    return char(n + '0');
}
0