QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#528628#6396. Puzzle: KusabiOneWanAC ✓45ms31400kbC++234.7kb2024-08-23 17:12:232024-08-23 17:12:23

Judging History

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

  • [2024-08-23 17:12:23]
  • 评测
  • 测评结果:AC
  • 用时:45ms
  • 内存:31400kb
  • [2024-08-23 17:12:23]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
vector<int> v[1000005];
int type[100005];  //1 tong 2 duan 3 chang
pair<int, int> hv[100005];
int dep[100005];
set<pair<int, int>> res;
bool cmp(int x, int y)
{
    return dep[x] < dep[y];
}
void dfs(int u, int fa)
{
    dep[u] = dep[fa] + 1;
    vector<int> t;
    vector<int> d;
    vector<int> c;
    if (type[u] != 0) {
        if (type[u] == 1) {
            t.push_back(u);
        } else if (type[u] == 2) {
            d.push_back(u);
        } else if (type[u] == 3) {
            c.push_back(u);
        }
    }
    for (auto x : v[u]) {
        if (x == fa) continue;
        dfs(x, u);
        if (hv[x].first != 0) {
            if (hv[x].first == 1) {
                t.push_back(hv[x].second);
            } else if (hv[x].first == 2) {
                d.push_back(hv[x].second);
            } else if (hv[x].first == 3) {
                c.push_back(hv[x].second);
            }
        }
    }
    sort(begin(c), end(c), cmp);
    sort(begin(d), end(d), cmp);
    sort(begin(t), end(t), cmp);
    int less = 0;
    int lessid = 0;
    //cout<<u<<" "<<t.size()<<'\n';
    for (int i = 0; i < t.size(); i++) {
        if (i + 1 < t.size() && dep[t[i]] == dep[t[i + 1]]) {
            res.insert({t[i], t[i + 1]});
            i++;
        } else {
            less++;
            if (hv[u].first != 0) {
                cout << "NO\n";
                exit(0);
            }
            hv[u].first = 1;
            hv[u].second = t[i];
        }
    }
    int l = 0, r = 0;
    // reverse(begin(c),end(c));
    //reverse(begin(d),end(d));

    //1 tong 2 duan 3 chang
    if (c.size() > d.size()) {
        for (l = 0, r = 0; l < d.size(); l++) {
            while (r < c.size() && dep[d[l]] >= dep[c[r]]) {
                if (hv[u].first != 0) {
                    cout << "NO\n";
                    exit(0);
                } else {
                    hv[u].first = 3;
                    hv[u].second = c[r];
                }
                r++;
            }
            if (r < c.size()) {
                res.insert({c[r], d[l]});
                r++;
            } else {
                if (hv[u].first != 0) {
                    cout << "NO\n";
                    exit(0);
                } else {
                    hv[u].first = 2;
                    hv[u].second = d[r];
                }
            }
        }
        for (; r < c.size(); r++) {
            if (hv[u].first != 0) {
                cout << "NO\n";
                exit(0);
            } else {
                hv[u].first = 3;
                hv[u].second = c[r];
            }
        }
    } else {
        // c.size( )<d.size()
        //1 tong 2 duan 3 chang
        reverse(begin(c), end(c));
        reverse(begin(d), end(d));
        for (l = 0, r = 0; l < c.size(); l++) {
            while (r < d.size() && dep[c[l]] <= dep[d[r]]) {
                if (hv[u].first != 0) {
                    cout << "No\n";
                    exit(0);
                } else {
                    hv[u].first = 2;
                    hv[u].second = d[r];
                }
                r++;
            }
            if (r < d.size()) {
                res.insert({c[l], d[r]});
                r++;
            } else {
                if (hv[u].first != 0) {
                    cout << "No\n";
                    exit(0);
                } else {
                    hv[u].first = 3;
                    hv[u].second = c[l];
                }
            }
        }
        //cout<<u<<" "<<d.size()<<" "<<c.size()<<" "<<hv[u].first<<" "<<'\n';
        for (r; r < d.size(); r++) {
            if (hv[u].first != 0) {
                // cout<<u<<"?\n";
                cout << "No\n";
                exit(0);
            } else {
                hv[u].first = 2;
                hv[u].second = d[r];
            }
        }
    }
    if (u == 1 && hv[u].first != 0) {
        cout << "No\n";
        exit(0);
    }
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin >> n;
    for (int i = 1; i <= n - 1; i++) {
        int x, y;
        cin >> x >> y;
        v[x].push_back(y);
        v[y].push_back(x);
        string sx;
        cin >> sx;
        if (sx == "Tong") {
            type[x] = 1;
        } else if (sx == "Duan") {
            type[x] = 2;
        } else if (sx == "Chang") {
            type[x] = 3;
        } else {
            type[x] = 0;
        }
    }

    dfs(1, 0);
    cout << "YES\n";
    for (auto [x, y] : res) {
        cout << x << " " << y << '\n';
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

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

output:

YES
4 5
8 6

result:

ok Correct.

Test #2:

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

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

result:

ok Correct.

Test #3:

score: 0
Accepted
time: 1ms
memory: 3840kb

input:

2
2 1 Tong

output:

No

result:

ok Correct.

Test #4:

score: 0
Accepted
time: 44ms
memory: 12288kb

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
59 5527
106 86
109 2476
130 10422
164 93
181 39
200 73147
204 126
227 4270
228 197
237 305
265 70
309 86323
323 217
326 1631
331 10331
339 253
355 49414
366 73134
376 674
393 81
423 6489
431 9646
432 1348
433 62
447 435
541 19628
546 63088
553 86498
563 498
591 320
597 2877
610 25453
612 418
621...

result:

ok Correct.

Test #5:

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

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
126 120
128 85
226 162
256 138
269 240
295 5055
340 81
431 420
519 345
614 382
627 968
656 563
657 459
685 112
768 24
773 1535
823 168
854 1429
874 151
899 548
931 407
1049 1256
1088 333
1103 402
1192 1606
1222 1152
1326 1205
1327 1377
1489 206
1550 1130
1689 1919
1700 814
1703 744
1721 351
1756...

result:

ok Correct.

Test #6:

score: 0
Accepted
time: 45ms
memory: 13136kb

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
5 4
7 6
9 8
11 10
13 12
15 14
16 17
20 19
23 18
26 25
28 27
33 21
34 22
36 35
37 31
39 38
41 29
42 40
43 32
45 30
48 46
50 66
53 52
57 56
60 54
61 58
63 49
65 55
68 64
69 67
73 59
76 82
78 70
79 71
85 83
86 84
87 80
90 75
91 74
93 77
94 89
100 97
101 98
104 95
105 92
109 106
112 96
113 111
114 9...

result:

ok Correct.

Test #7:

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

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
12 10
14 13
18 17
30 28
32 26
47 38
55 51
70 59
82 74
85 81
103 117
105 107
116 80
131 126
134 130
139 145
141 137
144 138
156 147
157 133
160 151
165 149
172 168
181 163
185 174
188 179
189 173
199 169
216 197
218 207
230 224
231 211
247 244
250 233
255 223
260 262
265 253
275 273
279 286
290 2...

result:

ok Correct.

Test #8:

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

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
10 6
13 12
16 15
23 19
27 25
36 31
44 39
47 46
52 53
62 59
66 65
69 68
74 71
81 79
87 82
92 83
93 94
100 98
105 102
110 108
117 112
120 118
130 122
133 132
142 134
148 144
161 155
168 165
170 169
173 171
178 175
187 183
199 194
205 203
207 202
212 214
217 215
223 224
225 221
232 226
237 227
242 ...

result:

ok Correct.

Test #9:

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

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
9 6
13 11
16 14
20 18
22 21
25 24
29 26
31 30
34 33
38 35
40 39
44 42
49 47
52 51
54 53
56 55
58 57
60 59
62 61
64 63
70 66
77 74
80 78
83 81
86 84
92 87
94 93
98 96
102 100
108 107
110 109
115 114
117 116
121 118
125 124
130 126
134 131
137 138
146 144
150 149
152 147
156 155
162 158
164 165
17...

result:

ok Correct.

Test #10:

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

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
187 46
560 379
664 617
1599 1104
1891 1692
2120 2060
2560 2231
3072 3050
3161 3156
3700 3406
3939 3934
4338 4116
4581 4326
4774 4758
4986 4915
5281 5196
5513 5384
6435 5677
6606 6536
6738 6570
7037 7012
7289 7170
7686 7658
7922 7894
8465 8110
9157 8862
9769 9716
10261 9829
11281 11025
12004 1180...

result:

ok Correct.

Test #11:

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

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
328 79419
707 35744
779 15678
1078 86789
1081 22959
1210 37184
1466 25727
1615 23036
1731 14873
1798 94935
1824 8696
1830 1992
1858 9218
1935 38351
1963 27369
2148 85534
2195 63581
2282 19342
2296 23421
2302 5427
2395 2713
2475 30917
2526 20722
2533 291
2551 19630
2572 44828
2574 62058
2652 743
...

result:

ok Correct.

Test #12:

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

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
236 9551
350 2306
711 15899
954 17703
1058 12605
1182 2525
1524 22723
1751 6314
1992 62081
2162 9312
2400 52630
2519 12330
2822 36463
2834 87704
2891 12810
3012 68132
3038 40924
3052 37450
3061 50775
3078 23080
3103 49533
3184 14343
3447 84390
3462 21868
3491 63352
3663 7603
3713 52176
3738 2269...

result:

ok Correct.

Test #13:

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

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
61 98201
258 55962
454 2940
507 4901
869 1413
1036 11
1216 11916
1231 2836
1254 14695
1717 4452
1773 7270
1853 16545
1930 5718
1937 3122
1942 19710
1998 15524
2010 19269
2064 17876
2178 14256
2181 2404
2196 6443
2303 3368
2365 2994
2438 4903
2517 3253
2569 20981
2687 3558
2730 20993
2734 26720
2...

result:

ok Correct.

Test #14:

score: 0
Accepted
time: 22ms
memory: 11588kb

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
217 31335
1054 9137
1139 1581
1282 2606
1390 1958
1392 2655
1630 2432
1741 2174
1769 221
1853 3020
1872 2715
1981 25759
2112 20253
2136 11759
2192 2538
2199 2365
2262 8949
2283 15868
2306 12078
2371 1970
2415 2661
2425 16215
2426 2531
2456 2756
2515 4249
2528 2881
2534 3086
2555 2591
2583 3445
2...

result:

ok Correct.

Test #15:

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

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
647 4243
688 809
784 717
813 8585
825 1165
842 888
911 946
1007 6056
1055 1367
1073 518
1124 8017
1197 1088
1219 1595
1240 1132
1282 12370
1385 1404
1395 18335
1399 1279
1405 1356
1420 1216
1438 15733
1457 411
1558 1079
1563 1012
1630 1610
1649 1594
1656 1676
1664 1508
1672 1125
1706 1431
1726 1...

result:

ok Correct.

Test #16:

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

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
274 240
293 282
322 294
369 344
384 376
407 391
419 500
435 431
439 438
444 442
464 1050
474 472
489 476
494 490
512 413
519 513
526 523
529 527
535 534
543 11127
547 539
551 549
554 609
558 557
569 564
573 563
577 570
587 574
600 594
603 602
608 604
621 754
627 623
630 624
642 632
646 644
648 6...

result:

ok Correct.

Test #17:

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

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: 22ms
memory: 10864kb

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

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: 27ms
memory: 11688kb

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: 28ms
memory: 11392kb

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

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: 21ms
memory: 10004kb

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: 25ms
memory: 12212kb

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: 26ms
memory: 11048kb

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: 22ms
memory: 11640kb

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: 27ms
memory: 12124kb

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: 18ms
memory: 11524kb

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: 29ms
memory: 13040kb

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: 26ms
memory: 13276kb

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: 34ms
memory: 31400kb

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
4 2
7 6
11 10
13 12
15 14
22 19
24 23
27 26
30 28
33 31
35 34
37 36
39 38
42 41
44 43
49 48
51 50
54 53
56 55
59 57
61 60
64 63
68 67
70 69
73 71
75 74
77 76
79 78
81 80
84 83
87 85
89 88
91 90
94 93
96 95
100 98
105 101
109 107
111 110
114 112
117 115
120 118
122 121
124 123
126 125
128 127
133...

result:

ok Correct.

Test #32:

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

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

result:

ok Correct.

Test #33:

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

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
148 51159
211 101
234 494
310 313
314 15050
315 262
354 708
364 78599
414 48732
597 821
747 1120
856 37465
897 745
916 72
942 662
986 25004
1031 26765
1039 4000
1070 9400
1076 58857
1078 243
1104 22997
1240 11484
1275 84508
1287 8723
1290 1325
1301 25835
1302 31396
1409 33109
1413 1124
1442 2455...

result:

ok Correct.

Test #34:

score: 0
Accepted
time: 40ms
memory: 12032kb

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
12 8
18 13
35 52669
50 11915
56 76538
84 29
95 832
116 10
133 132
140 125
162 2605
178 15469
183 49
203 124
206 127
220 87
234 15260
241 77237
249 194
260 1747
267 47
270 51
280 236
281 154
285 157
317 21410
332 738
334 248
372 4289
375 339
386 198
388 108
394 14
395 302
402 488
404 67
420 257
4...

result:

ok Correct.

Test #35:

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

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
60 707
75 30651
148 11159
168 1155
225 17127
258 8431
279 245
293 92310
363 7098
386 35
399 4150
446 76598
461 391
465 7755
481 55
583 412
589 86635
612 543
639 2996
660 28433
687 19463
712 914
734 83655
802 2221
805 637
847 766
860 41976
879 422
903 53481
951 918
952 55779
968 818
969 8442
998 ...

result:

ok Correct.

Test #36:

score: 0
Accepted
time: 40ms
memory: 12748kb

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
13 90829
92 19
94 71
97 74265
112 97842
119 51
132 1030
138 190
141 7794
158 3619
160 93
164 624
169 66154
188 47
195 1525
197 61
201 25153
206 810
233 82
247 19333
261 702
266 45100
275 124
286 171
337 2486
358 14010
359 86
379 35642
384 193
424 59558
427 38013
428 315
447 3933
467 21392
480 15...

result:

ok Correct.

Test #37:

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

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
39 2
50 15
75 1745
92 12541
94 2865
96 17786
107 54
128 7
131 21
133 124
162 11
178 122
213 18355
221 144
224 1167
240 24491
245 7555
251 200
268 401
269 216
320 242
332 23407
354 5919
372 4128
395 1508
400 2118
402 125
420 299
427 14865
447 63
457 6699
466 4072
488 477
493 11694
503 134
505 236...

result:

ok Correct.

Test #38:

score: 0
Accepted
time: 44ms
memory: 12124kb

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
6 65969
29 923
35 46
38 15
67 55
78 16946
80 5274
86 36
117 52
134 452
155 32
183 1139
186 12890
218 69
228 122
235 2004
255 58752
257 428
266 68381
283 26107
296 3895
301 274
313 79169
316 251
329 141
348 120
381 47174
386 2405
389 184
403 269
408 6297
429 25152
431 4
443 145
468 526
475 88642
...

result:

ok Correct.

Test #39:

score: 0
Accepted
time: 35ms
memory: 12356kb

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
22 19
36 14345
54 69
56 6
62 22256
63 185
99 1367
148 40308
167 89715
204 56554
210 1115
231 1177
307 53171
331 287
338 69766
357 72745
381 50116
385 125
395 4380
425 159
428 398
430 68699
431 2216
437 129
482 3227
501 10652
509 54043
515 7349
519 73
522 8280
541 376
554 16726
580 283
593 525
60...

result:

ok Correct.

Test #40:

score: 0
Accepted
time: 39ms
memory: 12112kb

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
7 3
44 410
54 8261
57 1000
89 39
95 28
112 49
141 24
166 73
190 1358
202 67
208 77
238 60866
243 1819
249 69124
256 44868
258 94
270 32520
273 912
278 63
313 5043
343 563
349 3228
353 236
354 40
372 78087
399 2679
405 54395
408 231
418 4695
429 68263
432 18
438 3297
456 16439
476 333
495 369
500...

result:

ok Correct.

Test #41:

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

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
86 211
101 2630
182 10034
210 2023
235 212
257 5784
274 158
308 23835
328 447
348 223
389 4824
404 10898
416 17844
450 858
482 19323
497 23438
498 283
514 291
532 508
547 1013
549 5206
557 15012
577 11
581 134
592 189
612 187
630 30246
648 28135
673 32489
674 70384
698 255
703 11872
735 248
739 ...

result:

ok Correct.

Test #42:

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

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
10 5
15 14
63 51
89 73370
120 2389
130 40
145 2532
161 69
254 105
272 1095
277 1661
281 567
283 110
291 26655
310 238
326 127
330 78
341 84
361 3385
374 122
428 388
453 721
501 11837
524 2831
551 41807
552 9
582 33591
597 165
598 16978
602 43554
616 427
645 53
646 33205
655 46522
656 316
677 200...

result:

ok Correct.

Test #43:

score: 0
Accepted
time: 1ms
memory: 3592kb

input:

1

output:

YES

result:

ok Correct.