QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#357061#6396. Puzzle: KusabiZhaobyWA 1ms3824kbC++173.0kb2024-03-18 17:57:302024-03-18 17:57:31

Judging History

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

  • [2024-03-18 17:57:31]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3824kb
  • [2024-03-18 17:57:30]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

#define all(v) v.begin(), v.end()

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

    vector<vector<int>> g(n);
    vector<int> dep(n), col(n, -1);
    vector<string> t = {"Tong", "Chang", "Duan"};
    for (int i = 0; i < n - 1; i++) {
        int u, v;
        string s;
        cin >> u >> v >> s;
        u--, v--;

        g[v].push_back(u);
        dep[u] = dep[v] + 1;
        for (int j = 0; j < 3; j++) {
            if (s == t[j]) {
                col[u] = j;
            }
        }
    }

    vector<int> recol(n, -1), redep(n), repos(n);
    vector<pair<int, int>> ans;
    auto dfs = [&](auto self, int u) -> void {
        int c = 0, lst = -1;
        set<pair<int, int>> a, b;
        if (col[u] == 0) {
            c++;
            lst = u;
        } else if (col[u] == 1) {
            a.emplace(dep[u], u);
        } else if (col[u] == 2) {
            b.emplace(dep[u], u);
        }
        for (auto v : g[u]) {
            self(self, v);
            if (recol[v] == 0) {
                c++;
                if (lst == -1) {
                    lst = repos[v];
                } else {
                    ans.emplace_back(lst, repos[v]);
                    lst = -1;
                }
            } else if (recol[v] == 1) {
                a.emplace(redep[v], repos[v]);
            } else if (recol[v] == 2) {
                b.emplace(redep[v], repos[v]);
            }
        }
        for (auto i = a.begin(); i != a.end(); i = a.erase(i)) {
            if (b.empty()) {
                break;
            }
            auto it = b.lower_bound(*i);
            if (it == b.begin()) {
                cout << "1NO\n";
                exit(0);
            } else {
                it = prev(it);
                ans.emplace_back((*it).second, (*i).second);
                b.erase(it);
            }
        }

        if (a.size() > 1 || b.size() > 1) {
            cout << "2NO\n";
            exit(0);
        } else if (a.size() == 1) {
            if (c & 1) {
                cout << "3NO\n";
                exit(0);
            } else {
                auto [x, y] = *a.begin();
                redep[u] = x;
                repos[u] = y;
                recol[u] = 1;
            }
        } else if (b.size() == 1) {
            if (c & 1) {
                cout << "4NO\n";
                exit(0);
            } else {
                auto [x, y] = *b.begin();
                redep[u] = x;
                repos[u] = y;
                recol[u] = 2;
            }
        } else if (c & 1) {
            recol[u] = 0;
            repos[u] = u;
        }
    };
    dfs(dfs, 0);

    cout << "YES\n";
    for (auto [u, v] : ans) cout << u + 1 << ' ' << v + 1 << '\n';
}

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

    int t = 1;
    // cin >> t;

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

    return 0;
}

详细

Test #1:

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

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: 0
Accepted
time: 1ms
memory: 3528kb

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:

YES
3 10
8 9
2 6
7 5

result:

ok Correct.

Test #3:

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

input:

2
2 1 Tong

output:

YES

result:

wrong answer Only odd number of marked vertices to match.