QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#222081#6396. Puzzle: Kusabitselmegkh#WA 0ms3880kbC++174.5kb2023-10-21 15:42:472023-10-21 15:42:47

Judging History

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

  • [2023-10-21 15:42:47]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3880kb
  • [2023-10-21 15:42:47]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n;
    cin >> n;
    vector<int> p(n, -1), a(n, -1);
    vector<vector<int>> ch(n);
    for (int i = 0; i < n - 1; i++) {
        int j;
        cin >> j; j--;
        cin >> p[j]; p[j]--;
        string s;
        cin >> s;
        if (s == "Tong") {
            a[j] = 2;
        } else if (s == "Chang") {
            a[j] = 1;
        } else if (s == "Duan") {
            a[j] = 0;
        }
        ch[p[j]].push_back(j);
    }
    vector<pair<int ,int>> ans;
    auto dfs = [&](auto dfs, int x, int dep) -> array<int, 3> {
        vector<pair<int, int>> v[3];
        for (int y : ch[x]) {
            auto [t, i, d] = dfs(dfs, y, dep + 1);
            if (i != -1) {
                v[t].emplace_back(d, i);
            }
            // cout << y + 1 << ": " << t << " " << i + 1 << " " << d << "\n";
        }
        for (int i = 0; i < 3; i++) {
            sort(v[i].begin(), v[i].end());
        }
        vector<pair<int, int>> b[3];
        for (int i = 0; i < v[2].size(); i++) {
            if (i + 1 == v[2].size() || v[2][i].first != v[2][i + 1].first) {
                b[2].push_back(v[2][i]);
            } else {
                ans.emplace_back(v[2][i].second, v[2][i + 1].second);
                i++;
            }
        }
        int s0 = v[0].size();
        int s1 = v[1].size();
        if (b[2].size() > 1 || abs(s0 - s1) > 1) {
            cout << "NO";
            exit(0);
        }
        if (s0 > s1) {
            int j = s0 - 1;
            for (int i = s1 - 1; i >= 0; i--) {
                while (j >= 0 && v[0][j].first >= v[1][i].first) {
                    b[0].push_back(v[0][j--]);
                }
                if (j == -1) {
                    cout << "NO";
                    exit(0);
                }
                ans.emplace_back(v[1][i].second, v[0][j].second);
            }
            while (j >= 0) {
                b[0].push_back(v[0][j--]);
            }
        } else if (s0 < s1) {
            int j = 0;
            for (int i = 0; i < s0; i++) {
                while (j < s1 && v[1][j].first <= v[0][i].first) {
                    b[1].push_back(v[1][j++]);
                }
                if (j == s1) {
                    cout << "NO";
                    exit(0);
                }
                ans.emplace_back(v[0][i].second, v[1][j].second);
            }
            while (j < s1) {
                b[1].push_back(v[1][j++]);
            }
        } else {
            for (int i = 0; i < s0; i++) {
                if (v[0][i].first >= v[1][i].first) {
                    cout << "NO";
                    exit(0);
                }
                ans.emplace_back(v[0][i].second, v[1][i].second);
            }
        }
        if (b[0].size() > 1 || b[1].size() > 1) {
            cout << "NO";
            exit(0);
        }
        vector<int> e;
        for (int i = 0; i < 3; i++) {
            if (!b[i].empty()) {
                e.push_back(i);
            }
        }
        if (a[x] == -1) {
            if (e.size() > 1) {
                cout << "NO";
                exit(0);
            }
            if (e.empty()) {
                return {-1, -1, -1};
            }
            return {e[0], b[e[0]][0].second, b[e[0]][0].first};
        }
        if (e.empty()) {
            return {a[x], x, dep};
        }
        if (a[x] == 2 || a[x] == 1) {
            cout << "NO";
            exit(0);
        }
        if (a[x] == 0) {
            if (!b[1].empty()) {
                ans.emplace_back(x, b[1][0].second);
                if (!b[0].empty() && !b[2].empty()) {
                    cout << "NO";
                    exit(0);
                }
                if (b[0].empty() && b[2].empty()) {
                    return {-1, -1, -1};
                }
                if (!b[0].empty()) {
                    return {0, b[0][0].second, b[0][0].first};
                }
                if (!b[2].empty()) {
                    return {2, b[2][0].second, b[2][0].first};
                }
                assert(0);
            }
            cout << "NO";
            exit(0);
        }
        assert(0);
    };
    auto [i, j, k] = dfs(dfs, 0, 0);
    if (i != -1) {
        cout << "NO";
        exit(0);
    }
    cout << "YES\n";
    for (auto [a, b] : ans) {
        cout << a + 1 << " " << b + 1 << "\n";
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

8
2 1 -
3 1 -
4 2 Tong
5 2 Tong
6 3 Duan
7 3 -
8 7 Chang

output:

YES
4 5
6 8

result:

ok Correct.

Test #2:

score: -100
Wrong Answer
time: 0ms
memory: 3588kb

input:

10
2 1 Duan
3 2 Duan
4 2 -
5 4 Chang
6 2 Chang
7 1 Duan
8 6 Tong
9 6 Tong
10 3 Chang

output:

NO

result:

wrong answer Jury has answer but participant has not.