Logo Wy Online Judge

WyOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#465#88. 「NOIP2018」旅行Pigsyy10063ms12536kbC++232.2kb2025-04-24 20:43:592025-04-24 20:44:00

answer

#include "cstdio"
#include "algorithm"
#include "cstring"
#define maxn 500005
int n, m, stck[maxn], top, cir = 1, in[maxn], cnt;
int ans[maxn], to[maxn << 2], s[maxn], idx = 0, len = 0;
int lnk[maxn], nxt[maxn << 1], son[maxn << 1], tot = 1;
bool vis[maxn], flg[maxn];
int num = 0;
inline int read() {
    char ch = getchar();
    int ret = 0, f = 1;

    while (ch > '9' || ch < '0') {
        if (ch == '-')
            f = -1;

        ch = getchar();
    }

    while (ch >= '0' && ch <= '9')
        ret = ret * 10 + ch - '0', ch = getchar();

    return ret * f;
}
inline void add(int x, int y) {
    son[++tot] = y, nxt[tot] = lnk[x], lnk[x] = tot;
}
inline void Trump() {
    memset(flg, 1, sizeof(flg));

    for (int i = 1; i <= n; ++i)
        flg[i] = true;

    top = 0;

    for (int i = 1; i <= n; ++i)
        if (in[i] == 1)
            stck[++top] = i;

    while (top) {
        int now = stck[top--];
        flg[now] = 0, cnt--;

        for (int j = lnk[now]; j; j = nxt[j]) {
            in[son[j]]--;

            if (in[son[j]] == 1)
                stck[++top] = son[j];
        }
    }
}
inline void DFS(int now, int lst, int pre) {
    if (!vis[now]) {
        ans[++idx] = now;

        if (flg[now])
            cnt--;
    }

    vis[now] = 1;
    int l = len + 1, r = len;

    for (int j = lnk[now]; j; j = nxt[j])
        if (!vis[son[j]])
            to[++len] = son[j];

    if (len < l)
        return;

    r = len, len++;
    std::sort(to + l, to + 1 + r);

    for (s[now] = l; s[now] <= r; ++s[now])
        if (s[now] == r) {
            if (flg[to[s[now]]] && flg[now] && flg[pre] && to[s[now]] > lst && cir < 0) {
                cir = 1;
                return;
            }

            DFS(to[s[now]], lst, now);
        } else
            DFS(to[s[now]], to[s[now] + 1], now);
}
int main() {
    n = read(), m = read();

    for (int i = 1; i <= m; ++i) {
        int x = read(), y = read();
        add(x, y), add(y, x);
        in[x]++, in[y]++;
    }

    if (n == m) {
        cir = -1, cnt = n;
        Trump();
        flg[0] = true;
    }

    DFS(1, n + 1, 0);

    for (int i = 1; i <= n; ++i)
        printf("%d ", ans[i]);

    return 0;
}

详细

小提示:点击横条可展开更详细的信息

Test #1:

score: 4
Accepted
time: 1ms
memory: 9752kb

input:

10 9
10 4
1 4
5 7
8 7
2 3
1 6
5 6
1 2
9 1

output:

1 2 3 4 10 6 5 7 8 9 

result:

ok 10 tokens

Test #2:

score: 4
Accepted
time: 0ms
memory: 9660kb

input:

10 9
10 3
4 1
3 2
5 8
7 4
8 6
5 3
1 9
6 1

output:

1 4 7 6 8 5 3 2 10 9 

result:

ok 10 tokens

Test #3:

score: 4
Accepted
time: 1ms
memory: 9748kb

input:

10 9
9 2
3 6
8 5
5 4
5 2
1 3
7 2
8 3
3 10

output:

1 3 6 8 5 2 7 9 4 10 

result:

ok 10 tokens

Test #4:

score: 4
Accepted
time: 1ms
memory: 11720kb

input:

100 99
22 23
27 35
49 42
16 15
70 53
67 39
68 48
43 48
85 94
82 1
25 32
34 78
100 8
87 31
48 28
19 9...

output:

1 10 52 30 84 13 5 92 34 78 65 47 17 79 72 40 89 57 62 96 42 49 46 36 38 48 28 43 88 71 6 66 16 15 9...

result:

ok 100 tokens

Test #5:

score: 4
Accepted
time: 0ms
memory: 9656kb

input:

100 99
76 17
32 78
77 83
40 94
99 6
57 76
90 92
44 80
21 56
92 16
8 48
67 20
55 46
23 28
71 85
67 40...

output:

1 10 49 91 13 80 15 54 81 55 46 39 24 3 22 70 57 76 17 82 25 14 62 53 87 29 51 61 7 52 72 38 27 33 6...

result:

ok 100 tokens

Test #6:

score: 4
Accepted
time: 3ms
memory: 9848kb

input:

1000 999
996 820
765 568
953 412
16 225
215 677
603 774
311 708
74 25
210 116
671 762
638 694
253 58...

output:

1 632 830 800 392 543 661 293 556 643 451 166 864 559 975 765 568 453 838 872 224 736 101 363 211 87...

result:

ok 1000 tokens

Test #7:

score: 4
Accepted
time: 0ms
memory: 9548kb

input:

1000 999
612 946
725 376
207 917
126 713
392 724
540 261
324 644
278 469
107 928
275 586
884 629
110...

output:

1 265 462 307 796 877 554 334 599 78 483 947 431 443 422 972 48 438 327 934 799 450 277 408 513 985 ...

result:

ok 1000 tokens

Test #8:

score: 4
Accepted
time: 3ms
memory: 9712kb

input:

1000 999
476 573
276 592
53 745
213 598
269 164
568 625
732 424
976 908
669 251
604 620
569 1000
45 ...

output:

1 255 453 858 702 175 17 77 57 161 720 105 341 608 970 465 709 778 72 798 621 930 736 483 648 420 21...

result:

ok 1000 tokens

Test #9:

score: 4
Accepted
time: 1ms
memory: 9744kb

input:

1000 999
592 756
473 895
104 801
326 184
724 454
569 958
2 295
450 291
849 65
864 166
873 947
422 87...

output:

1 206 536 209 428 616 617 431 132 567 242 582 615 453 396 923 860 65 620 9 535 8 147 94 683 770 544 ...

result:

ok 1000 tokens

Test #10:

score: 4
Accepted
time: 2ms
memory: 9720kb

input:

1000 999
593 26
9 827
834 643
509 174
350 979
524 118
573 53
324 211
74 883
98 394
248 231
174 599
8...

output:

1 92 695 930 850 144 771 567 122 278 692 482 540 241 453 424 590 74 883 893 75 585 137 605 201 440 5...

result:

ok 1000 tokens

Test #11:

score: 4
Accepted
time: 6ms
memory: 10004kb

input:

5000 4999
3830 670
4927 4630
4497 4511
4491 2516
4182 897
4445 4356
574 2303
2830 1928
4907 4456
300...

output:

1 1085 1471 3897 2453 298 1788 3306 3758 3459 2095 2255 1862 1178 1245 668 4845 3588 2496 1253 1175 ...

result:

ok 5000 tokens

Test #12:

score: 4
Accepted
time: 8ms
memory: 10068kb

input:

5000 4999
2596 3882
4758 4362
1904 1826
986 2286
609 3005
4872 3376
1796 4687
498 888
2520 2085
1925...

output:

1 732 2597 2408 3328 2328 3620 1182 3124 3201 688 1064 652 1315 2114 4262 44 1588 1372 3856 4146 287...

result:

ok 5000 tokens

Test #13:

score: 4
Accepted
time: 2ms
memory: 10072kb

input:

5000 4999
1031 3755
2028 4153
4601 1184
1617 1077
2377 3758
1128 667
168 3060
4590 544
4881 4789
389...

output:

1 1469 378 3917 3735 1721 4995 637 2878 1172 1381 2605 3056 3143 4699 732 2380 3998 3750 913 4439 33...

result:

ok 5000 tokens

Test #14:

score: 4
Accepted
time: 4ms
memory: 10000kb

input:

5000 4999
4139 702
1744 1510
4785 348
214 4322
1193 3762
1441 1663
1601 3283
4860 1397
2628 1246
45 ...

output:

1 466 86 4214 221 2521 2749 3925 2919 2980 4840 4746 3979 3782 91 44 545 4842 4391 3841 3223 169 328...

result:

ok 5000 tokens

Test #15:

score: 4
Accepted
time: 1ms
memory: 9920kb

input:

5000 4999
2072 4218
938 4671
1301 3532
3808 175
177 3580
209 3907
1988 2889
382 4974
3044 1544
1383 ...

output:

1 2735 315 2472 2881 4119 566 96 429 4024 1156 3319 472 1042 3154 871 1526 3947 1801 962 3666 2440 9...

result:

ok 5000 tokens

Test #16:

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

input:

10 10
5 10
4 1
6 2
1 7
9 2
3 8
7 6
9 4
2 10
2 3

output:

1 4 7 6 2 3 8 9 10 5 

result:

ok 10 tokens

Test #17:

score: 4
Accepted
time: 6ms
memory: 12132kb

input:

10 10
10 7
9 4
10 4
10 3
6 10
9 8
3 8
5 8
2 6
10 1

output:

1 10 3 4 9 8 5 6 2 7 

result:

ok 10 tokens

Test #18:

score: 4
Accepted
time: 1ms
memory: 10248kb

input:

100 100
62 23
44 94
39 50
95 83
26 15
4 69
56 66
53 38
79 28
86 70
4 57
87 48
30 29
87 43
78 8
71 28...

output:

1 32 42 21 27 68 89 86 70 20 24 36 62 23 91 38 53 56 66 73 40 78 8 65 51 80 67 82 77 11 60 47 76 49 ...

result:

ok 100 tokens

Test #19:

score: 4
Accepted
time: 1ms
memory: 10264kb

input:

100 100
88 9
60 16
43 75
72 15
91 97
100 1
12 94
89 66
15 32
8 9
96 85
25 64
2 68
24 50
34 67
69 51
...

output:

1 100 30 54 41 59 17 22 58 61 3 76 90 65 10 29 47 14 57 71 72 15 4 20 86 9 8 77 99 88 26 56 81 38 39...

result:

ok 100 tokens

Test #20:

score: 4
Accepted
time: 3ms
memory: 10252kb

input:

1000 1000
405 421
658 212
31 639
194 828
966 741
966 148
525 722
756 765
783 309
483 845
274 638
229...

output:

1 410 137 357 154 555 334 53 902 4 969 225 354 423 962 601 599 889 301 587 569 934 781 948 463 58 97...

result:

ok 1000 tokens

Test #21:

score: 4
Accepted
time: 3ms
memory: 12300kb

input:

1000 1000
49 681
629 565
83 535
871 428
65 667
641 718
448 404
189 162
622 551
621 882
577 676
655 5...

output:

1 427 443 688 671 485 866 527 108 469 490 173 53 755 781 421 518 322 909 54 740 539 559 950 441 155 ...

result:

ok 1000 tokens

Test #22:

score: 4
Accepted
time: 6ms
memory: 10144kb

input:

1000 1000
448 280
864 712
354 223
592 391
914 649
806 850
109 795
680 854
397 109
752 256
506 619
40...

output:

1 721 330 843 657 57 319 469 447 174 714 660 805 119 740 227 965 918 381 21 522 870 961 978 193 485 ...

result:

ok 1000 tokens

Test #23:

score: 4
Accepted
time: 8ms
memory: 10516kb

input:

5000 5000
4523 3510
1892 1078
4722 4812
4199 1974
4961 1671
1686 2332
2227 3899
1286 340
4212 2512
8...

output:

1 889 663 3983 1713 1031 158 3538 4146 2107 2281 2822 1553 4844 1603 479 3642 122 2207 3200 58 4207 ...

result:

ok 5000 tokens

Test #24:

score: 4
Accepted
time: 0ms
memory: 10592kb

input:

5000 5000
4350 1916
775 3638
669 3449
4259 1670
84 912
4308 2199
4727 3080
4711 1781
3622 452
590 42...

output:

1 1122 1335 3161 2089 4285 4270 4812 4273 30 417 3861 1074 1880 4536 340 458 2230 4541 2977 4283 487...

result:

ok 5000 tokens

Test #25:

score: 4
Accepted
time: 2ms
memory: 12536kb

input:

5000 5000
2420 4658
3014 750
1721 61
3469 163
2310 4527
4489 3343
1530 303
2359 883
3696 2308
263 33...

output:

1 51 1233 2964 2290 2736 4233 41 1719 3094 1239 1278 439 3276 1207 3376 4553 1889 3815 2183 2596 65 ...

result:

ok 5000 tokens