QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#230987#6396. Puzzle: KusabiUndefinedTL 2ms9820kbC++204.3kb2023-10-28 22:53:332023-10-28 22:53:34

Judging History

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

  • [2023-10-28 22:53:34]
  • 评测
  • 测评结果:TL
  • 用时:2ms
  • 内存:9820kb
  • [2023-10-28 22:53:33]
  • 提交

answer

#include <algorithm>
#include <cstdio>
#include <vector>

constexpr int N = 200005;

int n;

std::vector<int> G[N];

int tp[N], dep[N];

struct node
{
    int tp, dep, id;
};

void dfs1(int u, int fa)
{
    for (int v : G[u])
    {
        if (v == fa) continue;
        dep[v] = dep[u] + 1;
        dfs1(v, u);
    }
}

using pii = std::pair<int, int>;

std::vector<pii> ans;

node dfs2(int u, int fa)
{
    node res{0, 0, 0};
    std::vector<pii> sub[4];
    sub[tp[u]].emplace_back(dep[u], u);
    for (int v : G[u])
    {
        if (v == fa) continue;
        node t = dfs2(v, u);
        if (t.tp == -1) return node{-1, -1, -1};
        if (t.tp > 0) sub[t.tp].emplace_back(t.dep, t.id);
    }
    for (int i = 1; i <= 3; ++i) std::sort(sub[i].begin(), sub[i].end(), std::greater<pii>());
    if (!sub[3].empty())
    {
        int cnt = 1;
        for (int i = 1; i < sub[3].size(); ++i)
        {
            if (sub[3][i].first != sub[3][i - 1].first)
            {
                if (cnt & 1)
                {
                    if (res.tp) return node{-1, -1, -1};
                    res = node{3, sub[3][i - 1].first, sub[3][i - 1].second};
                }
                for (int j = i - cnt; j < i - 1; j = 2)
                    ans.emplace_back(sub[3][j].second, sub[3][j + 1].second);
                cnt = 1;
            }
            else
                cnt++;
        }
        for (int j = sub[3].size() - cnt; j < sub[3].size() - 1; j += 2)
            ans.emplace_back(sub[3][j].second, sub[3][j + 1].second);
        if (cnt & 1)
        {
            if (res.tp != 0) return (node){-1, -1, -1};
            res = (node){3, sub[3][sub[3].size() - 1].first, sub[3][sub[3].size() - 1].second};
        }
    }
    if (sub[1].size() == sub[2].size())
    {
        for (int i = 0; i < sub[1].size(); i++)
        {
            if (sub[1][i].first <= sub[2][i].first) return (node){-1, -1, -1};
            ans.emplace_back(sub[1][i].second, sub[2][i].second);
        }
    }
    else if (sub[1].size() > sub[2].size())
    {
        int poi = sub[1].size() - 1;
        for (int i = sub[2].size() - 1; i >= 0; i--)
        {
            while (poi >= 0 && sub[1][poi].first <= sub[2][i].first)
            {
                if (res.tp != 0) return node{-1, -1, -1};
                res = node{1, sub[1][poi].first, sub[1][poi].second}, poi--;
            }
            if (poi >= 0 && sub[1][poi].first > sub[2][i].first)
            {
                ans.emplace_back(sub[1][poi].second, sub[2][i].second);
                poi--;
            }
            else
                return node{-1, -1, -1};
        }
        while (poi >= 0)
        {
            if (res.tp != 0) return node{-1, -1, -1};
            res = node{1, sub[1][poi].first, sub[1][poi].second}, poi--;
        }
    }
    else
    {
        int poi = 0;
        for (int i = 0; i < sub[1].size(); i++)
        {
            while (poi < sub[2].size() && sub[1][i].first <= sub[2][poi].first)
            {
                if (res.tp != 0) return node{-1, -1, -1};
                res = node{2, sub[2][poi].first, sub[2][poi].second}, poi++;
            }
            if (poi < sub[2].size() && sub[1][i].first > sub[2][poi].first)
            {
                ans.emplace_back(sub[1][i].second, sub[2][poi].second);
                poi++;
            }
            else
                return (node){-1, -1, -1};
        }
        while (poi < sub[2].size())
        {
            if (res.tp != 0) return (node){-1, -1, -1};
            res = (node){2, sub[2][poi].first, sub[2][poi].second}, poi++;
        }
    }
    if (u == 1 && res.tp) return (node){-1, -1, -1};
    return res;
}

int main()
{
    scanf("%d", &n);
    for (int i = 1; i < n; i++)
    {
        int u, v;
        char str[20];
        scanf("%d%d%s", &u, &v, str);
        G[u].push_back(v);
        G[v].push_back(u);
        if (str[0] == 'C')
            tp[u] = 1;
        else if (str[0] == 'D')
            tp[u] = 2;
        else if (str[0] == 'T')
            tp[u] = 3;
    }
    dfs1(1, 0);
    node res = dfs2(1, 0);
    if (res.tp == -1)
        puts("NO");
    else
    {
        puts("YES");
        for (int i = 0; i < ans.size(); i++) printf("%d %d\n", ans[i].first, ans[i].second);
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 2ms
memory: 9820kb

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

result:

ok Correct.

Test #3:

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

input:

2
2 1 Tong

output:

NO

result:

ok Correct.

Test #4:

score: -100
Time Limit Exceeded

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: