Run ID 作者 问题 语言 测评结果 分数 时间 内存 代码长度 提交时间
34324 23级逯一鸣 【S】T2 C++ 通过 100 137 MS 14636 KB 1502 2024-11-05 21:36:37

Tests(10/10):


#include <functional> #include <iostream> #include <map> #include <numeric> #include <set> #include <vector> using namespace std; using i64 = long long; map<int, vector<int>> G; map<int, bool> vis; inline void connect(int u, int v) { G[u].emplace_back(v); G[v].emplace_back(u); } void dfs(int u) { if (vis[u]) return; vis[u] = true; for (int v : G[u]) dfs(v); } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int n; cin >> n; vector<int> arr(n), target(n); for (int& x : arr) cin >> x; for (int& x : target) cin >> x; if (arr == target) { cout << "0\n"; return 0; } arr.emplace_back(accumulate(arr.begin(), arr.end(), 0, bit_xor<int>())); target.emplace_back(accumulate(target.begin(), target.end(), 0, bit_xor<int>())); if (multiset<int>(arr.begin(), arr.end()) != multiset<int>(target.begin(), target.end())) { cout << "-1\n"; return 0; } int edge = 0; for (int i = 0; i < n; ++i) { if (arr[i] != target[i]) { connect(arr[i], target[i]); ++edge; } } int comp = 0; G[arr.back()]; G[target.back()]; for (const auto& [u, _] : G) { if (!vis[u]) { dfs(u); ++comp; } } cout << edge + comp - 1 << '\n'; return 0; }


测评信息: