QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#357293#6396. Puzzle: KusabiZhaobyWA 16ms33004kbC++175.3kb2024-03-18 19:58:392024-03-18 19:58:40

Judging History

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

  • [2024-03-18 19:58:40]
  • 评测
  • 测评结果:WA
  • 用时:16ms
  • 内存:33004kb
  • [2024-03-18 19:58:39]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

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

struct zby {
    int dep, pos;
    zby(int _dep = 0, int _pos = -1) {
        dep = _dep;
        pos = _pos;
    }
    bool operator<(zby x) const {
        return dep < x.dep;
    }
};

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;
    int cnt = 0;
    auto dfs = [&](auto self, int u) -> void {
        vector<vector<int>> c(n);
        multiset<zby> 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[redep[v]].push_back(repos[v]);
            } else if (recol[v] == 1) {
                a.emplace(redep[v], repos[v]);
            } else if (recol[v] == 2) {
                b.emplace(redep[v], repos[v]);
            }
        }
        // if (n == 100000) {
        //     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";
        // }
        cnt++;
        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()) {
                if (n == 100000) {
                    cout << cnt << '\n';
                    // cout << (*i).pos << ' ' << (*i).dep << '\n';
                    // cout << (*it).pos << ' ' << (*it).dep << '\n';
                }
                cout << "NO\n";
                exit(0);
            } else {
                it = prev(it);
                ans.emplace_back((*it).pos, (*i).pos);
                b.erase(it);
            }
        }
        // if (n == 100000) {
        //     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 (int i = 0; i < n; i++) {
            if (int(c[i].size()) & 1) {
                if (recol[u] == -1) {
                    recol[u] = 0;
                    redep[u] = i;
                    repos[u] = c[i][0];
                    for (int j = 1; j < int(c[i].size()); j += 2) {
                        ans.emplace_back(c[i][j], c[i][j + 1]);
                    }
                } else {
                    if (n == 100000) {
                        cout << "1NO\n";
                        exit(0);   
                    }
                    cout << "NO\n";
                    exit(0);
                }
            } else {
                for (int j = 0; j < int(c[i].size()); j += 2) {
                    ans.emplace_back(c[i][j], c[i][j + 1]);
                }
            }
        }

        if (a.size() > 1 || b.size() > 1) {
            if (n == 100000) {
                cout << "2NO\n";
                exit(0);   
            }
            cout << "NO\n";
            exit(0);
        } else if (a.size() == 1) {
            if (recol[u] != -1) {
                if (n == 100000) {
                    cout << "3NO\n";
                    exit(0);   
                }
                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 (recol[u] != -1) {
                if (n == 100000) {
                    cout << "4NO\n";
                    exit(0);   
                }
                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) {
        if (n == 100000) {
            cout << "5NO\n";
            exit(0);   
        }
        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: 3572kb

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

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

input:

2
2 1 Tong

output:

NO

result:

ok Correct.

Test #4:

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

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:

18
NO

result:

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