QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#818423#9881. Diverge and Convergeucup-team004WA 96ms90532kbC++233.8kb2024-12-17 20:11:522024-12-17 20:11:52

Judging History

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

  • [2024-12-17 20:11:52]
  • 评测
  • 测评结果:WA
  • 用时:96ms
  • 内存:90532kb
  • [2024-12-17 20:11:52]
  • 提交

answer

#include <bits/stdc++.h>

using i64 = long long;
using u64 = unsigned long long;
using u32 = unsigned;
using u128 = unsigned __int128;

std::array<int, 2> E(int u, int v) {
    if (u > v) {
        std::swap(u, v);
    }
    return {u, v};
}

std::pair<std::vector<std::array<int, 2>>, std::vector<std::array<int, 2>>> solve(int N, std::vector<std::array<int, 2>> e1, std::vector<std::array<int, 2>> e2) {
    if (e1.empty()) {
        return {};
    }
    std::vector<std::vector<int>> T1(N), T2(N);
    for (auto [u, v] : e1) {
        T1[u].push_back(v);
        T1[v].push_back(u);
    }
    for (auto [u, v] : e2) {
        T2[u].push_back(v);
        T2[v].push_back(u);
    }
    
    bool swapped = false;
    std::vector<std::array<int, 2>> a, b;
    for (int x = 0; x < N; x++) {
        if (T1[x].size() + T2[x].size() == 2) {
            int y = T1[x][0];
            int z = T2[x][0];
            e1.erase(std::find(e1.begin(), e1.end(), E(x, y)));
            e2.erase(std::find(e2.begin(), e2.end(), E(x, z)));
            std::tie(a, b) = solve(N, e1, e2);
            a.push_back(E(x, y));
            b.push_back(E(x, z));
            break;
        } else if (T1[x].size() + T2[x].size() == 3) {
            if (T1[x].size() == 2) {
                std::swap(T1, T2);
                std::swap(e1, e2);
                swapped = true;
            }
            int w = T1[x][0];
            int y = T2[x][0];
            int z = T2[x][1];
            e1.erase(std::find(e1.begin(), e1.end(), E(x, w)));
            e2.erase(std::find(e2.begin(), e2.end(), E(x, y)));
            e2.erase(std::find(e2.begin(), e2.end(), E(x, z)));
            e2.push_back(E(y, z));
            std::tie(a, b) = solve(N, e1, e2);
            
            int k = std::find(b.begin(), b.end(), E(y, z)) - b.begin();
            auto [u, v] = a[k];
            
            std::vector<std::vector<int>> T(N);
            for (int i = 0; i < a.size(); i++) {
                if (i != k) {
                    auto [u, v] = a[i];
                    T[u].push_back(v);
                    T[v].push_back(u);
                }
            }
            
            std::vector<bool> vis(N);
            auto dfs = [&](auto &&self, int x, int p) -> void {
                vis[x] = true;
                for (auto y : T[x]) {
                    if (y == p) {
                        continue;
                    }
                    self(self, y, x);
                }
            };
            dfs(dfs, u, -1);
            
            if (vis[w] == vis[y]) {
                b[k] = E(x, z);
                a.insert(a.begin() + k + 1, E(x, w));
                b.insert(b.begin() + k + 1, E(x, y));
            } else {
                b[k] = E(x, y);
                a.insert(a.begin() + k + 1, E(x, w));
                b.insert(b.begin() + k + 1, E(x, z));
            }
            break;
        }
    }
    
    if (swapped) {
        std::swap(a, b);
    }
    return {a, b};
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    
    int N;
    std::cin >> N;
    
    std::vector<std::array<int, 2>> e1(N - 1), e2(N - 1);
    for (int i = 0; i < N - 1; i++) {
        int u, v;
        std::cin >> u >> v;
        u--;
        v--;
        e1[i] = E(u, v);
    }
    for (int i = 0; i < N - 1; i++) {
        int u, v;
        std::cin >> u >> v;
        u--;
        v--;
        e2[i] = E(u, v);
    }
    
    auto [a, b] = solve(N, e1, e2);
    
    for (int i = 0; i < N - 1; i++) {
        std::cout << std::find(e1.begin(), e1.end(), a[i]) - e1.begin() + 1 << " \n"[i == N - 2];
    }
    for (int i = 0; i < N - 1; i++) {
        std::cout << std::find(e2.begin(), e2.end(), b[i]) - e2.begin() + 1 << " \n"[i == N - 2];
    }
    
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3600kb

input:

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

output:

3 2 1
2 3 1

result:

ok Correct, N = 4

Test #2:

score: 0
Accepted
time: 0ms
memory: 3624kb

input:

2
1 2
2 1

output:

1
1

result:

ok Correct, N = 2

Test #3:

score: 0
Accepted
time: 0ms
memory: 3568kb

input:

7
1 2
1 3
2 4
2 5
3 6
3 7
1 5
1 6
1 7
5 2
6 3
7 4

output:

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

result:

ok Correct, N = 7

Test #4:

score: -100
Wrong Answer
time: 96ms
memory: 90532kb

input:

1000
780 67
364 281
934 245
121 472
460 233
574 534
91 687
91 832
413 613
815 638
519 196
992 120
150 157
198 365
643 700
279 698
623 215
578 330
869 333
874 570
879 697
897 671
962 670
108 137
779 9
988 91
919 314
696 722
431 270
810 692
769 49
254 915
737 580
229 888
379 612
19 161
787 346
280 753...

output:

74 680 71 796 138 272 779 822 40 984 903 974 912 383 948 550 744 571 471 24 140 69 769 538 486 144 2 346 715 75 168 639 552 369 970 682 305 551 216 745 967 319 63 692 191 278 459 114 410 869 705 935 921 188 30 813 794 59 878 360 476 737 234 73 341 991 129 320 422 439 784 516 151 335 253 358 402 164 ...

result:

wrong answer Wrong, N = 1000, not forming a tree