QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#219122#6396. Puzzle: KusabizyoobnWA 16ms8360kbC++204.6kb2023-10-19 01:59:132023-10-19 01:59:13

Judging History

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

  • [2023-10-19 01:59:13]
  • 评测
  • 测评结果:WA
  • 用时:16ms
  • 内存:8360kb
  • [2023-10-19 01:59:13]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

using i64 = long long;

void solve() {
    int n;
    cin >> n;
    vector<int> a(n, -1);
    vector g(n, vector<int>());
    for (int i = 1; i < n; ++i) {
        int u, v;
        string s;
        cin >> u >> v >> s;
        --u, --v;
        g[v].push_back(u);
        if (s == "Tong") {
            a[u] = 0;
        } else if (s == "Duan") {
            a[u] = 1;
        } else if (s == "Chang") {
            a[u] = 2; 
        }
    }

    bool ok = true;
    vector<int> dep(n);
    vector<array<int, 2>> ans;

    auto dfs = [&] (auto dfs, int u) -> array<int, 2> {
        if (!ok) {
            cout << "NO1" << '\n'; exit(0);
            return {-1, -1};
        }
        vector f(3, vector<int>());
        if (a[u] != -1) {
            f[a[u]].push_back(u);
        }
        for (auto &v : g[u]) {
            dep[v] = dep[u] + 1;
            auto [x, y] = dfs(dfs, v);
            if (!ok) {
            cout << "NO2" << '\n'; exit(0);

                return {-1, -1};
            }
            if (x == -1) {
               continue; 
            }
            f[x].push_back(y);
        }
        bool have = false;
        sort(f[0].begin(), f[0].end(), [&] (auto &x, auto &y) {
            return dep[x] < dep[y];
        });
        array<int, 2> res{-1, -1};
        for (int i = 0; i < f[0].size(); ++i) {
            if (i + 1 >= f[0].size()) {
                if (!have) {
                    have = true;
                    res = {0, f[0][i]};
                } else {
            cout << "NO3" << '\n'; exit(0);
                    ok = false;
                    return {-1, -1};
                }
            } else {
                if (dep[f[0][i]] == dep[f[0][i + 1]]) {
                    ans.push_back({f[0][i], f[0][i + 1]});
                    ++i;
                } else {
                    if (!have) {
                        have = true;
                        res = {0, f[0][i]};
                    } else {
            cout << "NO4" << '\n'; exit(0);
                        ok = false;
                        return {-1, -1};
                    }
                }
            }
        }

        if (abs((int)f[1].size() - (int)f[2].size()) >= 2) {
            cout << "NO5" << '\n'; exit(0);
            ok = false;
            return {-1, -1};
        }

        if (f[1].size() >= f[2].size()) {
            sort(f[1].begin(), f[1].end(), [&] (auto &x, auto &y) {
                return dep[x] > dep[y];
            });
            sort(f[2].begin(), f[2].end(), [&] (auto &x, auto &y) {
                return dep[x] > dep[y];
            });
        } else {
            sort(f[1].begin(), f[1].end(), [&] (auto &x, auto &y) {
                return dep[x] < dep[y];
            });
            sort(f[2].begin(), f[2].end(), [&] (auto &x, auto &y) {
                return dep[x] < dep[y];
            });
        }

        int l = 0, r = 0;
        while (l < f[1].size() && r < f[2].size()) {
            int x = f[1][l], y = f[2][r];
            if (dep[x] < dep[y]) {
                ans.push_back({x, y});
            } else {
                if (!have) {
                    have = true;
                    if (f[1].size() > f[2].size()) {
                        res = {1, x};
                        --r;
                    } else if (f[1].size() < f[2].size()) {
                        res = {2, y};
                        --l;
                    } else {
            cout << "NO6" << '\n'; exit(0);
                        ok = false;
                        return {-1, -1};
                    }
                } else {
            cout << "NO7" << '\n'; exit(0);
                    ok = false;
                    return {-1, -1};
                }
            }
            ++l, ++r;
        }
        if (!have) {
            if (f[1].size() > f[2].size()) {
                res = {1, f[1][l]};
            } else if (f[1].size() < f[2].size()) {
                res = {2, f[2][r]};
            }
        } else if (f[1].size() != f[2].size()) {
            ok = false;
            return {-1, -1};
        }
        
        return res;
    };

    auto [x, y] = dfs(dfs, 0);
    
    if (x != -1 || !ok) {
        cout << "NO" << '\n';
        return;
    }
    
    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;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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

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

input:

2
2 1 Tong

output:

NO

result:

ok Correct.

Test #4:

score: -100
Wrong Answer
time: 16ms
memory: 8360kb

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:

NO2

result:

wrong answer YES or NO expected in answer, but NO2 found.