注意是爱三角,所以只能有3步。
#include <bits/stdc++.h>
using namespace std;
int a[5005];
int ok=0;
int n;
void dfs(int x)
{int step=0;for(int i=x;a[i]!=0&&step<=n;i=a[i]){//cout<<i<<"->";step++;if(a[i]==x&&step==3){ok=1;// cout<<"ok!"<<endl;break;}else if(a[i]==x){return;}}
}
int main()
{cin>>n;memset(a,0,sizeof(a));for(int i=1;i<=n;i++)scanf("%d",&a[i]);for(int i=1;i<=n;i++){dfs(i);if(ok)break;}if(ok)cout<<"YES"<<endl;else cout<<"NO"<<endl;
}
附上后台的测试数据:
Test: #
1, time:
15 ms., memory:
2028 KB, exit code:
0, checker exit code:
0, verdict:
OK
Input
5 2 4 5 1 3
Output
YES
Answer
YES
Checker Log
ok answer is YES
Test: #
2, time:
15 ms., memory:
2028 KB, exit code:
0, checker exit code:
0, verdict:
OK
Input
5 5 5 5 5 1
Output
NO
Answer
NO
Checker Log
ok answer is NO
Test: #
3, time:
0 ms., memory:
2032 KB, exit code:
0, checker exit code:
0, verdict:
OK
Input
3 3 1 2
Output
YES
Answer
YES
Checker Log
ok answer is YES
Test: #
4, time:
15 ms., memory:
2032 KB, exit code:
0, checker exit code:
0, verdict:
OK
Input
10 4 10 9 5 3 1 5 10 6 4
Output
NO
Answer
NO
Checker Log
ok answer is NO
Test: #
5, time:
0 ms., memory:
2028 KB, exit code:
0, checker exit code:
0, verdict:
OK
Input
10 5 5 4 9 10 9 9 5 3 1
Output
YES
Answer
YES
Checker Log
ok answer is YES
Test: #
6, time:
46 ms., memory:
2028 KB, exit code:
0, checker exit code:
0, verdict:
OK
Input
5000 4403 4460 1216 4380 3235 646 4334 3337 1999 1345 2542 4381 4771 4974 104 3251 9 2600 408 3531 4905 4167 733 3020 4070 2951 468 62 4782 4230 1689 2064 1708 3806 3917 459 881 606 3979 2495 4413 629 3512 872 1144 4394 2442 407 2067 3089 4520 1958 2818 3724 1433 4677 2132 4366 4709 1483 55 297 2529 3636 4300 3265 408 4 403 3502 1509 3874 822 285 472 385 4039 4287 2255 2261 3287 3436 4912 1928 4886 1505 47 2769 4537 4134 2267 1863 1232 157 2162 3558 861 3846 2019 3822 3860 3379 4128 379 1066 3002 202 1766...
Output
NO
Answer
NO
Checker Log
ok answer is NO
Test: #
7, time:
30 ms., memory:
2032 KB, exit code:
0, checker exit code:
0, verdict:
OK
Input
5000 2540 4861 3328 914 3497 4539 1589 418 5 905 1717 64 1907 1787 2769 1684 2249 154 2821 173 3056 4604 1811 3414 332 1900 4020 2883 2689 3939 3952 2043 1717 4257 1213 344 3473 126 2807 672 2402 3341 3281 2170 2615 4276 4541 4827 545 4032 1437 2801 2846 4653 770 1926 2878 249 3369 766 616 3466 1074 1256 4187 4804 75 4710 3915 923 1830 726 4780 1146 1279 1246 500 2650 802 4129 2692 3113 1416 4073 3837 3107 1274 2593 3661 4845 1165 1723 2635 3851 4057 4677 1146 3619 722 1254 23 606 2636 1596 3379 535 2152 ...
Output
YES
Answer
YES
Checker Log
ok answer is YES
Test: #
8, time:
15 ms., memory:
2032 KB, exit code:
0, checker exit code:
0, verdict:
OK
Input
100 50 40 60 87 39 58 44 84 46 68 16 57 77 87 92 95 42 31 74 15 36 84 30 3 47 15 87 90 76 66 6 63 74 19 40 49 6 84 41 9 77 34 7 12 11 73 58 24 81 14 81 29 65 100 1 85 64 32 38 4 54 67 32 81 80 7 100 71 29 80 4 52 47 7 78 56 52 75 81 37 16 41 27 28 58 60 62 47 29 40 37 14 59 91 12 54 25 58 12 43
Output
NO
Answer
NO
Checker Log
ok answer is NO
Test: #
9, time:
0 ms., memory:
2032 KB, exit code:
0, checker exit code:
0, verdict:
OK
Input
1000 701 827 691 591 472 427 946 187 165 183 713 396 720 192 620 740 754 796 854 723 447 123 63 959 833 712 135 618 139 17 74 676 646 894 643 339 817 309 957 800 963 13 383 976 190 505 549 703 837 386 253 525 496 373 247 589 879 840 777 880 836 858 681 29 697 859 969 580 814 10 992 265 22 942 125 291 125 437 51 599 850 361 795 973 667 449 934 101 472 758 774 978 364 319 46 273 32 932 954 718 993 261 650 874 676 236 719 785 510 639 324 42 585 546 181 546 431 544 604 455 948 998 973 541 631 411 487 913 243 ...
Output
NO
Answer
NO
Checker Log
ok answer is NO
Test: #
10, time:
15 ms., memory:
2024 KB, exit code:
0, checker exit code:
0, verdict:
OK
Input
100 25 6 46 37 87 99 70 31 46 12 94 40 87 56 28 8 94 39 13 12 67 13 71 39 83 48 40 14 62 41 16 71 20 41 83 41 68 98 23 82 62 83 62 35 49 22 31 21 66 98 54 39 34 52 11 28 47 89 25 44 68 36 91 46 82 86 88 48 27 93 7 9 53 36 16 100 84 84 44 25 58 66 16 46 72 21 91 78 4 17 44 17 47 67 93 89 75 44 56 50
Output
YES
Answer
YES
Checker Log
ok answer is YES
Test: #
11, time:
15 ms., memory:
2036 KB, exit code:
0, checker exit code:
0, verdict:
OK
Input
1000 687 753 86 72 52 116 644 70 228 922 269 68 655 863 368 80 979 565 579 167 120 1000 842 239 764 787 772 228 168 618 372 720 836 947 208 786 578 264 412 460 735 620 493 631 528 86 246 45 259 737 995 162 473 708 512 674 995 375 385 868 728 685 108 560 780 190 567 421 316 505 805 103 514 315 741 548 29 861 961 200 25 67 190 850 89 887 578 549 846 546 119 762 686 78 350 346 751 901 758 46 543 443 251 273 94 679 20 398 258 387 729 432 620 68 522 603 701 545 911 446 610 793 150 953 462 499 450 353 412 325 8...
Output
YES
Answer
YES
Checker Log
ok answer is YES
Test: #
12, time:
15 ms., memory:
2028 KB, exit code:
0, checker exit code:
0, verdict:
OK
Input
5000 2155 4100 2555 4634 288 3951 3181 2686 2140 1681 2640 946 3027 703 3126 1152 1621 2680 4856 1940 4563 3557 3569 4869 838 867 3911 87 804 4273 4212 367 1908 3312 2151 103 2163 3329 4500 2522 4275 2081 1266 4402 2078 3512 225 3800 1603 3863 4819 1631 1300 2073 438 3294 2122 3045 3152 3460 1727 1032 676 2746 4841 1280 968 3554 3099 569 80 4591 4892 2856 450 1 2913 1854 3037 4512 218 2321 2708 2917 1235 2829 4171 2708 1946 304 4275 2177 2334 16 4372 4671 3657 3009 2908 1828 1434 4987 2997 2929 4579 498 3...
Output
YES
Answer
YES
Checker Log
ok answer is YES
Test: #
13, time:
15 ms., memory:
2028 KB, exit code:
0, checker exit code:
0, verdict:
OK
Input
2 2 1
Output
NO
Answer
NO
Checker Log
ok answer is NO
Test: #
14, time:
15 ms., memory:
2040 KB, exit code:
0, checker exit code:
0, verdict:
OK
Input
3 2 3 1
Output
YES
Answer
YES
Checker Log
ok answer is YES
Test: #
15, time:
0 ms., memory:
2024 KB, exit code:
0, checker exit code:
0, verdict:
OK
Input
5 2 1 4 5 3
Output
YES
Answer
YES
Checker Log
ok answer is YES
Test: #
16, time:
0 ms., memory:
2028 KB, exit code:
0, checker exit code:
0, verdict:
OK
Input
5 5 4 5 5 2
Output
YES
Answer
YES
Checker Log
ok answer is YES