Submission #66736458
Source Code Expand
Copy
#include <iostream>#include <vector>#include <numeric>#include <algorithm>using namespace std;struct Edge {int to;long long weight;};vector<vector<Edge>> adj;vector<long long> dist;vector<long long> cycle_basis;// DFSで基準パスとサイクルを見つけるvoid find_path_and_cycles(int u) {for (const auto& edge : adj[u]) {int v = edge.to;long long w = edge.weight;
#include <iostream>
#include <vector>
#include <numeric>
#include <algorithm>
using namespace std;
struct Edge {
int to;
long long weight;
};
vector<vector<Edge>> adj;
vector<long long> dist;
vector<long long> cycle_basis;
// DFSで基準パスとサイクルを見つける
void find_path_and_cycles(int u) {
for (const auto& edge : adj[u]) {
int v = edge.to;
long long w = edge.weight;
if (dist[v] != -1) {
// vは訪問済み -> サイクルを発見
cycle_basis.push_back(dist[u] ^ w ^ dist[v]);
} else {
// vは未訪問
dist[v] = dist[u] ^ w;
find_path_and_cycles(v);
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int N, M;
cin >> N >> M;
adj.resize(N + 1);
for (int i = 0; i < M; ++i) {
int u, v;
long long w;
cin >> u >> v >> w;
adj[u].push_back({v, w});
adj[v].push_back({u, w}); // 問題文は有向グラフだが、サイクルは無向で考えて良いため両方向に追加
}
dist.assign(N + 1, -1);
dist[1] = 0;
find_path_and_cycles(1);
if (dist[N] == -1) {
cout << -1 << endl;
return 0;
}
// XOR基底を構築
vector<long long> basis;
for (long long val : cycle_basis) {
for (long long b : basis) {
val = min(val, val ^ b);
}
if (val > 0) {
basis.push_back(val);
}
}
// 基準パスのXOR和を基底で最小化
long long result = dist[N];
for (long long b : basis) {
result = min(result, result ^ b);
}
cout << result << endl;
return 0;
}
Submission Info
| Submission Time | |
|---|---|
| Task | D - XOR Shortest Walk |
| User | renbaku |
| Language | C++ 20 (gcc 12.2) |
| Score | 0 |
| Code Size | 1801 Byte |
| Status | WA |
| Exec Time | 2 ms |
| Memory | 3760 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 | 3564 KiB |
| hand_02.txt | AC | 1 ms | 3416 KiB |
| hand_03.txt | WA | 1 ms | 3628 KiB |
| hand_04.txt | AC | 1 ms | 3492 KiB |
| hand_05.txt | WA | 1 ms | 3520 KiB |
| hand_06.txt | WA | 1 ms | 3508 KiB |
| hand_07.txt | WA | 1 ms | 3500 KiB |
| hand_08.txt | WA | 1 ms | 3424 KiB |
| random_01.txt | AC | 1 ms | 3628 KiB |
| random_02.txt | AC | 1 ms | 3692 KiB |
| random_03.txt | AC | 1 ms | 3496 KiB |
| random_04.txt | AC | 1 ms | 3544 KiB |
| random_05.txt | AC | 1 ms | 3500 KiB |
| random_06.txt | AC | 1 ms | 3464 KiB |
| random_07.txt | AC | 1 ms | 3628 KiB |
| random_08.txt | AC | 1 ms | 3544 KiB |
| random_09.txt | AC | 1 ms | 3512 KiB |
| random_10.txt | AC | 1 ms | 3564 KiB |
| random_11.txt | AC | 1 ms | 3428 KiB |
| random_12.txt | AC | 1 ms | 3636 KiB |
| random_13.txt | AC | 1 ms | 3552 KiB |
| random_14.txt | AC | 1 ms | 3564 KiB |
| random_15.txt | AC | 1 ms | 3444 KiB |
| random_16.txt | AC | 1 ms | 3424 KiB |
| random_17.txt | AC | 1 ms | 3620 KiB |
| random_18.txt | AC | 1 ms | 3760 KiB |
| random_19.txt | AC | 2 ms | 3704 KiB |
| random_20.txt | AC | 1 ms | 3644 KiB |
| random_21.txt | AC | 1 ms | 3676 KiB |
| random_22.txt | AC | 1 ms | 3624 KiB |
| sample_01.txt | AC | 1 ms | 3420 KiB |
| sample_02.txt | AC | 1 ms | 3428 KiB |
| sample_03.txt | AC | 1 ms | 3532 KiB |