Submission #66778262
Source Code Expand
Copy
// include#include <bits/stdc++.h>using namespace std;// define#define fore(x, a) for (auto &x : a)#define rep(i, n) for (int i = 0; i < (int)(n); i++)#define repp(i, m, n) for (int i = (int)(m); i < (int)(n); i++)#define all(v) v.begin(), v.end()#define rall(v) v.rbegin(), v.rend()// typedeftypedef long long ll;typedef unsigned long long ull;typedef pair<int, int> pii;typedef pair<ll, ll> pll;typedef vector<int> vi;typedef vector<ll> vll;typedef vector<string> vs;const double pi = 3.141592653589793238;
// include
#include <bits/stdc++.h>
using namespace std;
// define
#define fore(x, a) for (auto &x : a)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repp(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
// typedef
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<string> vs;
const double pi = 3.141592653589793238;
const int inf = 1073741823;
const ll infl = 1LL << 60;
const int mod = 998244353;
struct Init { Init() { ios::sync_with_stdio(0); cin.tie(0); } }init;
struct Edge {
int to;
int cost;
Edge(int t, ll c) : to(t), cost(c) {}
};
vector<vector<Edge>> adj; // 隣接リスト
vector<bool> visited;
vi xor_val; // 各ノードへのXOR値 予約語なのか?xorは使えなかった
int basis[60]; // XOR基底?っていうらしい
void insert_basis(ll mask) {
for (int i = 59; i >= 0; i--) {
if (!((mask >> i-1) & 1)) continue;
if (!basis[i]) {
basis[i] = mask;
return;
}
mask ^= basis[i]; // 基底にある要素とXORを取る
}
}
void dfs(int u, int current_xor) {
visited[u] = true;
xor_val[u] = current_xor;
for (const auto& edge : adj[u]) {
int v = edge.to;
ll w = edge.cost;
if (!visited[v]) {
dfs(v, current_xor ^ w);
} else {
insert_basis(current_xor ^ w ^ xor_val[v]);
}
}
}
int main() {
int n, m;
cin >> n >> m;
adj.resize(n+1);
visited.resize(n+1, false);
xor_val.resize(n+1, 0);
memset(basis, 0, sizeof(basis));
rep(i, n) {
int u, v, w;
cin >> u >> v >> w;
adj[u].push_back(Edge(v, w));
}
dfs(1, 0);
if (!visited[n]) {
cout << -1 << endl;
} else {
ll path_xor = xor_val[n];
for (int i = 59; i >= 0; i--) {
if (basis[i]) {
path_xor = min(path_xor, path_xor ^ basis[i]);
}
}
cout << path_xor << endl;
}
return 0;
}
Submission Info
| Submission Time | |
|---|---|
| Task | D - XOR Shortest Walk |
| User | ponzu_domo |
| Language | C++ 20 (gcc 12.2) |
| Score | 0 |
| Code Size | 2274 Byte |
| Status | WA |
| Exec Time | 1 ms |
| Memory | 3712 KiB |
Compile Error
Main.cpp: In function ‘void insert_basis(ll)’:
Main.cpp:40:25: warning: suggest parentheses around ‘-’ inside ‘>>’ [-Wparentheses]
40 | if (!((mask >> i-1) & 1)) continue;
| ~^~
Judge Result
| Set Name | Sample | All | ||||||
|---|---|---|---|---|---|---|---|---|
| Score / Max Score | 0 / 0 | 0 / 400 | ||||||
| Status |
|
|
| 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 | 3524 KiB |
| hand_02.txt | AC | 1 ms | 3428 KiB |
| hand_03.txt | AC | 1 ms | 3524 KiB |
| hand_04.txt | AC | 1 ms | 3420 KiB |
| hand_05.txt | AC | 1 ms | 3404 KiB |
| hand_06.txt | WA | 1 ms | 3528 KiB |
| hand_07.txt | AC | 1 ms | 3584 KiB |
| hand_08.txt | AC | 1 ms | 3528 KiB |
| random_01.txt | AC | 1 ms | 3528 KiB |
| random_02.txt | AC | 1 ms | 3528 KiB |
| random_03.txt | AC | 1 ms | 3452 KiB |
| random_04.txt | AC | 1 ms | 3500 KiB |
| random_05.txt | AC | 1 ms | 3532 KiB |
| random_06.txt | AC | 1 ms | 3524 KiB |
| random_07.txt | AC | 1 ms | 3536 KiB |
| random_08.txt | AC | 1 ms | 3712 KiB |
| random_09.txt | AC | 1 ms | 3536 KiB |
| random_10.txt | AC | 1 ms | 3528 KiB |
| random_11.txt | AC | 1 ms | 3492 KiB |
| random_12.txt | AC | 1 ms | 3540 KiB |
| random_13.txt | WA | 1 ms | 3492 KiB |
| random_14.txt | WA | 1 ms | 3460 KiB |
| random_15.txt | WA | 1 ms | 3620 KiB |
| random_16.txt | AC | 1 ms | 3656 KiB |
| random_17.txt | AC | 1 ms | 3540 KiB |
| random_18.txt | AC | 1 ms | 3540 KiB |
| random_19.txt | AC | 1 ms | 3704 KiB |
| random_20.txt | AC | 1 ms | 3588 KiB |
| random_21.txt | AC | 1 ms | 3660 KiB |
| random_22.txt | AC | 1 ms | 3496 KiB |
| sample_01.txt | AC | 1 ms | 3648 KiB |
| sample_02.txt | AC | 1 ms | 3648 KiB |
| sample_03.txt | AC | 1 ms | 3568 KiB |