Submission #66745724
Source Code Expand
Copy
#include <iostream>#include <vector>#include <queue>#include <algorithm>const int INF = 1e9;struct Edge{int to;int weight;};std::vector<std::vector<Edge>> adj;std::vector<int> path_xor;std::vector<bool> visited;std::vector<int> cycle_basis;void add_to_basis(int val){for (int b : cycle_basis)
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
const int INF = 1e9;
struct Edge
{
int to;
int weight;
};
std::vector<std::vector<Edge>> adj;
std::vector<int> path_xor;
std::vector<bool> visited;
std::vector<int> cycle_basis;
void add_to_basis(int val)
{
for (int b : cycle_basis)
{
val = std::min(val, val ^ b);
}
if (val > 0)
{
cycle_basis.push_back(val);
}
}
void dfs_build_basis(int u, int current_xor_sum)
{
visited[u] = true;
path_xor[u] = current_xor_sum;
for (const auto &edge : adj[u])
{
int v = edge.to;
int w = edge.weight;
if (!visited[v])
{
dfs_build_basis(v, current_xor_sum ^ w);
}
else
{
int cycle_xor_val = current_xor_sum ^ w ^ path_xor[v];
add_to_basis(cycle_xor_val);
}
}
}
int main()
{
int N, M;
std::cin >> N >> M;
adj.resize(N + 1);
path_xor.assign(N + 1, INF); // INF で初期化
visited.assign(N + 1, false);
for (int i = 0; i < M; ++i)
{
int A, B, W;
std::cin >> A >> B >> W;
adj[A].push_back({B, W});
}
dfs_build_basis(1, 0);
if (path_xor[N] == INF)
{
std::cout << -1 << std::endl;
return 0;
}
int min_walk_xor = path_xor[N];
for (int b : cycle_basis)
{
min_walk_xor = std::min(min_walk_xor, min_walk_xor ^ b);
}
std::cout << min_walk_xor << std::endl;
return 0;
}
Submission Info
| Submission Time | |
|---|---|
| Task | D - XOR Shortest Walk |
| User | tanto330 |
| Language | C++ 20 (gcc 12.2) |
| Score | 0 |
| Code Size | 1538 Byte |
| Status | WA |
| Exec Time | 2 ms |
| Memory | 3704 KiB |
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 | 3384 KiB |
| hand_02.txt | AC | 1 ms | 3620 KiB |
| hand_03.txt | AC | 1 ms | 3448 KiB |
| hand_04.txt | AC | 1 ms | 3468 KiB |
| hand_05.txt | AC | 1 ms | 3516 KiB |
| hand_06.txt | WA | 1 ms | 3468 KiB |
| hand_07.txt | WA | 1 ms | 3456 KiB |
| hand_08.txt | WA | 1 ms | 3464 KiB |
| random_01.txt | AC | 1 ms | 3452 KiB |
| random_02.txt | AC | 2 ms | 3488 KiB |
| random_03.txt | AC | 1 ms | 3476 KiB |
| random_04.txt | AC | 1 ms | 3472 KiB |
| random_05.txt | AC | 1 ms | 3624 KiB |
| random_06.txt | AC | 1 ms | 3612 KiB |
| random_07.txt | AC | 1 ms | 3428 KiB |
| random_08.txt | AC | 2 ms | 3520 KiB |
| random_09.txt | AC | 2 ms | 3468 KiB |
| random_10.txt | AC | 2 ms | 3704 KiB |
| random_11.txt | AC | 1 ms | 3480 KiB |
| random_12.txt | AC | 2 ms | 3528 KiB |
| random_13.txt | AC | 2 ms | 3436 KiB |
| random_14.txt | AC | 2 ms | 3488 KiB |
| random_15.txt | AC | 2 ms | 3440 KiB |
| random_16.txt | AC | 1 ms | 3448 KiB |
| random_17.txt | AC | 2 ms | 3516 KiB |
| random_18.txt | AC | 2 ms | 3532 KiB |
| random_19.txt | AC | 2 ms | 3504 KiB |
| random_20.txt | AC | 2 ms | 3596 KiB |
| random_21.txt | AC | 2 ms | 3560 KiB |
| random_22.txt | AC | 2 ms | 3512 KiB |
| sample_01.txt | AC | 1 ms | 3544 KiB |
| sample_02.txt | AC | 1 ms | 3404 KiB |
| sample_03.txt | AC | 1 ms | 3452 KiB |