Submission #66762929


Source Code Expand

Copy
#include <bits/stdc++.h>
using namespace std;
#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;
#endif
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define fix(x) fixed << setprecision(x)
#define rep(i, start, end) for (auto i = (start); (i) < (end); (i)++)
#define repe(i, start, end) for (auto i = (start); (i) <= (end); (i)++)
#define rrep(i, start, end) for (auto i = (start); (i) >= (end); (i)--)
constexpr auto PI = 3.14159265358979;
constexpr int INF = 1e+9;
constexpr long long INFL = 1e+18;
using ll = long long;
using lld = long double;
 
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
#include <bits/stdc++.h>
using namespace std;

#if __has_include(<atcoder/all>)
#include <atcoder/all>
using namespace atcoder;
#endif

#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define fix(x) fixed << setprecision(x)
#define rep(i, start, end) for (auto i = (start); (i) < (end); (i)++)
#define repe(i, start, end) for (auto i = (start); (i) <= (end); (i)++)
#define rrep(i, start, end) for (auto i = (start); (i) >= (end); (i)--)

constexpr auto PI = 3.14159265358979;
constexpr int INF = 1e+9;
constexpr long long INFL = 1e+18;

using ll = long long;
using lld = long double;
// using mint = modint1000000007;
using mint = modint998244353;
using Pair_int = pair<int, int>;
using Pair_ll = pair<ll, ll>;
template <class T>
using Graph = vector<vector<T>>;

template <class T1, class T2>
inline auto div_floor(T1 a, T2 b)
{
    if (b < 0)
        a *= -1, b *= -1;
    if (a >= 0)
        return a / b;
    else
        return (a + 1) / b - 1;
}
template <class T1, class T2>
inline auto div_ceil(T1 a, T2 b)
{
    if (b < 0)
        a *= -1, b *= -1;
    if (a <= 0)
        return a / b;
    else
        return (a - 1) / b + 1;
}
ll pow_int(ll x, ll n)
{
    ll res = 1;
    while (n > 0)
    {
        if (n & 1)
            res *= x;
        x *= x;
        n >>= 1;
    }
    return res;
}
ll pow_mod(ll x, ll n, ll mod)
{
    ll res = 1;
    while (n > 0)
    {
        if (n & 1)
            res = res * x % mod;
        x = x * x % mod;
        n >>= 1;
    }
    return res;
}
ll gcd(ll x, ll y)
{
    if (x < y)
        swap(x, y);
    ll r;
    while (y > 0)
    {
        r = x % y;
        x = y;
        y = r;
    }
    return x;
}
ll lcm(ll x, ll y) { return ll(x / gcd(x, y)) * y; }
ll nCk(ll n, ll r)
{
    if (r < 0 || n < r)
        return 0;
    ll ans = 1;
    for (ll i = 1; i <= r; i++)
    {
        ans *= n--;
        ans /= i;
    }
    return ans;
}
int get_rand(int seed, int min, int max)
{
    static mt19937_64 mt64(seed);
    uniform_int_distribution<int> get_rand_int(min, max);
    return get_rand_int(mt64);
}
template <typename T>
inline bool chmax(T &a, T b) { return ((a < b) ? (a = b, true) : (false)); }
template <typename T>
inline bool chmin(T &a, T b) { return ((a > b) ? (a = b, true) : (false)); }
template <class T1, class T2>
inline auto mod(T1 x, T2 r) { return (x % r + r) % r; }

// ======================================== //

struct Edge
{
    int to, weight;
};

int main()
{
    int N, M;
    cin >> N >> M;
    Graph<Edge> edges(N), edges_rev(N);
    rep(i, 0, M)
    {
        int A, B, W;
        cin >> A >> B >> W;
        A--, B--;
        edges[A].push_back({B, W});
        edges_rev[B].push_back({A, W});
    }

    vector<ll> xor_sum(N, -1);
    auto bfs = [&]() -> void
    {
        queue<int> que;
        que.push(0);
        xor_sum[0] = 0;

        while (!que.empty())
        {
            int now = que.front();
            que.pop();
            for (const auto &e : edges[now])
            {
                if (xor_sum[e.to] == -1)
                {
                    xor_sum[e.to] = xor_sum[now] ^ e.weight;
                    que.push(e.to);
                }
            }
        }
    };

    bfs();
    if (xor_sum[N - 1] == -1)
    {
        cout << -1 << endl;
        return 0;
    }

    vector<bool> reachable(N, false);
    auto bfs_rev = [&]() -> void
    {
        queue<int> que;
        que.push(N - 1);
        reachable[N - 1] = true;

        while (!que.empty())
        {
            int now = que.front();
            que.pop();
            for (const auto &e : edges_rev[now])
            {
                if (!reachable[e.to])
                {
                    reachable[e.to] = true;
                    que.push(e.to);
                }
            }
        }
    };

    bfs_rev();

    vector<int> basis;
    rep(u, 0, N)
    {
        if (xor_sum[u] == -1)
            continue;

        for (const auto &e : edges[u])
        {
            int v = e.to;
            int w = e.weight;
            if (xor_sum[v] != -1 && reachable[v])
            {
                int cycle_xor = xor_sum[u] ^ w ^ xor_sum[v];
                for (const auto &b : basis)
                {
                    chmin(cycle_xor, cycle_xor ^ b);
                }

                if (cycle_xor > 0)
                {
                    basis.push_back(cycle_xor);
                }
            }
        }
    }

    int ans = xor_sum[N - 1];
    for (const auto &b : basis)
    {
        chmin(ans, ans ^ b);
    }

    cout << ans << endl;

    return 0;
}

Submission Info

Submission Time
Task D - XOR Shortest Walk
User Yuulis
Language C++ 23 (gcc 12.2)
Score 0
Code Size 4594 Byte
Status WA
Exec Time 2 ms
Memory 3820 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 400
Status
AC × 3
AC × 31
WA × 2
Set Name Test Cases
Sample sample_01.txt, sample_02.txt, sample_03.txt
All hand_01.txt, hand_02.txt, hand_03.txt, hand_04.txt, hand_05.txt, hand_06.txt, hand_07.txt, hand_08.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt, random_20.txt, random_21.txt, random_22.txt, sample_01.txt, sample_02.txt, sample_03.txt
Case Name Status Exec Time Memory
hand_01.txt AC 1 ms 3624 KiB
hand_02.txt AC 1 ms 3560 KiB
hand_03.txt AC 1 ms 3616 KiB
hand_04.txt AC 1 ms 3516 KiB
hand_05.txt AC 1 ms 3616 KiB
hand_06.txt AC 1 ms 3552 KiB
hand_07.txt WA 1 ms 3500 KiB
hand_08.txt WA 1 ms 3544 KiB
random_01.txt AC 1 ms 3540 KiB
random_02.txt AC 1 ms 3584 KiB
random_03.txt AC 1 ms 3520 KiB
random_04.txt AC 1 ms 3660 KiB
random_05.txt AC 1 ms 3520 KiB
random_06.txt AC 1 ms 3448 KiB
random_07.txt AC 1 ms 3708 KiB
random_08.txt AC 1 ms 3772 KiB
random_09.txt AC 1 ms 3620 KiB
random_10.txt AC 2 ms 3580 KiB
random_11.txt AC 1 ms 3516 KiB
random_12.txt AC 1 ms 3652 KiB
random_13.txt AC 1 ms 3540 KiB
random_14.txt AC 1 ms 3652 KiB
random_15.txt AC 1 ms 3564 KiB
random_16.txt AC 1 ms 3584 KiB
random_17.txt AC 1 ms 3604 KiB
random_18.txt AC 1 ms 3604 KiB
random_19.txt AC 1 ms 3820 KiB
random_20.txt AC 1 ms 3600 KiB
random_21.txt AC 1 ms 3660 KiB
random_22.txt AC 1 ms 3508 KiB
sample_01.txt AC 1 ms 3620 KiB
sample_02.txt AC 1 ms 3520 KiB
sample_03.txt AC 1 ms 3668 KiB


2025-06-16 (Mon)
13:10:34 +09:00