QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#104255#6396. Puzzle: KusabiMelacau#RE 3ms12820kbC++172.9kb2023-05-09 20:37:592023-05-09 20:38:01

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-05-09 20:38:01]
  • 评测
  • 测评结果:RE
  • 用时:3ms
  • 内存:12820kb
  • [2023-05-09 20:37:59]
  • 提交

answer

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

const int N = 1e5 + 5;

typedef enum {NONE, SAME, SHORT, LONG} NodeType;
int n, f[N], d[N];
NodeType psh[N], cur[N];

vector<int> rem[N][4];
vector<pair<int, int>> ans;

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

int solve(int u) {
    psh[u] = NONE;
    int res = 0;
    for (int i = SAME; i <= LONG; ++i) 
        sort(rem[u][i].begin(), rem[u][i].end(), [&](int x, int y){return d[x] < d[y];});
    {
        for (int i = 0, j, k; i < rem[u][SAME].size(); i = j) {
            for (j = i; d[rem[u][SAME][i]] == d[rem[u][SAME][j]] && j < rem[u][SAME].size(); ++j);
            for (k = i; k + 2 <= j; k += 2) ans.emplace_back(rem[u][SAME][k], rem[u][SAME][k + 1]);
            if (k < j) {
                if (res) gg();
                psh[u] = SAME;
                res = rem[u][SAME][k];
            }
        }
    }
    if (rem[u][SHORT].size() < rem[u][LONG].size()) {
        int diff = rem[u][LONG].size() - rem[u][SHORT].size();
        if (diff > 1 || res) gg();
        psh[u] = LONG;
        int j = 0;
        for (int i = 0; i < rem[u][SHORT].size(); ++i) {
            while (d[rem[u][LONG][j]] <= d[rem[u][SHORT][i]]) {
                if (res) gg();
                res = rem[u][LONG][j++];
            }
            ans.emplace_back(rem[u][SHORT][i], rem[u][LONG][j++]);
        }
        if (j < rem[u][LONG].size())
            res = rem[u][LONG][j];
    }
    else if (rem[u][SHORT].size() > rem[u][LONG].size()) {
        int diff = rem[u][SHORT].size() - rem[u][LONG].size();
        if (diff > 1 || res) gg();
        psh[u] = SHORT;
        int j = int(rem[u][SHORT].size()) - 1;
        for (int i = int(rem[u][LONG].size()) - 1; i >= 0; --i) {
            while (d[rem[u][SHORT][j]] >= d[rem[u][LONG][i]]) {
                if (res) gg();
                res = rem[u][SHORT][j--];
            }
            ans.emplace_back(rem[u][LONG][i], rem[u][SHORT][j--]);
        }
        if (j >= 0) res = rem[u][SHORT][j];
    }
    else {
        for (int i = 0; i < rem[u][SHORT].size(); ++i)
            if (d[rem[u][SHORT][i]] >= d[rem[u][LONG][i]]) gg();
            else ans.emplace_back(rem[u][SHORT][i], rem[u][LONG][i]);
    }   
    return res;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr); cout.tie(nullptr);
    cin >> n;
    for (int i = 2, x; i <= n; ++i) {
        cin >> x; cin >> f[x];
        d[x] = d[f[x]] + 1;
        static string s; cin >> s;
        if (s == "Tong"s) cur[x] = SAME;
        else if (s == "Duan"s) cur[x] = SHORT;
        else if (s == "Chang"s) cur[x] = LONG;
        else cur[x] = NONE;
    }
    for (int i = n; i; --i) {
        if (cur[i] != NONE) rem[i][cur[i]].emplace_back(i);
        int p = solve(i);
        if (p) {
            if (i == 1) gg();
            else rem[f[i]][psh[i]].emplace_back(p);
        }
    }
    cout << "YES\n";
    for (auto [x, y]: ans)
        cout << x << ' ' << y << '\n';
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 3ms
memory: 12728kb

input:

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

output:

YES
6 8
5 4

result:

ok Correct.

Test #2:

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

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

result:

ok Correct.

Test #3:

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

input:

2
2 1 Tong

output:

NO

result:

ok Correct.

Test #4:

score: -100
Runtime Error

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:


result: