QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#253107#6396. Puzzle: KusabisupepapupuWA 34ms17732kbC++173.1kb2023-11-16 18:02:562023-11-16 18:02:57

Judging History

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

  • [2023-11-16 18:02:57]
  • 评测
  • 测评结果:WA
  • 用时:34ms
  • 内存:17732kb
  • [2023-11-16 18:02:56]
  • 提交

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) {
    map<int, 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 t[get<0>(f[u])].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[get<0>(f[v])].push_back(f[v]);
        }
    }
    vector<tiii> tt;
    for (auto[i, v]: t) {
        while (v.size() >= 2) {
            ans.emplace_back(get<2>(v.back()), get<2>(v[v.size() - 2]));
            v.pop_back(), v.pop_back();
        }
        if (v.size()) tt.emplace_back(v[0]);
    }
    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() + tt.size() >= 2) quit();
    if (c.size()) f[u] = *c.begin();
    else if (d.size()) f[u] = *d.begin();
    else if (tt.size()) f[u] = *tt.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: 0ms
memory: 12520kb

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

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

input:

2
2 1 Tong

output:

NO

result:

ok Correct.

Test #4:

score: -100
Wrong Answer
time: 34ms
memory: 17732kb

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:

YES
56115 46666
88119 38802
93143 10006
76473 31531
42604 15988
30148 6569
11412 2008
64525 46703
71289 70618
81204 47748
42353 20514
97634 46131
83516 82155
66867 62410
15712 9975
4978 3205
83026 5622
48783 10902
82167 30893
93809 65878
66951 33377
94549 66936
79128 64411
22475 8453
54702 36857
905...

result:

wrong answer Pair 76598-95427 symbol not satisfied.