QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#253100#6396. Puzzle: KusabisupepapupuWA 27ms17744kbC++173.1kb2023-11-16 17:55:252023-11-16 17:55:25

Judging History

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

  • [2023-11-16 17:55:25]
  • 评测
  • 测评结果:WA
  • 用时:27ms
  • 内存:17744kb
  • [2023-11-16 17:55:25]
  • 提交

answer

#include <bits/stdc++.h>

#define x first
#define y second
#define el '\n'
#define debug(x) cout << #x << ": " << x << el
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef tuple<int, int, int> tiii;
const int N = 3e5 + 10, INF = 0x3f3f3f3f, mod = 998244353;

void inc(ll &a, ll b) {
    a += b;
    if (a >= mod) a -= mod;
}

void dec(ll &a, ll b) {
    a -= b;
    if (a < 0) a += mod;
}

ll add(ll a, ll b) {
    a += b;
    return a >= mod ? a - mod : a;
}

ll del(ll a, ll b) {
    a -= b;
    return a < 0 ? a + mod : a;
}

int p[N];
vector<int> G[N];
vector<int> chang, duan, tong;
int dep[N];
vector<pii> ans;
tiii f[N];

void quit() {
    cout << "NO\n";
    exit(0);
}

void dfs0(int u) {
    dep[u] = dep[p[u]] + 1;
    for (int v: G[u]) dfs0(v);
}

void dfs1(int u) {
    vector<tiii> t;
    set<tiii> c, d;
    if (get<1>(f[u])) {
        if (get<1>(f[u]) == 1) c.insert(f[u]);
        else if (get<1>(f[u]) == 2) d.insert(f[u]);
        else if (get<1>(f[u]) == 3) t.push_back(f[u]);
    }
    for (int v: G[u]) {
        dfs1(v);
        int x = get<1>(f[v]);
        if (x) {
            if (x == 1) c.insert(f[v]);
            else if (x == 2) d.insert(f[v]);
            else t.push_back(f[v]);
        }
    }
    sort(t.begin(), t.end());
    while (t.size() >= 2) {
        if (get<0>(t.back()) == get<0>(t[t.size() - 2])) {
            ans.emplace_back(get<2>(t.back()), get<2>(t[t.size() - 2]));
            t.pop_back(), t.pop_back();
        } else quit();
    }
    if (abs((int)c.size() - (int)d.size()) >= 2) quit;
    if (c.size() <= d.size()) {
        while (c.size()) {
            auto[x, y, z] = *c.begin();
            auto it = d.lower_bound({x, 0, 0});
            if (it == d.begin()) quit();
            --it;
            ans.emplace_back(z, get<2>(*it));
            c.erase(c.begin()), d.erase(it);
        }
    } else {
        while (d.size()) {
            auto[x, y, z] = *d.begin();
            auto it = c.lower_bound({x, 0, 0});
            if (it == c.end()) quit();
            ans.emplace_back(z, get<2>(*it));
            d.erase(d.begin()), c.erase(it);
        }
    }
    if (c.size() + d.size() + t.size() >= 2) quit();
    if (c.size()) f[u] = *c.begin();
    else if (d.size()) f[u] = *d.begin();
    else if (t.size()) f[u] = *t.begin();
    else f[u] = {0, 0, 0};
}

int main() {
    ios::sync_with_stdio(0); cin.tie(0);
    int n; cin >> n;
    for (int i = 1; i < n; ++i) {
        int a, b; string str; cin >> a >> b >> str;
        if (str == "Chang") chang.emplace_back(a);
        else if (str == "Duan") duan.emplace_back(a);
        else if (str == "Tong") tong.emplace_back(a);
        G[b].emplace_back(a);
        p[a] = b;
    }
    dfs0(1);
    for (int i: chang) f[i] = {dep[i], 1, i};
    for (int i: duan) f[i] = {dep[i], 2, i};
    for (int i: tong) f[i] = {dep[i], 3, i};
    dfs1(1);
    if (get<0>(f[1])) quit();
    cout << "YES\n";
    for (auto[a, b]: ans) cout << a << ' ' << b << el;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 12004kb

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

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

result:

ok Correct.

Test #3:

score: 0
Accepted
time: 2ms
memory: 13720kb

input:

2
2 1 Tong

output:

NO

result:

ok Correct.

Test #4:

score: -100
Wrong Answer
time: 27ms
memory: 17744kb

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.