QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#369423#6503. DFS Order 3bigJWA 0ms3596kbC++201.9kb2024-03-28 08:50:102024-03-28 08:50:11

Judging History

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

  • [2024-03-28 08:50:11]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3596kb
  • [2024-03-28 08:50:10]
  • 提交

answer

#include <bits/stdc++.h>

using i64 = int64_t;

namespace stdr = std::ranges;
namespace stdv = std::views;

struct DSU {
    std::vector<int> f, siz;

    DSU() {}
    DSU(int n) {
        init(n);
    }

    void init(int n) {
        f.resize(n);
        std::iota(f.begin(), f.end(), 0);
        siz.assign(n, 1);
    }

    constexpr int find(int x) {
        while (x != f[x]) {
            x = f[x] = f[f[x]];
        }
        return x;
    }
    constexpr int operator[](int i) {
        return find(i);
    }

    bool same(int x, int y) {
        return find(x) == find(y);
    }

    bool merge(int x, int y, bool t = true) {
        x = find(x);
        y = find(y);
        if (x == y) {
            return false;
        }
        if (t && siz[x] < siz[y]) {
            std::swap(x, y);
        }
        siz[x] += siz[y];
        f[y] = x;
        return true;
    }

    int size(int x) {
        return siz[find(x)];
    }
};

void solve() {
    int n;
    std::cin >> n;

    std::vector a(n, std::vector<int>(n));
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            std::cin >> a[i][j];
        }
    }

    if (n == 1) {
        return;
    }

    std::set<std::pair<int, int>> ans;
    DSU dsu(n + 1);
    while (ans.size() < n - 1) {
        for (int i = 0; i < n; i++) {
            for (int j = 1; j < n; j++) {
                if (dsu.merge(a[i][0], a[i][j])) {
                    ans.emplace(a[i][0], a[i][j]);
                    break;
                }
            }
            if (ans.size() == n - 1) {
                break;
            }
        }
    }

    for (auto [x, y] : ans) {
        std::cout << x << " " << y << "\n";
    }
}

int main() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);

    int t;
    std::cin >> t;

    while (t--) {
        solve();
    }

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 3596kb

input:

4
2
1 2
2 1
3
1 2 3
2 1 3
3 2 1
4
1 2 3 4
2 1 3 4
3 2 4 1
4 2 1 3
5
1 2 4 3 5
2 4 1 3 5
3 5 1 2 4
4 2 1 3 5
5 3 1 2 4

output:

1 2
1 2
2 3
1 2
2 3
3 4
1 2
2 4
3 5
4 3

result:

wrong answer ord[3] didn't pass the test (test case 3)