QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#265390#6503. DFS Order 3shiftWA 137ms3668kbC++231.8kb2023-11-25 18:11:472023-11-25 18:11:47

Judging History

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

  • [2023-11-25 18:11:47]
  • 评测
  • 测评结果:WA
  • 用时:137ms
  • 内存:3668kb
  • [2023-11-25 18:11:47]
  • 提交

answer

#include <bits/stdc++.h>

using i64 = long long;

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

    DSU(int n): f(n) {std::iota(f.begin(), f.end(), 0); }

    int find(int x) {
        if(x != f[x]) f[x] = find(f[x]);
        return f[x];
    }

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

    bool merge(int x, int y) {
        x = find(x), y = find(y);
        if(x == y) return false;
        f[x] = y;
        return true;
    }

};

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

    DSU dsu(n + 1);
    std::set<std::pair<int, int>> edges;
    std::vector<std::vector<int>> g(n, std::vector<int>(n));
    for(int i = 0; i < n; i ++ ) {
        for(int j = 0; j < n; j ++ ) {
            std::cin >> g[i][j];
        }
        int x = g[i][0], y = g[i][1];
        if(x > y) std::swap(x, y);
        edges.insert({x, y});
        dsu.merge(x, y);
    }

    std::map<std::pair<int, int>, int> mp;
    std::set<std::pair<int, int>> vis;
    for(int i = 0; i < n; i ++ ) {
        vis.clear();
        for(int j = 1; j < n; j ++ ) {
            int x = g[i][j - 1], y = g[i][j];
            int u = dsu.find(x), v = dsu.find(y);
            if(u != v && !vis.count({u, v})) {
                mp[{u, v}] = y;
                vis.insert({u, v});
            }
        }
    }

    for(auto [o, w] : mp) {
        auto [u, v] = o;
        int x = w, y = mp[{v, u}];
        if(x > y) std::swap(x, y);
        edges.insert({x, y});
    }

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

}

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

    int T = 1;
    std::cin >> T;

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

    return 0;
}

详细

Test #1:

score: 100
Accepted
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
2 4
1 2
1 3
2 4
3 5

result:

ok correct answer! (4 test cases)

Test #2:

score: -100
Wrong Answer
time: 137ms
memory: 3668kb

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:

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

result:

wrong answer Integer parameter [name=x_i] equals to 0, violates the range [1, 10] (test case 1)