QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#371020#6396. Puzzle: Kusabiricofx#WA 16ms16708kbC++172.9kb2024-03-29 21:06:272024-03-29 21:07:08

Judging History

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

  • [2024-03-29 21:07:08]
  • 评测
  • 测评结果:WA
  • 用时:16ms
  • 内存:16708kb
  • [2024-03-29 21:06:27]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define enl putchar('\n')
const int MAXN = 1e5 + 5;
typedef pair<int, int> pii;
int n, m;
string s;
struct EG {
    int to, nxt;
}e[MAXN];
int head[MAXN], etot;
inline void add(int u, int v) {
    e[etot] = { v,head[u] };
    head[u] = etot++;
}
struct FFF {
    vector<int>v;
    priority_queue<int>q1, q2;
}f[MAXN];
int fa[MAXN], dep[MAXN];
vector<pii>ans;
bool bfs() {
    vector<int>vec;
    queue<int>q;
    q.push(1);
    while (!q.empty()) {
        int u = q.front(); q.pop();
        vec.push_back(u);
        for (int i = head[u]; ~i; i = e[i].nxt) {
            q.push(e[i].to);
            dep[e[i].to] = dep[u] + 1;
        }
    }
    reverse(vec.begin(), vec.end());
    for (int u : vec) {
        int sizq1 = f[u].q1.size(), sizq2 = f[u].q2.size();
        if (abs(sizq1 - sizq2) > 1)return !printf("NO\n");
        int rst = -1;
        while (!f[u].q1.empty() && !f[u].q2.empty()) {
            int x = f[u].q1.top(), y = f[u].q2.top();
            f[u].q1.pop(); f[u].q2.pop();
            if (dep[x] >= dep[y]) {
                if (rst == -1) {
                    if (sizq1 > sizq2) {
                        rst = x;
                        x = f[u].q1.top();
                        f[u].q1.pop();
                    } else {
                        rst = y;
                        y = f[u].q2.top();
                        f[u].q2.pop();
                    }
                }
                return !printf("NO\n");
            }
            ans.push_back({ x,y });
        }
        if (rst != -1) {
            if (sizq1 > sizq2)f[u].q1.push(rst);
            else f[u].q2.push(rst);
        }
        if (f[u].v.size() + f[u].q1.size() + f[u].q2.size() > 1)
            return !printf("NO\n");
        if (!f[u].v.empty())f[fa[u]].v.push_back(f[u].v[0]);
        if (!f[u].q1.empty())f[fa[u]].q1.push(f[u].q1.top());
        if (!f[u].q2.empty())f[fa[u]].q2.push(f[u].q2.top());
        if (f[fa[u]].v.size() == 2) {
            int x = f[fa[u]].v.back(); f[fa[u]].v.pop_back();
            int y = f[fa[u]].v.back(); f[fa[u]].v.pop_back();
            ans.push_back({ x,y });
        }
    }
    if (f[1].v.size() || f[1].q1.size() || f[1].q2.size())
        return !printf("NO\n");
    return true;
}

int main() {
    ios::sync_with_stdio(0); cin.tie(0);
    memset(head, -1, sizeof(head));
    cin >> n;
    for (int i = 1; i < n; ++i) {
        int u, v;
        cin >> u >> v >> s;
        fa[u] = v;
        add(v, u);
        if (s == "Tong")f[u].v.push_back(u);
        else if (s == "Duan")f[u].q1.push(u);
        else if (s == "Chang")f[u].q2.push(u);
    }
    // for(int i=1;i<=n;++i,enl)
    //     for(int j=0;j<3;++j)
    //         // if(!f[i][j].empty())
    //             printf("%d ",f[i][j].size());
    if (bfs()) {
        printf("YES\n");
        for (auto [x, y] : ans)
            printf("%d %d\n", x, y);
    }
}

詳細信息

Test #1:

score: 100
Accepted
time: 4ms
memory: 12980kb

input:

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

output:

YES
5 4
6 8

result:

ok Correct.

Test #2:

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

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: 0ms
memory: 12792kb

input:

2
2 1 Tong

output:

NO

result:

ok Correct.

Test #4:

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

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.