QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#357167#6396. Puzzle: KusabiZhaobyWA 21ms9504kbC++173.7kb2024-03-18 19:01:022024-03-18 19:01:02

Judging History

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

  • [2024-03-18 19:01:02]
  • 评测
  • 测评结果:WA
  • 用时:21ms
  • 内存:9504kb
  • [2024-03-18 19:01:02]
  • 提交

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) {
            recol[u] = 0;
            repos[u] = u;
            redep[u] = dep[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]);
            }
        }
        // cout << "a:\n";
        // for (auto [x, y] : a) cout << x << ", " << y << ' ';
        // cout << '\n';
        // cout << "b:\n";
        // for (auto [x, y] : b) cout << x << ", " << y << ' ';
        // cout << '\n';
        // cout << "---------------------\n";
        for (auto i = a.begin(); i != a.end(); i = a.erase(i)) {
            if (b.empty()) {
                break;
            }
            auto it = b.lower_bound(pair((*i).first, 0));
            if (it == b.begin()) {
                cout << "NO\n";
                exit(0);
            } else {
                it = prev(it);
                ans.emplace_back((*it).second, (*i).second);
                b.erase(it);
            }
        }
        // cout << "a:\n";
        // for (auto [x, y] : a) cout << x << ", " << y << ' ';
        // cout << '\n';
        // cout << "b:\n";
        // for (auto [x, y] : b) cout << x << ", " << y << ' ';
        // cout << '\n';
        // cout << "----------\n";

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

    if (recol[0] != -1) {
        cout << "NO\n";
    } else {
        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: 0ms
memory: 3512kb

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: 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:

YES
3 10
8 9
2 6
7 5

result:

ok Correct.

Test #3:

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

input:

2
2 1 Tong

output:

NO

result:

ok Correct.

Test #4:

score: -100
Wrong Answer
time: 21ms
memory: 9504kb

input:

100000
2 1 Duan
3 1 Duan
4 3 -
5 4 Duan
6 3 -
7 4 Duan
8 4 -
9 8 -
10 7 Duan
11 9 -
12 7 Duan
13 7 Duan
14 8 Duan
15 13 -
16 10 Duan
17 11 Duan
18 12 -
19 1 Duan
20 5 Duan
21 4 Duan
22 14 Duan
23 16 -
24 22 Duan
25 16 Duan
26 13 -
27 13 -
28 17 -
29 5 Duan
30 22 -
31 23 -
32 9 Duan
33 5 -
34 30 Duan...

output:

NO

result:

wrong answer Jury has answer but participant has not.