QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#647493 | #3846. Link-Cut Tree | ucup-team3519# | WA | 0ms | 3560kb | C++17 | 2.0kb | 2024-10-17 14:26:42 | 2024-10-17 14:26:42 |
Judging History
answer
#include <bits/stdc++.h>
struct DSU {
std::vector<int> fa;
explicit DSU(int n) : fa(n) {
std::iota(fa.begin(), fa.end(), 0);
}
int find(int x) {
return fa[x] == x ? x : fa[x] = find(fa[x]);
}
void merge(int x, int y) {
x = find(x);
y = find(y);
fa[x] = y;
}
};
void solve() {
int n, m;
std::cin >> n >> m;
std::vector<std::pair<int, int>> edge(m);
for (auto &[x, y] : edge) {
std::cin >> x >> y;
--x, --y;
}
DSU dsu(n);
std::vector<std::vector<std::pair<int, int>>> adj(n);
auto find_path = [&](int x, int y) -> std::vector<int> {
std::vector<uint8_t> vist(n);
std::vector<int> st;
auto dfs = [&](auto &self, int node) -> bool {
vist[node] = true;
if (node == y) {
return true;
}
for (auto [to, id] : adj[node]) {
if (!vist[to]) {
st.push_back(id);
if (self(self, to)) {
return true;
}
st.pop_back();
}
}
return false;
};
assert(dfs(dfs, x));
return st;
};
for (int i = 0; i < m; ++i) {
auto [x, y] = edge[i];
if (dsu.find(x) == dsu.find(y)) {
auto ans = find_path(x, y);
ans.push_back(i);
std::sort(ans.begin(), ans.end());
for (int i : ans) {
std::cout << i + 1 << ' ';
}
std::cout << '\n';
return;
}
dsu.merge(x, y);
adj[x].emplace_back(y, i);
adj[y].emplace_back(x, i);
}
std::cout << "-1\n";
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int t;
std::cin >> t;
while (t--) {
solve();
}
}
详细
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 3560kb
input:
2 6 8 1 2 2 3 5 6 3 4 2 5 5 4 5 1 4 2 4 2 1 2 4 3
output:
2 4 5 6 -1
result:
wrong answer 1st lines differ - expected: '2 4 5 6', found: '2 4 5 6 '