QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#369464#6503. DFS Order 3bigJWA 100ms3848kbC++201.5kb2024-03-28 10:47:562024-03-28 10:47:58

Judging History

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

  • [2024-03-28 10:47:58]
  • 评测
  • 测评结果:WA
  • 用时:100ms
  • 内存:3848kb
  • [2024-03-28 10:47:56]
  • 提交

answer

#include <bits/stdc++.h>

using i64 = int64_t;

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

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];
            a[i][j]--;
        }
    }

    if (n == 1) {
        return;
    }

    std::set<std::pair<int, int>> S;

    for (auto b : a) {
        if (b[0] > b[1]) {
            std::swap(b[0], b[1]);
        }
        S.emplace(b[0], b[1]);
    }

    if (n > 2) {
        for (auto b : a) {
            int x = b[0];
            int y = b[1];
            int z = b[2];
            if (a[y][1] != x) {
                continue;
            }
            if (a[x][2] == a[y][2]) {
                int w = (stdr::find(a[z], x) < stdr::find(a[z], y) ? x : y);
                if (w > z) {
                    std::swap(w, z);
                }
                S.emplace(w, z);
            }
            else {
                if (z > x) {
                    std::swap(z, x);
                }
                S.emplace(z, x);
            }
        }
    }
    assert(S.size() <= n - 1);

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

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

    int t;
    std::cin >> t;

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

    return 0;
}

详细

Test #1:

score: 100
Accepted
time: 1ms
memory: 3584kb

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
2 4
1 2
1 3
2 4
3 5

result:

ok correct answer! (4 test cases)

Test #2:

score: -100
Wrong Answer
time: 100ms
memory: 3848kb

input:

20000
10
1 2 4 5 6 7 3 8 10 9
2 1 4 5 6 7 3 8 10 9
3 8 1 2 4 5 6 7 10 9
4 5 6 7 1 2 3 8 10 9
5 4 6 7 1 2 3 8 10 9
6 7 4 5 1 2 3 8 10 9
7 6 4 5 1 2 3 8 10 9
8 3 1 2 4 5 6 7 10 9
9 10 1 2 4 5 6 7 3 8
10 1 2 4 5 6 7 3 8 9
10
1 4 3 8 2 9 6 5 7 10
2 8 9 6 3 4 1 5 7 10
3 8 2 9 6 4 1 5 7 10
4 1 3 8 2 9 6 5...

output:

1 2
1 3
1 4
1 10
3 8
4 5
4 6
6 7
9 10
1 4
2 8
3 4
3 8
4 5
5 7
6 9
7 10
8 9
1 9
2 4
2 8
3 10
5 6
5 7
5 9
8 10
1 6
2 4
2 10
3 8
5 7
6 7
6 9
7 8
1 5
1 9
2 10
3 6
3 7
3 10
4 7
5 7
8 9
1 10
2 6
3 8
4 8
5 10
6 10
7 9
8 9
1 10
2 3
2 9
2 10
3 7
4 8
5 7
5 8
6 7
1 4
1 9
2 3
2 4
4 7
4 8
5 10
6 7
9 10
1 7
2 3
2...

result:

wrong answer your output is not a tree (test case 3)