QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#186341#6396. Puzzle: Kusabiucup-team228#AC ✓41ms16196kbC++204.9kb2023-09-23 17:30:402023-09-23 17:30:42

Judging History

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

  • [2023-09-23 17:30:42]
  • 评测
  • 测评结果:AC
  • 用时:41ms
  • 内存:16196kb
  • [2023-09-23 17:30:40]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N = 1e5 + 10;
int par[N], type[N];
vector<int> g[N];
int depth[N];

vector<int> level[N];
bool dead[N];

vector<pair<int, int>> ans;
int carry[N];

bool used[N];

void dfs1(int v) {
    if (type[v] == 3) {
        level[depth[v]].push_back(v);
    }
    for (int to : g[v]) {
        depth[to] = depth[v] + 1;
        dfs1(to);
    }
}

bool dfs2(int v) {
    used[v] = true;
    vector<pair<int, int>> up;
    vector<pair<int, int>> down;
    for (int to : g[v]) {
        if (!dead[to] && !used[to]) {
            if (!dfs2(to)) {
                return false;
            }
            if (carry[to]) {
                if (type[carry[to]] == 1) {
                    down.emplace_back(depth[carry[to]], carry[to]);
                } else if (type[carry[to]] == 2) {
                    up.emplace_back(depth[carry[to]], carry[to]);
                } else {
                    assert(false);
                }
            }
        }
    }
    if (type[v] == 1) {
        down.emplace_back(depth[v], v);
    } else if (type[v] == 2) {
        up.emplace_back(depth[v], v);
    }
    sort(up.begin(), up.end());
    sort(down.begin(), down.end());
    if (abs(int(up.size()) - int(down.size())) >= 2) {
        return false;
    } else if (up.size() == down.size()) {
        set<pair<int, int>> s;
        for (auto it : down) {
            s.insert(it);
        }
        while (!up.empty()) {
            auto [dep, u] = up.back();
            up.pop_back();
            auto it = s.lower_bound({dep + 1, 0});
            if (it == s.end()) {
                return false;
            } else {
                ans.emplace_back(u, it->second);
                s.erase(it);
            }
        }
    } else if (up.size() == down.size() + 1) {
        set<pair<int, int>> s;
        for (auto it : up) {
            s.insert(it);
        }
        for (auto [dep, u] : down) {
            auto it = s.lower_bound({dep, 0});
            if (it == s.begin()) {
                return false;
            } else {
                it--;
                ans.emplace_back(u, it->second);
                s.erase(it);
            }
        }
        assert(s.size() == 1);
        carry[v] = s.begin()->second;
    } else if (up.size() + 1 == down.size()) {
        set<pair<int, int>> s;
        for (auto it : down) {
            s.insert(it);
        }
        while (!up.empty()) {
            auto [dep, u] = up.back();
            up.pop_back();
            auto it = s.lower_bound({dep + 1, 0});
            if (it == s.end()) {
                return false;
            } else {
                ans.emplace_back(u, it->second);
                s.erase(it);
            }
        }
        assert(s.size() == 1);
        carry[v] = s.begin()->second;
    } else {
        assert(false);
    }
    return true;
}

bool solve(int n) {
    dfs1(1);
    dead[1] = true;
    for (int d = 0; d <= n; d++) {
        if (level[d].size() & 1) {
            return false;
        }
        vector<pair<int, int>> cur;
        for (int v : level[d]) {
            cur.emplace_back(v, v);
        }
        while (!cur.empty()) {
            vector<pair<int, int>> nxt;
            sort(cur.begin(), cur.end());
            for (int i = 0; i < cur.size(); i++) {
                if (i + 1 < cur.size() && cur[i].first == cur[i + 1].first) {
                    ans.emplace_back(cur[i].second, cur[i + 1].second);
                    i++;
                } else {
                    if (dead[cur[i].first]) {
                        return false;
                    } else {
                        dead[cur[i].first] = true;
                        nxt.emplace_back(par[cur[i].first], cur[i].second);
                    }
                }
            }
            swap(cur, nxt);
        }
    }
    for (int i = 1; i <= n; i++) {
        if (!used[i]) {
            if (!dfs2(i) || carry[i]) {
                return false;
            }
        }
    }
    return true;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
#endif

    int n;
    cin >> n;
    for (int i = 2; i <= n; i++) {
        int waste;
        string t;
        cin >> waste >> par[i] >> t;
        if (t == "-") {
            type[i] = 0;
        } else if (t == "Chang") {
            type[i] = 1;
        } else if (t == "Duan") {
            type[i] = 2;
        } else {
            type[i] = 3;
        }
        g[par[i]].push_back(i);
    }

    if (solve(n)) {
        cout << "YES\n";
        for (auto [u, v] : ans) {
            cout << u << " " << v << "\n";
        }
    } else {
        cout << "NO\n";
    }

#ifdef LOCAL
    cout << "\nTime elapsed: " << double(clock()) / CLOCKS_PER_SEC << " s.\n";
#endif
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

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

output:

YES
4 5
6 8

result:

ok Correct.

Test #2:

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

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

result:

ok Correct.

Test #3:

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

input:

2
2 1 Tong

output:

NO

result:

ok Correct.

Test #4:

score: 0
Accepted
time: 36ms
memory: 11948kb

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
28763 79617
61327 78961
41090 52589
3156 31315
3681 15911
24772 27446
79799 95031
14132 59851
3283 78400
7241 8873
8241 69010
19874 85932
28194 83333
33789 58699
69894 93895
34214 79826
2267 7602
10051 10691
22026 80131
54276 63173
2564 13533
8051 18882
10294 15047
8063 14566
2042 19510
3940 292...

result:

ok Correct.

Test #5:

score: 0
Accepted
time: 28ms
memory: 11868kb

input:

100000
2 1 -
3 2 -
4 3 -
5 4 -
6 4 -
7 6 -
8 7 -
9 5 -
10 9 -
11 10 -
12 6 -
13 12 -
14 11 -
15 9 -
16 14 -
17 16 -
18 10 -
19 15 -
20 13 -
21 20 -
22 17 -
23 22 -
24 22 Duan
25 11 -
26 12 -
27 20 -
28 18 -
29 28 -
30 16 -
31 28 -
32 30 -
33 31 -
34 28 -
35 34 -
36 35 -
37 22 -
38 34 -
39 38 -
40 35...

output:

YES
420 431
120 126
3704 5004
1049 1256
459 657
5364 37144
1952 2349
7076 9865
773 1535
3785 4661
5639 9907
1192 1606
4165 6455
6738 7202
5860 8278
8778 50378
14615 15535
10437 27020
16452 58982
11047 20745
16427 20964
27195 32419
2650 5438
28794 43193
39132 42829
47477 63482
32547 67846
28817 44928...

result:

ok Correct.

Test #6:

score: 0
Accepted
time: 38ms
memory: 12224kb

input:

100000
2 1 -
3 2 -
4 3 Duan
5 4 Chang
6 5 Duan
7 6 Chang
8 7 Duan
9 8 Chang
10 9 Duan
11 10 Chang
12 11 Duan
13 12 Chang
14 12 Duan
15 14 Chang
16 15 Tong
17 15 Tong
18 17 Duan
19 18 Duan
20 19 Chang
21 18 Duan
22 21 Duan
23 18 Chang
24 21 -
25 24 Duan
26 25 Chang
27 26 Duan
28 27 Chang
29 26 Duan
3...

output:

YES
16 17
50 66
76 82
121 141
154 160
135 148
185 189
156 186
237 263
246 247
227 340
235 238
331 367
289 313
322 337
377 389
510 524
328 357
351 372
385 421
422 425
419 440
428 538
492 502
443 449
371 410
437 526
542 558
634 716
562 611
579 706
608 688
728 755
569 583
622 827
548 601
805 884
821 88...

result:

ok Correct.

Test #7:

score: 0
Accepted
time: 25ms
memory: 12120kb

input:

100000
2 1 -
3 2 -
4 3 -
5 4 -
6 5 -
7 6 -
8 7 -
9 8 -
10 9 Duan
11 10 -
12 11 Chang
13 12 Duan
14 13 Chang
15 14 -
16 15 -
17 16 Duan
18 17 Chang
19 17 -
20 19 -
21 20 -
22 21 -
23 22 -
24 23 -
25 24 -
26 25 Duan
27 26 -
28 27 Duan
29 28 -
30 29 Chang
31 28 -
32 31 Chang
33 32 -
34 32 -
35 34 -
36 ...

output:

YES
103 117
105 107
260 262
279 286
307 316
375 386
385 409
479 486
465 476
429 437
562 579
710 733
808 846
995 1021
1157 1184
1328 1329
1195 1297
1241 1260
1364 1414
1376 1381
1975 2217
3083 3245
3203 3314
2197 2216
2064 2101
1512 1552
1931 1962
1836 1849
3304 3518
1580 1583
3084 3177
3230 3328
186...

result:

ok Correct.

Test #8:

score: 0
Accepted
time: 30ms
memory: 12072kb

input:

100000
2 1 -
3 2 -
4 3 -
5 4 -
6 5 Duan
7 6 -
8 7 -
9 8 -
10 9 Chang
11 10 -
12 11 Duan
13 12 Chang
14 13 -
15 14 Duan
16 15 Chang
17 16 -
18 17 -
19 18 Duan
20 19 -
21 20 -
22 21 -
23 22 Chang
24 23 -
25 24 Duan
26 25 -
27 26 Chang
28 27 -
29 28 -
30 29 -
31 30 Duan
32 31 -
33 32 -
34 33 -
35 34 -
...

output:

YES
52 53
93 94
212 214
223 224
265 266
338 341
353 383
424 430
399 400
422 429
440 455
462 466
557 558
699 707
755 757
778 787
1019 1021
1081 1110
1097 1098
1239 1261
1247 1253
1159 1160
1281 1317
1295 1331
1292 1312
1388 1402
1444 1450
1573 1575
1460 1466
1473 1507
1497 1515
1569 1601
1741 1765
18...

result:

ok Correct.

Test #9:

score: 0
Accepted
time: 30ms
memory: 12200kb

input:

100000
2 1 -
3 2 -
4 3 -
5 4 -
6 5 Duan
7 6 -
8 7 -
9 8 Chang
10 9 -
11 10 Duan
12 11 -
13 12 Chang
14 13 Duan
15 14 -
16 15 Chang
17 16 -
18 17 Duan
19 18 -
20 19 Chang
21 20 Duan
22 21 Chang
23 22 -
24 23 Duan
25 24 Chang
26 25 Duan
27 26 -
28 27 -
29 28 Chang
30 29 Duan
31 30 Chang
32 31 -
33 32 ...

output:

YES
137 138
164 165
178 179
247 248
258 259
301 302
322 323
331 332
360 361
367 368
374 375
397 399
525 527
565 566
608 611
680 684
703 705
737 738
754 761
757 762
772 773
830 831
845 849
895 897
959 970
1027 1028
1062 1066
1054 1056
1057 1058
1070 1071
1094 1095
1154 1157
1165 1170
1216 1220
1260 1...

result:

ok Correct.

Test #10:

score: 0
Accepted
time: 13ms
memory: 13152kb

input:

100000
2 1 -
3 2 -
4 3 -
5 4 -
6 5 -
7 6 -
8 7 -
9 8 -
10 9 -
11 10 -
12 11 -
13 12 -
14 13 -
15 14 -
16 15 -
17 16 -
18 17 -
19 18 -
20 19 -
21 20 -
22 21 -
23 22 -
24 23 -
25 24 -
26 25 -
27 26 -
28 27 -
29 28 -
30 29 -
31 30 -
32 31 -
33 32 -
34 33 -
35 34 -
36 35 -
37 36 -
38 37 -
39 38 -
40 39 ...

output:

YES
35528 35551
46356 46361
85950 86202
4915 4986
6570 6738
13597 14195
22019 22308
31792 31800
38225 38186
43803 43898
41995 41864
42327 42725
43807 43871
43542 43953
47608 47755
46932 47521
44955 46885
48486 48399
49839 50116
49668 49912
53355 54823
54273 54714
53853 54239
52524 52947
55640 57456
...

result:

ok Correct.

Test #11:

score: 0
Accepted
time: 25ms
memory: 11300kb

input:

100000
2 1 -
3 1 -
4 2 -
5 1 -
6 1 -
7 2 Duan
8 4 -
9 1 -
10 1 -
11 2 -
12 2 -
13 2 -
14 6 -
15 1 -
16 6 -
17 1 -
18 5 -
19 1 -
20 1 -
21 2 -
22 8 -
23 6 -
24 1 -
25 4 Duan
26 1 -
27 10 -
28 1 -
29 8 -
30 5 -
31 7 -
32 2 -
33 3 -
34 12 -
35 3 -
36 1 -
37 12 -
38 8 -
39 8 -
40 1 -
41 4 -
42 16 -
43 8...

output:

YES
19144 51496
20489 34166
15575 73797
53024 62557
29702 32886
24045 70919
72829 93199
68260 71252
328 79419
1731 14873
57325 93313
6260 32265
10041 51513
51701 53678
25394 25819
3468 57628
34041 86628
15892 89185
13897 29652
36262 63078
1798 94935
2660 7717
15781 77506
1615 23036
26468 59225
2526 ...

result:

ok Correct.

Test #12:

score: 0
Accepted
time: 30ms
memory: 10896kb

input:

100000
2 1 -
3 1 -
4 1 -
5 1 -
6 1 -
7 1 -
8 1 -
9 1 -
10 3 -
11 1 -
12 2 -
13 2 -
14 2 -
15 1 -
16 2 -
17 2 -
18 1 -
19 1 -
20 1 -
21 2 -
22 1 -
23 2 -
24 2 -
25 1 -
26 1 -
27 4 -
28 1 -
29 2 -
30 3 -
31 1 -
32 10 -
33 6 -
34 4 -
35 1 -
36 2 Duan
37 1 -
38 4 -
39 10 -
40 1 -
41 1 -
42 3 -
43 6 -
44...

output:

YES
1058 12605
1182 2525
9562 18450
236 9551
54737 79661
2891 12810
11091 23426
10715 12472
15394 21378
16387 21742
31469 36646
52709 55714
5873 19214
61133 85488
6134 51038
32066 39265
7726 28438
7445 44881
21515 87249
38360 41771
9592 13863
60781 71809
15090 81476
39272 68594
67589 89903
7770 4152...

result:

ok Correct.

Test #13:

score: 0
Accepted
time: 34ms
memory: 11304kb

input:

100000
2 1 Duan
3 1 Duan
4 1 Duan
5 1 Duan
6 1 Duan
7 1 -
8 1 Duan
9 1 Duan
10 1 -
11 1 Duan
12 1 -
13 1 Duan
14 1 Duan
15 1 Duan
16 1 Duan
17 1 Duan
18 2 -
19 1 Duan
20 1 Duan
21 2 -
22 2 Duan
23 1 Duan
24 1 Duan
25 1 Duan
26 1 Duan
27 2 Duan
28 1 Duan
29 2 Duan
30 1 Duan
31 2 Duan
32 1 -
33 1 Duan...

output:

YES
424 904
2103 2877
4225 4444
6210 6840
8060 12726
15602 19080
19299 19399
20046 20066
24634 24812
27414 28917
29521 32852
32876 41290
46356 59371
67771 80608
85254 85349
90430 93178
2438 4903
5379 8245
8331 8860
9254 9706
11981 12347
12525 13097
13225 13591
14879 20454
21163 21949
23352 24058
246...

result:

ok Correct.

Test #14:

score: 0
Accepted
time: 18ms
memory: 10720kb

input:

100000
2 1 -
3 1 -
4 1 -
5 1 -
6 1 -
7 1 -
8 1 -
9 1 -
10 1 -
11 1 -
12 1 -
13 1 Duan
14 1 -
15 1 -
16 1 -
17 1 -
18 1 -
19 1 -
20 1 -
21 1 -
22 1 -
23 1 -
24 1 -
25 1 -
26 1 -
27 1 -
28 1 -
29 1 Duan
30 1 -
31 1 -
32 2 Duan
33 1 -
34 1 -
35 1 Duan
36 1 -
37 1 -
38 1 -
39 1 -
40 1 -
41 1 -
42 1 -
43...

output:

YES
3070 3485
3492 3781
5179 6572
8297 9034
11736 11739
12880 15328
24758 26900
27252 30498
31920 40543
44585 46865
49884 53366
57509 57959
63083 81007
217 2112
3871 3969
4485 4959
6609 6615
6645 6775
6857 7157
11441 12194
12863 15527
18229 18562
20253 21901
31335 32279
36317 41179
49126 53818
54353...

result:

ok Correct.

Test #15:

score: 0
Accepted
time: 19ms
memory: 10716kb

input:

100000
2 1 Duan
3 1 -
4 1 -
5 1 Duan
6 1 -
7 1 -
8 1 -
9 1 -
10 1 -
11 1 -
12 1 -
13 1 -
14 1 -
15 1 -
16 1 -
17 1 -
18 1 -
19 1 -
20 1 -
21 1 -
22 1 -
23 1 -
24 1 -
25 1 -
26 1 -
27 1 -
28 1 -
29 1 -
30 1 -
31 1 -
32 1 -
33 1 Duan
34 1 -
35 1 -
36 1 -
37 1 -
38 1 -
39 1 -
40 1 -
41 1 -
42 1 -
43 1 ...

output:

YES
411 842
888 911
946 1055
1367 1385
1404 1457
1560 1656
1676 2457
2491 2674
2792 2998
3256 3438
3764 4010
4372 4473
5115 5573
6243 6374
6420 6978
7150 7602
7674 8871
9053 9780
10237 10338
10355 10475
10551 10853
10948 10953
11058 11128
11950 12775
13025 13567
14321 15710
16637 19349
19675 19681
2...

result:

ok Correct.

Test #16:

score: 0
Accepted
time: 15ms
memory: 10876kb

input:

100000
2 1 Duan
3 1 Duan
4 1 -
5 1 Duan
6 1 -
7 1 Duan
8 1 Duan
9 1 Duan
10 1 Duan
11 1 -
12 1 -
13 1 -
14 1 Duan
15 1 Duan
16 1 Duan
17 1 -
18 1 Duan
19 1 Duan
20 1 Duan
21 1 -
22 1 -
23 1 Duan
24 1 Duan
25 1 Duan
26 1 -
27 1 -
28 1 Duan
29 1 Duan
30 1 -
31 1 Duan
32 1 Duan
33 1 -
34 1 Duan
35 1 -
...

output:

YES
240 274
282 293
294 322
344 369
376 384
391 407
413 419
431 435
438 439
442 444
472 474
476 489
490 494
500 512
513 519
523 526
527 529
534 535
539 547
549 551
553 554
557 558
563 573
574 587
594 600
602 603
604 608
609 621
623 627
632 642
644 646
647 648
649 650
653 661
665 666
670 677
678 679
...

result:

ok Correct.

Test #17:

score: 0
Accepted
time: 26ms
memory: 11696kb

input:

100000
2 1 -
3 1 -
4 2 -
5 2 -
6 2 Duan
7 3 -
8 1 -
9 1 -
10 6 -
11 3 -
12 2 -
13 7 -
14 1 -
15 9 -
16 11 -
17 13 -
18 9 -
19 16 -
20 19 -
21 8 -
22 5 -
23 14 -
24 21 -
25 21 -
26 16 -
27 5 -
28 5 -
29 19 -
30 8 -
31 24 -
32 30 -
33 12 Duan
34 9 -
35 12 Duan
36 6 -
37 15 -
38 26 -
39 29 -
40 13 -
41...

output:

NO

result:

ok Correct.

Test #18:

score: 0
Accepted
time: 27ms
memory: 11796kb

input:

100000
2 1 Duan
3 2 Duan
4 3 -
5 3 Duan
6 4 -
7 4 Duan
8 3 Duan
9 2 -
10 4 Duan
11 8 Duan
12 6 -
13 3 Duan
14 6 Duan
15 7 Duan
16 6 Duan
17 14 Tong
18 7 -
19 16 Duan
20 14 Duan
21 12 Duan
22 20 Duan
23 14 Duan
24 19 -
25 2 Duan
26 22 -
27 24 Duan
28 8 Duan
29 4 Duan
30 23 Duan
31 24 Duan
32 23 Duan
...

output:

NO

result:

ok Correct.

Test #19:

score: 0
Accepted
time: 23ms
memory: 11564kb

input:

100000
2 1 -
3 2 -
4 3 -
5 4 -
6 5 -
7 4 -
8 7 -
9 8 -
10 9 -
11 10 Duan
12 9 -
13 12 -
14 12 -
15 10 -
16 8 -
17 13 -
18 10 -
19 14 -
20 13 -
21 19 -
22 19 Duan
23 11 -
24 23 Duan
25 23 -
26 5 -
27 22 -
28 22 Duan
29 17 -
30 29 -
31 29 -
32 17 -
33 27 -
34 33 Duan
35 31 -
36 24 -
37 34 -
38 37 -
39...

output:

NO

result:

ok Correct.

Test #20:

score: 0
Accepted
time: 24ms
memory: 11472kb

input:

100000
2 1 Duan
3 2 Chang
4 3 -
5 4 -
6 5 -
7 6 Duan
8 7 -
9 8 Chang
10 9 Duan
11 10 Chang
12 11 -
13 12 Duan
14 13 Chang
15 13 -
16 15 -
17 15 -
18 15 Duan
19 18 -
20 19 Duan
21 19 Chang
22 20 -
23 22 -
24 21 Duan
25 23 -
26 22 -
27 25 Chang
28 26 -
29 28 Duan
30 24 Chang
31 28 -
32 23 -
33 28 -
34...

output:

NO

result:

ok Correct.

Test #21:

score: 0
Accepted
time: 24ms
memory: 12028kb

input:

100000
2 1 Duan
3 2 Chang
4 3 -
5 4 -
6 5 Duan
7 6 -
8 7 -
9 8 Chang
10 9 Duan
11 10 Chang
12 11 Duan
13 12 Chang
14 12 -
15 14 -
16 15 Duan
17 16 Chang
18 17 -
19 17 Duan
20 19 -
21 19 Chang
22 21 -
23 21 Duan
24 23 Duan
25 24 Chang
26 24 Chang
27 24 Duan
28 27 -
29 28 Chang
30 28 Duan
31 30 -
32 3...

output:

NO

result:

ok Correct.

Test #22:

score: 0
Accepted
time: 36ms
memory: 11408kb

input:

100000
2 1 Duan
3 2 Chang
4 3 Duan
5 4 -
6 5 Chang
7 6 -
8 7 Duan
9 8 -
10 9 Chang
11 10 Duan
12 11 Chang
13 12 -
14 13 Duan
15 14 -
16 15 Chang
17 16 -
18 17 Duan
19 18 -
20 19 -
21 20 -
22 21 Chang
23 22 -
24 23 Duan
25 24 Chang
26 25 -
27 26 Duan
28 27 -
29 28 Chang
30 28 Duan
31 30 -
32 31 Chang...

output:

NO

result:

ok Correct.

Test #23:

score: 0
Accepted
time: 15ms
memory: 12072kb

input:

100000
2 1 Duan
3 2 -
4 3 -
5 4 -
6 5 -
7 6 -
8 7 -
9 8 -
10 9 -
11 10 -
12 11 Chang
13 12 Duan
14 13 -
15 14 -
16 15 Chang
17 16 Duan
18 17 Chang
19 18 -
20 19 -
21 20 -
22 21 -
23 22 -
24 23 -
25 24 -
26 25 Duan
27 26 -
28 27 -
29 28 -
30 29 -
31 30 -
32 31 -
33 32 -
34 33 Chang
35 34 -
36 35 -
37...

output:

NO

result:

ok Correct.

Test #24:

score: 0
Accepted
time: 18ms
memory: 11768kb

input:

100000
2 1 -
3 2 -
4 3 -
5 4 -
6 5 Duan
7 6 -
8 7 Chang
9 8 -
10 9 -
11 10 -
12 11 -
13 12 -
14 13 Duan
15 14 -
16 15 Chang
17 16 -
18 17 -
19 18 -
20 19 Duan
21 20 -
22 21 -
23 22 -
24 23 Chang
25 24 -
26 25 Duan
27 26 Chang
28 27 Duan
29 28 -
30 29 -
31 30 -
32 31 Chang
33 32 Duan
34 33 -
35 34 -
...

output:

NO

result:

ok Correct.

Test #25:

score: 0
Accepted
time: 17ms
memory: 11268kb

input:

100000
2 1 Duan
3 1 -
4 1 -
5 1 -
6 3 -
7 2 -
8 2 -
9 4 -
10 1 -
11 2 -
12 3 Duan
13 2 -
14 4 -
15 3 -
16 3 -
17 2 -
18 2 -
19 1 -
20 7 Duan
21 1 -
22 15 -
23 6 -
24 1 -
25 8 -
26 11 -
27 5 -
28 16 -
29 17 -
30 16 -
31 3 -
32 7 -
33 3 Duan
34 7 Duan
35 3 -
36 8 -
37 6 -
38 16 -
39 3 -
40 3 -
41 11 -...

output:

NO

result:

ok Correct.

Test #26:

score: 0
Accepted
time: 23ms
memory: 10984kb

input:

100000
2 1 -
3 1 -
4 1 -
5 1 -
6 1 -
7 1 Duan
8 1 -
9 1 -
10 1 -
11 3 -
12 2 -
13 1 -
14 1 -
15 1 -
16 2 -
17 1 Duan
18 3 -
19 1 -
20 1 Duan
21 8 -
22 2 -
23 3 -
24 3 Duan
25 1 -
26 1 -
27 1 -
28 1 -
29 4 -
30 1 -
31 3 -
32 3 -
33 3 -
34 2 -
35 1 -
36 1 Duan
37 1 -
38 3 -
39 2 -
40 4 -
41 2 Duan
42 ...

output:

NO

result:

ok Correct.

Test #27:

score: 0
Accepted
time: 21ms
memory: 10504kb

input:

100000
2 1 Duan
3 1 Duan
4 1 Duan
5 1 Duan
6 1 Duan
7 1 Duan
8 1 Duan
9 1 Duan
10 1 Duan
11 1 Duan
12 1 Duan
13 1 Duan
14 2 Duan
15 1 Duan
16 1 Duan
17 1 Duan
18 2 Duan
19 1 Duan
20 1 -
21 1 Duan
22 1 Duan
23 1 Duan
24 1 Duan
25 1 Duan
26 1 Duan
27 2 Duan
28 1 Duan
29 1 Duan
30 1 -
31 1 Duan
32 1 Du...

output:

NO

result:

ok Correct.

Test #28:

score: 0
Accepted
time: 27ms
memory: 11236kb

input:

100000
2 1 -
3 1 Duan
4 1 -
5 1 -
6 1 -
7 1 Duan
8 1 -
9 1 Duan
10 1 -
11 1 Duan
12 1 Duan
13 1 Duan
14 1 Duan
15 1 Duan
16 1 -
17 1 Duan
18 1 Duan
19 1 Duan
20 1 Duan
21 1 -
22 1 -
23 1 Duan
24 1 -
25 1 Duan
26 1 Duan
27 1 Duan
28 1 Duan
29 1 Duan
30 1 Duan
31 1 Duan
32 1 -
33 1 -
34 1 Duan
35 1 Du...

output:

NO

result:

ok Correct.

Test #29:

score: 0
Accepted
time: 17ms
memory: 10352kb

input:

100000
2 1 Duan
3 1 Duan
4 1 Duan
5 1 Duan
6 1 Duan
7 1 Duan
8 1 -
9 1 Duan
10 1 Duan
11 1 Duan
12 1 -
13 1 Duan
14 1 Duan
15 1 Duan
16 1 Duan
17 1 Duan
18 1 Duan
19 1 Duan
20 1 -
21 1 -
22 1 Duan
23 1 -
24 1 Duan
25 1 Duan
26 1 Duan
27 1 Duan
28 1 Duan
29 1 Duan
30 1 Duan
31 1 Duan
32 1 Duan
33 1 -...

output:

NO

result:

ok Correct.

Test #30:

score: 0
Accepted
time: 18ms
memory: 12068kb

input:

100000
2 1 Duan
3 1 -
4 1 Duan
5 1 Duan
6 1 Duan
7 1 Duan
8 1 Duan
9 1 Duan
10 1 Duan
11 1 Duan
12 1 Duan
13 1 Duan
14 1 Duan
15 1 Duan
16 1 Duan
17 1 Duan
18 1 Duan
19 1 -
20 1 Duan
21 1 Duan
22 1 Duan
23 1 Duan
24 1 Duan
25 1 Duan
26 1 Duan
27 1 Duan
28 1 -
29 1 Duan
30 1 Duan
31 1 Duan
32 1 -
33 ...

output:

NO

result:

ok Correct.

Test #31:

score: 0
Accepted
time: 33ms
memory: 16196kb

input:

100000
2 1 Duan
3 2 -
4 3 Chang
5 4 -
6 5 Duan
7 6 Chang
8 7 -
9 8 -
10 9 Duan
11 10 Chang
12 11 Duan
13 12 Chang
14 13 Duan
15 14 Chang
16 15 -
17 16 -
18 17 -
19 18 Duan
20 19 -
21 20 -
22 21 Chang
23 22 Duan
24 23 Chang
25 24 -
26 25 Duan
27 26 Chang
28 27 Duan
29 28 -
30 29 Chang
31 30 Duan
32 3...

output:

YES
13847 13848
18286 18287
19837 19838
20393 20394
20577 20578
21841 21842
22947 22948
23039 23040
24456 24457
24718 24719
25216 25217
25298 25299
25771 25772
25996 25997
26257 26258
26411 26412
26537 26538
26557 26558
26940 26941
28473 28474
28878 28879
29751 29752
29847 29848
30630 30631
30643 30...

result:

ok Correct.

Test #32:

score: 0
Accepted
time: 10ms
memory: 10100kb

input:

100000
2 1 Duan
3 1 -
4 1 -
5 1 -
6 1 -
7 1 -
8 1 -
9 1 -
10 1 -
11 1 -
12 1 Tong
13 1 -
14 1 -
15 1 -
16 1 Tong
17 1 -
18 1 -
19 1 -
20 1 -
21 1 -
22 1 Tong
23 1 -
24 1 -
25 1 -
26 1 -
27 1 -
28 1 -
29 1 -
30 1 -
31 1 -
32 1 -
33 1 -
34 1 -
35 1 -
36 1 -
37 1 -
38 1 Tong
39 1 -
40 1 -
41 1 -
42 1 -...

output:

YES
12 16
22 38
43 45
50 58
61 62
75 83
96 102
103 107
120 122
124 127
164 172
195 201
203 208
221 233
238 242
251 253
256 257
261 265
270 271
278 285
286 287
300 301
303 317
322 328
330 345
351 378
380 393
394 397
403 405
414 415
422 425
442 444
448 456
460 461
463 484
486 504
510 514
518 522
530 5...

result:

ok Correct.

Test #33:

score: 0
Accepted
time: 26ms
memory: 11580kb

input:

93284
2 1 -
3 1 Duan
4 2 -
5 4 -
6 2 -
7 4 Duan
8 1 -
9 4 -
10 4 Duan
11 6 -
12 7 -
13 6 -
14 1 Duan
15 4 -
16 9 -
17 12 -
18 15 -
19 6 Duan
20 1 -
21 15 -
22 2 -
23 5 Duan
24 7 -
25 22 -
26 13 -
27 12 -
28 6 -
29 27 Duan
30 7 Duan
31 9 -
32 14 -
33 3 -
34 21 -
35 18 -
36 35 -
37 7 -
38 17 Duan
39 2...

output:

YES
25771 67448
7176 35252
8371 48280
11355 16762
72424 78066
1039 4000
7254 8065
1104 22997
1552 18781
5362 30421
38438 80287
8106 29300
3134 53775
9719 56514
10601 14281
48145 87748
30607 79092
48623 51727
8913 13260
32430 41207
15464 83299
1540 32064
1076 58857
5240 7253
1031 26765
27920 50765
23...

result:

ok Correct.

Test #34:

score: 0
Accepted
time: 37ms
memory: 11668kb

input:

92284
2 1 Duan
3 2 -
4 3 Duan
5 2 Duan
6 3 -
7 4 Duan
8 6 Duan
9 4 Duan
10 6 Duan
11 4 Duan
12 8 Chang
13 1 Duan
14 5 Duan
15 5 -
16 15 Duan
17 15 Duan
18 13 Chang
19 12 Duan
20 4 -
21 1 Duan
22 17 Duan
23 2 Duan
24 14 Duan
25 17 Duan
26 22 Duan
27 4 -
28 26 Duan
29 9 Duan
30 15 Duan
31 3 Duan
32 7 ...

output:

YES
38828 58063
3062 36738
10928 69515
4051 28790
23444 84810
14004 38175
55023 67540
7990 22128
9994 21285
25029 90228
907 32256
7032 8117
56 76538
95 832
18957 79752
2694 8493
10058 33594
14242 14870
4139 92214
3552 5776
12069 75175
8698 53284
3277 68335
3395 89798
4302 41332
4239 13794
37206 4024...

result:

ok Correct.

Test #35:

score: 0
Accepted
time: 34ms
memory: 11600kb

input:

93444
2 1 -
3 1 Duan
4 1 Duan
5 2 -
6 4 -
7 3 -
8 6 Duan
9 6 Duan
10 9 -
11 2 -
12 1 -
13 11 -
14 5 -
15 14 Duan
16 11 -
17 16 -
18 4 -
19 7 Duan
20 15 -
21 17 -
22 21 -
23 20 -
24 23 Duan
25 7 -
26 14 Duan
27 18 -
28 13 Duan
29 2 Duan
30 19 Duan
31 5 -
32 7 Duan
33 23 Duan
34 17 Duan
35 2 Duan
36 2...

output:

YES
2245 5451
7473 10206
6478 8193
4623 63353
42330 58464
1745 78187
3242 86375
14695 77097
6658 9341
2510 15339
2500 11814
3823 8728
15405 36710
1619 30479
39351 40168
1597 78181
8953 33067
33227 52978
38592 49996
34857 47774
9958 16925
24755 81083
23373 53351
31903 59286
68354 79681
446 76598
2000...

result:

ok Correct.

Test #36:

score: 0
Accepted
time: 41ms
memory: 11796kb

input:

98244
2 1 Duan
3 1 Duan
4 3 Duan
5 4 Duan
6 4 Duan
7 4 Duan
8 5 Duan
9 8 Duan
10 5 Duan
11 9 Duan
12 3 Duan
13 11 Tong
14 5 Duan
15 5 Duan
16 12 Duan
17 2 Duan
18 3 Duan
19 4 Duan
20 2 Duan
21 6 Duan
22 21 Duan
23 1 -
24 15 -
25 20 Duan
26 8 Duan
27 13 Duan
28 12 Duan
29 22 Duan
30 24 Duan
31 21 Dua...

output:

YES
47712 94678
30080 59506
97 74265
23770 76738
112 97842
3542 69818
5092 64950
73539 86537
5799 63910
37557 39012
82408 82910
32873 50798
32809 37935
169 66154
3185 30377
2151 2221
6053 16134
66653 87276
5473 28227
2513 24524
33795 80664
7453 51541
682 1482
141 7794
2542 32164
195 1525
13722 76147...

result:

ok Correct.

Test #37:

score: 0
Accepted
time: 7ms
memory: 9752kb

input:

25284
2 1 Duan
3 2 -
4 3 -
5 3 Duan
6 2 Duan
7 3 Duan
8 1 Duan
9 5 -
10 4 -
11 5 Duan
12 4 Duan
13 1 Duan
14 4 -
15 10 Duan
16 8 -
17 13 -
18 15 -
19 12 -
20 16 Duan
21 6 Duan
22 2 Duan
23 19 -
24 12 -
25 2 -
26 19 Duan
27 5 -
28 4 -
29 9 -
30 12 Duan
31 7 Duan
32 31 -
33 21 -
34 24 Duan
35 28 Duan
...

output:

YES
993 21116
14105 14395
4289 21260
2540 2930
6639 6909
13944 14434
510 11662
3025 9061
9793 12166
22526 22586
75 1745
4092 14360
92 12541
240 24491
1269 7652
665 3499
692 1879
561 1776
531 792
16681 19190
3543 18684
5214 5426
3525 12636
5966 20638
16744 23470
8846 18925
4746 20968
14626 21135
5415...

result:

ok Correct.

Test #38:

score: 0
Accepted
time: 36ms
memory: 11884kb

input:

93246
2 1 Duan
3 2 Duan
4 3 Duan
5 1 -
6 4 Tong
7 4 Duan
8 4 Duan
9 8 -
10 6 Duan
11 2 Duan
12 1 Duan
13 11 Duan
14 8 Duan
15 1 Duan
16 8 Duan
17 10 Duan
18 3 Duan
19 1 Duan
20 18 Duan
21 10 Duan
22 14 Duan
23 11 Duan
24 19 Duan
25 21 Duan
26 17 Duan
27 24 Duan
28 24 Duan
29 17 Tong
30 23 Duan
31 17...

output:

YES
20776 34186
4891 14299
17675 47821
30209 64734
1308 32227
9990 40253
13353 31960
54207 69829
6059 45774
14728 25053
1353 8027
11026 26226
3886 68595
33220 35228
48618 63670
3969 5264
25564 32318
9052 71328
18678 43434
11764 28292
36263 81047
512 1296
52312 86815
6 65969
3546 12172
1099 13939
154...

result:

ok Correct.

Test #39:

score: 0
Accepted
time: 32ms
memory: 11844kb

input:

99837
2 1 -
3 1 -
4 3 Duan
5 2 Duan
6 5 Duan
7 3 Duan
8 5 -
9 6 Duan
10 4 -
11 4 -
12 6 Duan
13 8 -
14 7 -
15 10 Duan
16 11 Duan
17 8 Duan
18 6 -
19 13 Duan
20 16 Duan
21 20 -
22 19 Chang
23 4 Duan
24 13 Duan
25 21 -
26 17 Duan
27 7 Duan
28 8 -
29 3 -
30 18 -
31 21 Duan
32 27 Duan
33 21 Duan
34 17 -...

output:

YES
603 6013
63 185
3866 3940
30910 65564
11667 12218
77403 94273
1400 86124
9496 93491
30346 55493
51616 84632
25108 94309
15346 38323
430 68699
3715 3818
4744 5255
25323 48618
11658 40500
1129 7260
11856 14535
22717 65165
3634 39151
8999 64048
14576 89242
12610 29865
19909 98550
18354 29092
20437 ...

result:

ok Correct.

Test #40:

score: 0
Accepted
time: 31ms
memory: 11644kb

input:

91248
2 1 Duan
3 1 Duan
4 1 Duan
5 2 Duan
6 3 Duan
7 3 Chang
8 2 Duan
9 1 Duan
10 7 Duan
11 10 Duan
12 4 Duan
13 2 Duan
14 11 Duan
15 6 Duan
16 8 Duan
17 7 Duan
18 2 Duan
19 15 Duan
20 4 Duan
21 7 Duan
22 6 Duan
23 6 Duan
24 20 Duan
25 13 Duan
26 22 Duan
27 5 Duan
28 19 Duan
29 14 Duan
30 12 -
31 8 ...

output:

YES
4282 76466
627 8607
14799 79826
31347 47797
51145 87837
683 83512
21360 41159
604 2388
5150 23230
36864 48167
10840 37828
14041 16074
608 8294
1148 29292
7683 39341
8227 36618
18411 35808
6547 18634
34055 37431
45110 49663
66293 69359
10884 54094
13885 16287
49148 69524
804 12470
27013 30982
57 ...

result:

ok Correct.

Test #41:

score: 0
Accepted
time: 33ms
memory: 11664kb

input:

93538
2 1 -
3 2 -
4 2 -
5 3 Duan
6 4 -
7 4 -
8 2 -
9 1 -
10 7 -
11 4 Duan
12 8 -
13 9 -
14 1 Duan
15 7 Duan
16 3 -
17 5 -
18 14 Duan
19 5 -
20 7 -
21 15 Duan
22 9 -
23 8 -
24 16 Duan
25 5 Duan
26 6 Duan
27 10 -
28 7 -
29 21 Duan
30 1 -
31 18 -
32 13 -
33 6 -
34 28 -
35 16 Duan
36 32 Duan
37 22 -
38 ...

output:

YES
2127 8347
4574 29754
86 211
1376 33809
4941 42030
8773 9501
36242 45163
4227 5723
41232 56423
32894 37374
21300 24505
17730 22241
43590 85233
32449 42405
78359 79403
71885 78212
35794 92829
50839 54153
16389 79352
703 11872
1900 11867
31471 43748
1121 35626
12374 17007
34350 42448
56215 68825
87...

result:

ok Correct.

Test #42:

score: 0
Accepted
time: 29ms
memory: 11648kb

input:

92384
2 1 Duan
3 2 -
4 3 Duan
5 3 Duan
6 3 -
7 2 Duan
8 1 -
9 7 Duan
10 5 Chang
11 4 -
12 3 Duan
13 7 -
14 9 Duan
15 14 Chang
16 3 Duan
17 6 Duan
18 10 Duan
19 17 Duan
20 10 -
21 2 Duan
22 10 -
23 5 Duan
24 18 -
25 11 -
26 13 Duan
27 12 Duan
28 1 -
29 18 Duan
30 28 Duan
31 28 -
32 16 -
33 27 Duan
34...

output:

YES
7970 31792
646 33205
74103 90866
677 20080
24340 31849
1461 6816
42819 54000
16191 64152
21434 55763
31148 33373
1387 61344
24918 51399
524 2831
3487 78295
61012 71242
31384 53900
5974 20263
24271 26218
16339 37880
6680 23940
49315 92180
3034 7848
18481 74463
8893 15694
12200 68750
40801 64715
4...

result:

ok Correct.

Test #43:

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

input:

1

output:

YES

result:

ok Correct.