QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#357263#6396. Puzzle: KusabiZhaobyWA 23ms33148kbC++174.9kb2024-03-18 19:46:542024-03-18 19:46:54

Judging History

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

  • [2024-03-18 19:46:54]
  • 评测
  • 测评结果:WA
  • 用时:23ms
  • 内存:33148kb
  • [2024-03-18 19:46:54]
  • 提交

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;
    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]);
            }
        }
        // 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(*i);
            if (it == b.begin()) {
                cout << "NO\n";
                exit(0);
            } else {
                it = prev(it);
                ans.emplace_back((*it).pos, (*i).pos);
                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";

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

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

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

input:

2
2 1 Tong

output:

NO

result:

ok Correct.

Test #4:

score: -100
Wrong Answer
time: 23ms
memory: 33148kb

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.