QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#647493#3846. Link-Cut Treeucup-team3519#WA 0ms3560kbC++172.0kb2024-10-17 14:26:422024-10-17 14:26:42

Judging History

你现在查看的是最新测评结果

  • [2024-10-17 14:26:42]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3560kb
  • [2024-10-17 14:26:42]
  • 提交

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 '