QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#608568#6396. Puzzle: Kusabilqh2024WA 23ms12112kbC++174.3kb2024-10-03 23:15:432024-10-03 23:15:45

Judging History

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

  • [2024-10-03 23:15:45]
  • 评测
  • 测评结果:WA
  • 用时:23ms
  • 内存:12112kb
  • [2024-10-03 23:15:43]
  • 提交

answer

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/priority_queue.hpp>
#include <ext/rope>
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
#define int long long
using i64 = long long;
mt19937_64 rd(time(0));
template <class K, class C = less<>> using paring_heap = __gnu_pbds::priority_queue<K, C>;
template <class K> using rb_tree = tree<K, null_type, less<>, rb_tree_tag, tree_order_statistics_node_update>;
template <class T, class ... A> void debug(const T & t, const A & ... a) { cerr << "[" << t, ((cerr << ", " << a), ...), cerr << "]\n"; }
const i64 mod = [](bool n) { return n ? 998244353 : 1e9 + 7; } (1);

void QAQ() {
    int n, m, cnt = 0;
    cin >> n;

    vector adj(n + 1, vector<int>(m + 1));
    vector<int> a(n + 1), dep(n + 1);

    for (int i = 2, u, v; i <= n; i++) {
        string s;
        cin >> u >> v >> s;
        if (s[0] == '-') a[u] = 0;
        else if (s[0] == 'T') a[u] = 1;
        else if (s[0] == 'D') a[u] = 2;
        else a[u] = 3;
        cnt += !!a[u];
        adj[u].emplace_back(v), adj[v].emplace_back(u);
    }

    struct Info : multimap<array<int, 2>, int> {
        vector<array<int, 2>> res;
        Info(int op, int dep, int u) {
            if (op) insert({{op, dep}, u});
        }
        int merge(Info & dp, int op) {
            if (size() <= dp.size()) swap(dp);
            if (op) {
                for (auto & [_, v] : dp) {
                    auto & [sta, len] = _;
                    if (sta == 1) return -1;
                    if (sta == 2) {
                        if (begin() -> first[0] != 3) return -1;
                    } else if (sta == 3) {
                        if (begin() -> first[1] != 2) return -1;
                    }
                    res.emplace_back(array{begin() -> second, v});
                    erase(begin());
                }
                return 0;
            }
            for (auto & [_, v] : dp) {
                auto & [sta, len] = _;
                if (sta == 1) {
                    auto it = find(_);
                    if (it != end()) {
                        res.emplace_back(array{it -> second, v});
                        erase(it);
                    } else {
                        insert({_, v});
                    }
                } else if (sta == 2) {
                    auto it = upper_bound({3, len});
                    if (it != end()) {
                        res.emplace_back(array{it -> second, v});
                        erase(it);
                    } else {
                        insert({_, v});
                    }
                } else if (sta == 3) {
                    auto it = lower_bound({2, len});
                    if (it != begin()) {
                        it = prev(it);
                        if (it -> first[0] == 2 && it -> first[1] < len) {
                            res.emplace_back(array{it -> second, v});
                            erase(it);
                        } else {
                            insert({_, v});
                        }
                    } else {
                        insert({_, v});
                    }
                }
            }
            return 0;
        }
    };

    vector<array<int, 2>> ans;

    auto dsu_tr = [&](auto && self, int u, int fa = 0) -> Info {
        Info dp(a[u], dep[u] = dep[fa] + 1, u);
        for (auto & v : adj[u]) if (v != fa) {
            auto tmp = self(self, v, u); 
            if (tmp == Info(-1, -1, -1) || dp.merge(tmp, adj[u].size() == 1 + !!fa) == -1) {
                debug(u, fa, adj[u].size() == 1 + !!fa);
                return Info(-1, -1, -1);
            }
        }
        if (dp.size() > 1) return Info(-1, -1, -1);
        ans.insert(ans.end(), dp.res.begin(), dp.res.end());
        return move(dp);
    };

    if (dsu_tr(dsu_tr, 1) == Info(-1, -1, -1)) {
        cout << "NO\n";
        return;
    }
    if (ans.size() * 2 == cnt) {
        cout << "YES\n";
        for (auto & [u, v] : ans) {
            cout << u << " " << v << "\n";
        }
        return;
    }
    cout << "NO\n";
}

signed main() {
    cin.tie(0) -> sync_with_stdio(0);
    int t = 1;
    // cin >> t;
    for (cout << fixed << setprecision(12); t--; QAQ());
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

8
2 1 -
3 1 -
4 2 Tong
5 2 Tong
6 3 Duan
7 3 -
8 7 Chang

output:

YES
5 4
8 6

result:

ok Correct.

Test #2:

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

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
10 3
8 9
5 2
7 6

result:

ok Correct.

Test #3:

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

input:

2
2 1 Tong

output:

NO

result:

ok Correct.

Test #4:

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

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.