QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#219115#6396. Puzzle: KusabizyoobnWA 34ms9972kbC++204.5kb2023-10-19 01:49:122023-10-19 01:49:12

Judging History

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

  • [2023-10-19 01:49:12]
  • 评测
  • 测评结果:WA
  • 用时:34ms
  • 内存:9972kb
  • [2023-10-19 01:49:12]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

using i64 = long long;

void solve() {
    int n;
    cin >> n;
    vector<int> a(n, -1);
    vector g(n, vector<int>());
    for (int i = 1; i < n; ++i) {
        int u, v;
        string s;
        cin >> u >> v >> s;
        --u, --v;
        g[v].push_back(u);
        if (s == "Tong") {
            a[u] = 0;
        } else if (s == "Duan") {
            a[u] = 1;
        } else if (s == "Chang") {
            a[u] = 2; 
        }
    }

    bool ok = true;
    vector<int> dep(n);
    vector<array<int, 2>> ans;

    auto dfs = [&] (auto dfs, int u) -> array<int, 2> {
        if (!ok) {
            cout << "NO1" << '\n'; exit(0);
            return {-1, -1};
        }
        vector f(3, vector<int>());
        if (a[u] != -1) {
            f[a[u]].push_back(u);
        }
        for (auto &v : g[u]) {
            dep[v] = dep[u] + 1;
            auto [x, y] = dfs(dfs, v);
            if (!ok) {
            cout << "NO2" << '\n'; exit(0);

                return {-1, -1};
            }
            if (x == -1) {
               continue; 
            }
            f[x].push_back(y);
        }
        bool have = false;
        sort(f[0].begin(), f[0].end(), [&] (auto &x, auto &y) {
            return dep[x] < dep[y];
        });
        array<int, 2> res{-1, -1};
        for (int i = 0; i < f[0].size(); ++i) {
            if (i + 1 >= f[0].size()) {
                if (!have) {
                    have = true;
                    res = {0, f[0][i]};
                } else {
            cout << "NO3" << '\n'; exit(0);
                    ok = false;
                    return {-1, -1};
                }
            } else {
                if (dep[f[0][i]] == dep[f[0][i + 1]]) {
                    ans.push_back({f[0][i], f[0][i + 1]});
                    ++i;
                } else {
                    if (!have) {
                        have = true;
                        res = {0, f[0][i]};
                    } else {
            cout << "NO4" << '\n'; exit(0);
                        ok = false;
                        return {-1, -1};
                    }
                }
            }
        }

        if (abs((int)f[1].size() - (int)f[2].size()) >= 2) {
            cout << "NO5" << '\n'; exit(0);
            ok = false;
            return {-1, -1};
        }

        if (f[1].size() >= f[2].size()) {
            sort(f[1].begin(), f[1].end(), [&] (auto &x, auto &y) {
                return dep[x] > dep[y];
            });
            sort(f[2].begin(), f[2].end(), [&] (auto &x, auto &y) {
                return dep[x] > dep[y];
            });
        } else {
            sort(f[1].begin(), f[1].end(), [&] (auto &x, auto &y) {
                return dep[x] < dep[y];
            });
            sort(f[2].begin(), f[2].end(), [&] (auto &x, auto &y) {
                return dep[x] < dep[y];
            });
        }

        int l = 0, r = 0;
        while (l < f[1].size() && r < f[2].size()) {
            int x = f[1][l], y = f[2][r];
            if (dep[x] < dep[y]) {
                ans.push_back({x, y});
            } else {
                if (!have) {
                    have = true;
                    if (f[1].size() > f[2].size()) {
                        res = {1, x};
                        --r;
                    } else if (f[1].size() < f[2].size()) {
                        res = {2, y};
                        --l;
                    } else {
            cout << "NO6" << '\n'; exit(0);
                        ok = false;
                        return {-1, -1};
                    }
                } else {
            cout << "NO7" << '\n'; exit(0);
                    ok = false;
                    return {-1, -1};
                }
            }
            ++l, ++r;
        }
        if (!have) {
            if (f[1].size() > f[2].size()) {
                res = {1, f[1][l]};
            } else if (f[1].size() < f[2].size()) {
                res = {2, f[2][r]};
            }
        }
        
        return res;
    };

    auto [x, y] = dfs(dfs, 0);
    
    if (x != -1 || !ok) {
        cout << "NO" << '\n';
        return;
    }
    
    cout << "YES" << '\n';
    
    for (auto &[u, v] : ans) {
        cout << u + 1 << ' ' << v + 1 << '\n';
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int t = 1;
    // cin >> t;
    while(t--) {
        solve();
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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

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

result:

ok Correct.

Test #3:

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

input:

2
2 1 Tong

output:

NO

result:

ok Correct.

Test #4:

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

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

result:

ok Correct.

Test #5:

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

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
5203 28541
8106 5981
7551 7900
47998 53424
91669 86909
72002 40295
32334 65376
57528 95652
66693 91216
88445 98194
44959 54118
74202 76761
64661 71470
30271 85084
41192 60344
10342 41421
12876 79425
25933 35989
39297 41959
46303 94979
10581 46189
14956 51797
41806 82599
26090 60566
48923 94132
6...

result:

ok Correct.

Test #6:

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

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
19 20
55 65
52 53
46 48
35 36
22 34
58 61
56 57
1097 1206
1522 1593
1890 1938
1217 1365
1191 1295
2158 2188
1764 2845
1408 1646
3147 5721
2746 2810
2937 3194
2399 2710
3657 3778
2668 3580
2179 2259
2122 2174
2204 2379
2392 2449
2218 2499
2233 2369
2020 2077
8244 9174
14498 15184
14362 15193
1040...

result:

ok Correct.

Test #7:

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

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
81 85
105 107
103 117
168 172
273 275
321 333
369 384
314 327
433 463
429 437
543 588
667 639
1167 1273
1160 1375
1117 1176
995 1021
926 929
832 1059
1196 1242
1328 1329
1254 1259
1194 1213
1150 1275
1521 1689
1424 1620
1226 1229
1134 1277
915 1099
1218 1237
1316 1358
1284 1350
2462 2524
2128 23...

result:

ok Correct.

Test #8:

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

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
68 69
118 120
227 237
221 225
265 266
314 323
304 309
343 346
345 352
338 341
347 350
392 401
436 439
469 471
473 474
463 472
480 482
495 499
477 483
567 570
604 613
584 588
576 577
738 744
739 743
730 732
848 851
809 841
881 886
911 930
947 951
1007 1029
964 1002
946 958
914 919
939 942
952 959...

result:

ok Correct.

Test #9:

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

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
238 239
272 274
343 344
415 416
432 433
477 478
512 515
539 544
579 582
631 633
700 711
717 724
719 730
784 787
780 782
772 773
777 783
830 831
824 821
810 816
845 849
858 860
868 872
895 897
876 894
873 871
929 936
986 999
981 984
976 980
964 966
950 955
958 967
968 977
996 997
993 995
1038 103...

result:

ok Correct.

Test #10:

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

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
4915 4986
6570 6738
13597 14195
22019 22308
31792 31800
38225 38186
43803 43898
41995 41864
42327 42725
43807 43871
43542 43953
46356 46361
47608 47755
46932 47521
44955 46885
48486 48399
49839 50116
49668 49912
53355 54823
54273 54714
53853 54239
52524 52947
55640 57456
58053 58348
59228 59658
...

result:

ok Correct.

Test #11:

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

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
34242 34406
14906 23870
28768 79207
50509 68052
36687 70523
32304 70759
36153 68751
3567 47669
18177 70173
50978 58218
56574 27598
6904 79895
47073 46683
4139 85751
17677 83196
65881 90916
44928 67031
25922 49996
28068 95211
43712 69374
19260 84697
32128 70765
30451 79850
12144 92289
51006 99611...

result:

ok Correct.

Test #12:

score: 0
Accepted
time: 14ms
memory: 7404kb

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
15102 66302
37177 40113
67735 67876
70745 98880
75547 84740
88398 71830
62593 85699
6455 71970
21755 94458
39981 74239
13515 71421
37672 71660
90862 91238
19470 42744
13573 72747
10218 81223
15063 87004
4280 92721
6427 19357
68085 44673
17731 33161
358 45357
49326 65914
45470 93622
35687 90190
1...

result:

ok Correct.

Test #13:

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

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
63894 95158
88697 88086
85544 75216
73765 72642
72241 69085
14063 61983
55203 54430
50491 41588
40110 38849
34955 27596
1136 15274
10786 18595
21493 24025
25178 30164
30800 40282
50699 52476
59910 76452
79238 86389
91441 96792
1197 30463
34098 46043
47433 49151
56601 57549
59254 65754
66989 8754...

result:

ok Correct.

Test #14:

score: 0
Accepted
time: 14ms
memory: 7320kb

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
51260 62964
64799 73003
87232 98672
31245 38059
47626 58383
78385 82570
84269 91105
67893 90700
95599 97507
51711 96567
2480 84425
90521 97467
3614 97387
52378 20327
24851 31941
34768 35502
36133 37919
38081 41305
46820 47134
51834 18144
52602 53825
60150 63344
65516 68450
79862 84495
88745 9084...

result:

ok Correct.

Test #15:

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

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
51853 97806
89831 81679
70717 62015
60751 59449
56321 54934
54125 53261
24271 49313
49018 46496
43803 39709
39458 38753
38738 38484
37384 32530
58304 99744
99690 97190
93135 88437
76662 73977
73029 71317
68370 61172
61007 58424
18859 57761
57280 55259
54712 46485
44356 43694
39921 34688
33247 30...

result:

ok Correct.

Test #16:

score: 0
Accepted
time: 20ms
memory: 7264kb

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
336 82705
60220 72014
80211 95022
93125 94029
59250 85950
77242 89854
56243 61755
80147 86382
86818 91748
408 37709
87876 89771
452 83493
481 97158
614 92488
824 95750
30379 31237
31218 31062
31048 30964
30860 30850
30689 30666
30642 30487
31450 30316
30298 30147
29890 29748
29745 29652
29648 29...

result:

ok Correct.

Test #17:

score: -100
Wrong Answer
time: 17ms
memory: 8432kb

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:

NO5

result:

wrong answer YES or NO expected in answer, but NO5 found.