{"id":369857,"date":"2024-05-21T04:12:36","date_gmt":"2024-05-21T04:12:36","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=369857"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=369857","title":{"rendered":"<span>\u0428\u043f\u0430\u0440\u0433\u0430\u043b\u043a\u0430 \u0434\u043b\u044f \u0430\u043b\u0433\u043e\u0441\u043e\u0431\u0435\u0441\u0430 2 \u2014 \u0433\u0440\u0430\u0444\u043e\u0432\u044b\u0435 \u0438 \u0441\u0442\u0440\u043e\u043a\u043e\u0432\u044b\u0435 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u044b<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041f\u0440\u0438\u0432\u0435\u0442, \u0425\u0430\u0431\u0440!<\/p>\n<p>\u0412 \u043d\u0430\u0448\u0435 \u043d\u0435\u0441\u043f\u043e\u043a\u043e\u0439\u043d\u043e\u0435 \u0432\u0440\u0435\u043c\u044f, \u043a\u043e\u0433\u0434\u0430 \u0441\u0444\u0435\u0440\u0430 AI \u0441\u0442\u0440\u0435\u043c\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0434\u0432\u0438\u0436\u0435\u0442\u0441\u044f \u0432\u043f\u0435\u0440\u0451\u0434, \u0445\u043e\u0447\u0435\u0442\u0441\u044f \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0441\u0442\u0430\u0431\u0438\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0438 \u0443\u0432\u0435\u0440\u0435\u043d\u043d\u043e\u0441\u0442\u0438 \u0432 \u0437\u0430\u0432\u0442\u0440\u0430\u0448\u043d\u0435\u043c \u0434\u043d\u0435. \u041a\u0430\u043a \u044d\u0442\u043e \u0441\u0432\u044f\u0437\u0430\u043d\u043e \u0441 \u0442\u0435\u043c\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0438?<\/p>\n<p>\u0421\u0430\u043c\u044b\u043c \u043f\u0440\u044f\u043c\u044b\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u2014 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u044b \u043f\u0440\u043e\u043a\u0430\u0447\u0438\u0432\u0430\u044e\u0442 \u0443\u043c\u0435\u043d\u0438\u0435 \u0441\u0438\u0441\u0442\u0435\u043c\u043d\u043e \u043c\u044b\u0441\u043b\u0438\u0442\u044c, \u0438\u0441\u043a\u0430\u0442\u044c \u043d\u0435\u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0435 \u043f\u0443\u0442\u0438 \u0440\u0435\u0448\u0435\u043d\u0438\u044f; \u0447\u0435\u043b\u043e\u0432\u0435\u043a\u0430, \u0443 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u044d\u0442\u0438 \u0441\u043a\u0438\u043b\u043b\u044b \u043d\u0430 \u0432\u044b\u0441\u043e\u0442\u0435, \u0432\u0440\u044f\u0434 \u043b\u0438 \u0437\u0430\u043c\u0435\u043d\u0438\u0442 \u0418\u0418. \u0422\u0430\u043a \u0447\u0442\u043e, \u0442\u0440\u0430\u0442\u044f \u0431\u0435\u0441\u0446\u0435\u043d\u043d\u043e\u0435 \u0432\u0440\u0435\u043c\u044f \u043d\u0430 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u044b, \u0432\u044b \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442\u0435\u0441\u044c \u043e\u0447\u0435\u043d\u044c \u043f\u043e\u043b\u0435\u0437\u043d\u044b\u043c \u0434\u0435\u043b\u043e\u043c, \u0440\u0430\u0441\u0441\u043b\u0430\u0431\u044c\u0442\u0435\u0441\u044c \u0438 \u043f\u043e\u043b\u0443\u0447\u0430\u0439\u0442\u0435 \u0443\u0434\u043e\u0432\u043e\u043b\u044c\u0441\u0442\u0432\u0438\u0435) \u0412 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0431\u043e\u043d\u0443\u0441\u0430 \u043a \u043f\u0440\u043e\u043a\u0430\u0447\u0438\u0432\u0430\u043d\u0438\u044e \u0441\u0435\u0440\u043e\u0433\u043e \u0432\u0435\u0449\u0435\u0441\u0442\u0432\u0430 \u0432\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u0435 \u0441\u0432\u0435\u0440\u0445\u0441\u043f\u043e\u0441\u043e\u0431\u043d\u043e\u0441\u0442\u044c \u043f\u0440\u043e\u0439\u0442\u0438 \u043b\u044e\u0431\u043e\u0439 \u0430\u043b\u0433\u043e\u0441\u043e\u0431\u0435\u0441 \u0432 FAANG \u0438 \u0443\u0434\u043e\u0432\u043b\u0435\u0442\u0432\u043e\u0440\u0438\u0442\u044c \u043b\u044e\u0431\u044b\u0435 \u043f\u043e\u0442\u0440\u0435\u0431\u043d\u043e\u0441\u0442\u0438 \u043f\u0438\u0440\u0430\u043c\u0438\u0434\u044b \u041c\u0430\u0441\u043b\u043e\u0443 \u2014 \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u043f\u0440\u0438\u044f\u0442\u043d\u043e)<\/p>\n<p>\u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u043c\u044b \u0440\u0430\u0437\u0431\u0435\u0440\u0451\u043c \u0433\u0440\u0430\u0444\u043e\u0432\u044b\u0435 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u044b \u0442\u0438\u043f\u043e DFS, \u0424\u043b\u043e\u0439\u0434\u0430\u2013\u0423\u043e\u0440\u0448\u0435\u043b\u043b\u0430 \u0438 \u0441\u0442\u0440\u043e\u043a\u043e\u0432\u044b\u0435 \u043d\u0430\u043f\u043e\u0434\u043e\u0431\u0438\u0435 \u0410\u0445\u043e-\u041a\u043e\u0440\u0430\u0441\u0438\u043a.<\/p>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/38d\/ec5\/fab\/38dec5fab0e644d5f9f438d448e85b58.gif\" width=\"1\" height=\"1\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/38d\/ec5\/fab\/38dec5fab0e644d5f9f438d448e85b58.gif\"\/><\/figure>\n<p>\u042d\u0442\u043e 2 \u0447\u0430\u0441\u0442\u044c \u0440\u0430\u0437\u0431\u043e\u0440\u0430 \u0441\u0430\u043c\u044b\u0445 \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0445 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u043e\u0432, \u0430 \u0432\u043e\u0442 1 \u0447\u0430\u0441\u0442\u044c: <a href=\"https:\/\/habr.com\/ru\/articles\/794556\/\" rel=\"noopener noreferrer nofollow\">\u00ab\u0428\u043f\u0430\u0440\u0433\u0430\u043b\u043a\u0430 \u0434\u043b\u044f \u0430\u043b\u0433\u043e\u0441\u043e\u0431\u0435\u0441\u0430 \u2014 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c, \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0434\u0430\u043d\u043d\u044b\u0445, \u043c\u0435\u0442\u043e\u0434\u044b \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u0438 \u0414\u0435\u0439\u043a\u0441\u0442\u0440\u0430\u00bb<\/a><\/p>\n<p>\u041f\u0440\u0438\u0432\u0435\u0434\u0443 \u0435\u0449\u0451 \u0440\u0430\u0437 \u0442\u0443 \u0441\u0445\u0435\u043c\u0443 \u0441\u043e \u0432\u0441\u0435\u043c\u0438 \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u043c\u0438 \u043f\u043e\u043d\u044f\u0442\u0438\u044f\u043c\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u043e\u0433\u0443\u0442 \u0432\u0441\u043f\u043b\u044b\u0442\u044c \u043d\u0430 \u0441\u0435\u043a\u0446\u0438\u0438 &#171;\u0410\u043b\u0433\u043e\u0440\u0442\u0438\u043c\u044b&#187;:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w780q1\/getpro\/habr\/upload_files\/9be\/f1a\/332\/9bef1a33269673ce72905152cd9afbc8.jpeg\" width=\"3764\" height=\"20510\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/9be\/f1a\/332\/9bef1a33269673ce72905152cd9afbc8.jpeg\" data-blurred=\"true\"\/><\/figure>\n<p>\u042d\u0442\u0430 \u0441\u0445\u0435\u043c\u0430 \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u043c\u043e\u0447\u044c \u0432\u0430\u043c \u0441\u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0432 \u0442\u0435\u043c\u0435 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u043e\u0432.<\/p>\n<p><em>\u0420\u0430\u0437\u0431\u043e\u0440 \u043e\u0433\u0440\u043e\u043c\u043d\u043e\u0433\u043e \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0430 \u0432\u043e\u043f\u0440\u043e\u0441\u043e\u0432 \u0441 \u0441\u043e\u0431\u0435\u0441\u043e\u0432\u00a0\u0438 \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e <\/em><a href=\"https:\/\/t.me\/+8DmvLCXF5FZkNGQy\" rel=\"noopener noreferrer nofollow\">\u0432 \u043c\u043e\u0435\u043c \u043a\u0430\u043d\u0430\u043b\u0435<\/a>, \u0432\u043e\u043f\u0440\u043e\u0441\u044b \u043f\u043e <a href=\"https:\/\/t.me\/+5eGt0MFT8opkZjcy\" rel=\"noopener noreferrer nofollow\">\u043c\u0430\u0448\u0438\u043d\u043d\u043e\u043c\u0443 \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044e, <\/a>\u0430 \u0437\u0434\u0435\u0441\u044c \u0441\u043e\u0431\u0440\u0430\u043d\u0430<em> <\/em><a href=\"https:\/\/t.me\/addlist\/8vDUwYRGujRmZjFi\" rel=\"noopener noreferrer nofollow\"><em>\u0446\u0435\u043b\u0430\u044f \u043f\u0430\u043f\u043a\u0430\u00a0<\/em><\/a><em>\u0441 \u043a\u0430\u043d\u0430\u043b\u0430\u043c\u0438 \u0434\u043b\u044f \u043f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u043a\u0438 \u043a \u0441\u043e\u0431\u0435\u0441\u0435\u0434\u043e\u0432\u0430\u043d\u0438\u044f\u043c.<\/em><\/p>\n<p>\u041a\u0441\u0442\u0430\u0442\u0438, \u0442\u0443\u0442 \u0431\u0443\u043a\u0432\u0430\u043b\u044c\u043d\u043e \u043d\u0430 \u0434\u043d\u044f\u0445 \u0432\u044b\u0448\u0435\u043b \u0434\u0438\u0432\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a\u043e\u0432\u0438\u043a <a href=\"https:\/\/explorer.globe.engineer\/?q=%D0%B0%D0%BB%D0%B3%D0%BE%D1%80%D0%B8%D1%82%D0%BC%D1%8B+computer+science\" rel=\"noopener noreferrer nofollow\">explorer.globe.engineer<\/a>, \u043e\u043d \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u043d\u0430 \u043f\u043e\u0438\u0441\u043a\u0435 \u043f\u043e \u0433\u0440\u0430\u0444\u0438\u043a\u0430\u043c \u0438 \u0441\u0445\u0435\u043c\u0430\u043c, \u0430 \u0437\u0430\u043e\u0434\u043d\u043e \u043f\u044b\u0442\u0430\u0435\u0442\u0441\u044f \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432\u044b\u0434\u0430\u0447\u0443 \u2014 \u0442\u0430\u043a \u0447\u0442\u043e \u043c\u043e\u0436\u043d\u043e \u0431\u0435\u0437 \u043f\u0440\u043e\u0431\u043b\u0435\u043c \u043d\u0430\u0440\u0443\u043b\u0438\u0442\u044c \u0441\u0435\u0431\u0435 \u043d\u0435\u043f\u043b\u043e\u0445\u0438\u0435 \u0432\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043a\u0430\u043a\u043e\u0433\u043e-\u043d\u0438\u0431\u0443\u0434\u044c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430. \u0417\u0430\u0446\u0435\u043d\u0438\u0442\u0435, \u043a\u0430\u043a \u043a\u0440\u0443\u0442\u043e:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/324\/c95\/6f8\/324c956f8b0476f0e9535611a90c4b82.gif\" width=\"1366\" height=\"744\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/324\/c95\/6f8\/324c956f8b0476f0e9535611a90c4b82.gif\"\/><\/figure>\n<p>\u041b\u0430\u0434\u043d\u043e, \u043f\u043e\u0435\u0445\u0430\u043b\u0438 \u0443\u0436\u0435 \u043a \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430\u043c!<\/p>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/4fd\/35a\/df9\/4fd35adf93acb82a6a5f4477a11697b8.gif\" width=\"1\" height=\"1\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/4fd\/35a\/df9\/4fd35adf93acb82a6a5f4477a11697b8.gif\"\/><\/figure>\n<h2>\u0413\u0440\u0430\u0444\u043e\u0432\u044b\u0435 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u044b<\/h2>\n<p>\u041d\u0438\u0436\u0435 \u044d\u0442\u043e \u0443 \u043d\u0430\u0441 \u043f\u0440\u043e\u0441\u0442\u044b\u0435 (\u0431\u0435\u0437 \u043f\u0435\u0442\u0435\u043b\u044c \u0438 \u043a\u0440\u0430\u0442\u043d\u044b\u0445 \u0440\u0451\u0431\u0435\u0440) \u043d\u0435\u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u0433\u0440\u0430\u0444\u044b:<\/p>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/a80\/cd3\/cba\/a80cd3cba732212325d7419d388f0c3d.gif\" width=\"507\" height=\"239\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/a80\/cd3\/cba\/a80cd3cba732212325d7419d388f0c3d.gif\"\/><\/figure>\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u043c\u0441\u044f \u0441 \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u043c\u0438 \u043f\u043e\u043d\u044f\u0442\u0438\u044f\u043c\u0438, \u043c\u0430\u043b\u043e \u043b\u0438, \u043c\u043e\u0436\u0435\u0442 \u043a\u0442\u043e \u043d\u0435 \u0432 \u0442\u0435\u043c\u0435:<\/p>\n<ul>\n<li>\n<p><strong>\u041f\u0443\u0442\u044c<\/strong> \u2013 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0432\u0435\u0440\u0448\u0438\u043d, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043a\u0430\u0436\u0434\u0430\u044f \u0432\u0435\u0440\u0448\u0438\u043d\u0430 \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0430 \u0441\u043e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u043e\u0439 \u0440\u0435\u0431\u0440\u043e\u043c. \u041d\u0430 \u0420\u0438\u0441. 1 \u043a\u0440\u0430\u0441\u043d\u044b\u043c\u0438 \u0441\u0442\u0440\u0435\u043b\u043a\u0430\u043c\u0438 \u043f\u043e\u043a\u0430\u0437\u0430\u043d \u043f\u0443\u0442\u044c \u043e\u0442 \u0432\u0435\u0440\u0448\u0438\u043d\u044b 1 \u043a \u0432\u0435\u0440\u0435\u0448\u0438\u043d\u0435 3 \u0447\u0435\u0440\u0435\u0437 \u0432\u0435\u0440\u0448\u0438\u043d\u0443 4.<\/p>\n<\/li>\n<li>\n<p><strong>\u041f\u043e\u0440\u044f\u0434\u043e\u043a<\/strong> \u2013 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0432\u0435\u0440\u0448\u0438\u043d \u0433\u0440\u0430\u0444\u0430.<\/p>\n<\/li>\n<li>\n<p><strong>\u0420\u0430\u0437\u043c\u0435\u0440<\/strong> \u2013 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0440\u0435\u0431\u0435\u0440 \u0433\u0440\u0430\u0444\u0430.<\/p>\n<\/li>\n<li>\n<p><strong>\u0421\u0432\u044f\u0437\u043d\u043e\u0441\u0442\u044c (\u0441\u0442\u0435\u043f\u0435\u043d\u044c \u0441\u0432\u044f\u0437\u043d\u043e\u0441\u0442\u0438) \u0432\u0435\u0440\u0448\u0438\u043d\u044b<\/strong> \u2013 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0440\u0435\u0431\u0435\u0440, \u0438\u0441\u0445\u043e\u0434\u044f\u0449\u0438\u0445 \u0438\u0437 \u044d\u0442\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b.<\/p>\n<\/li>\n<li>\n<p><strong>\u0418\u0437\u043e\u043b\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u0430\u044f \u0432\u0435\u0440\u0448\u0438\u043d\u0430<\/strong> \u2013 \u0432\u0435\u0440\u0448\u0438\u043d\u0430, \u043d\u0435 \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u0430\u044f \u0441 \u0434\u0440\u0443\u0433\u0438\u043c\u0438 \u0432\u0435\u0440\u0448\u0438\u043d\u0430\u043c\u0438 \u0433\u0440\u0430\u0444\u0430.<\/p>\n<\/li>\n<li>\n<p><strong>\u0426\u0438\u043a\u043b (\u043f\u0435\u0442\u043b\u044f)<\/strong> \u2013 \u0440\u0435\u0431\u0440\u043e \u043e\u0442 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u043a \u044d\u0442\u043e\u0439 \u0436\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u0435.<\/p>\n<\/li>\n<li>\n<p><strong>\u041e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0433\u0440\u0430\u0444<\/strong> \u2013 \u0433\u0440\u0430\u0444, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0432\u0441\u0435 \u0440\u0435\u0431\u0440\u0430 \u0438\u043c\u0435\u044e\u0442 \u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435, \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0449\u0435\u0435, \u043a\u0430\u043a\u0430\u044f \u0438\u0437 \u0432\u0435\u0440\u0448\u0438\u043d \u0440\u0435\u0431\u0440\u0430 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0439, \u0430 \u043a\u0430\u043a\u0430\u044f \u2013 \u043a\u043e\u043d\u0435\u0447\u043d\u043e\u0439.<\/p>\n<\/li>\n<li>\n<p><strong>\u041d\u0435\u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0433\u0440\u0430\u0444<\/strong> \u2013 \u0433\u0440\u0430\u0444, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0443 \u0440\u0435\u0431\u0435\u0440 \u043d\u0435\u0442 \u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0439.<\/p>\n<\/li>\n<li>\n<p><strong>\u0412\u0437\u0432\u0435\u0448\u0435\u043d\u043d\u044b\u0439 \u0433\u0440\u0430\u0444<\/strong> \u2013 \u0432\u0441\u0435 \u0440\u0435\u0431\u0440\u0430 \u0433\u0440\u0430\u0444\u0430 \u0438\u043c\u0435\u044e\u0442 \u0432\u0435\u0441\u0430.<\/p>\n<\/li>\n<li>\n<p><strong>\u041d\u0435\u0432\u0437\u0432\u0435\u0448\u0435\u043d\u043d\u044b\u0439 \u0433\u0440\u0430\u0444<\/strong> \u2013 \u0440\u0435\u0431\u0440\u0430 \u0433\u0440\u0430\u0444\u0430 \u043d\u0435 \u0438\u043c\u0435\u044e\u0442 \u0432\u0435\u0441\u043e\u0432.<\/p>\n<\/li>\n<\/ul>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/83a\/09d\/fa0\/83a09dfa0e3974876bdead7f231e3e4b.png\" width=\"2400\" height=\"878\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/83a\/09d\/fa0\/83a09dfa0e3974876bdead7f231e3e4b.png\"\/><\/figure>\n<p>\u0413\u0440\u0430\u0444 \u043d\u0430\u0437\u044b\u0432\u0430\u044e\u0442 <em>\u0430\u0446\u0438\u043a\u043b\u0438\u0447\u043d\u044b\u043c<\/em>, \u0435\u0441\u043b\u0438 \u0432 \u043d\u0435\u043c \u043d\u0435\u0442 \u043f\u0440\u043e\u0441\u0442\u044b\u0445 \u0446\u0438\u043a\u043b\u043e\u0432.<\/p>\n<p>\u0410\u0446\u0438\u043a\u043b\u0438\u0447\u043d\u044b\u0439 \u043d\u0435\u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0433\u0440\u0430\u0444 \u043d\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f <em>\u043b\u0435\u0441\u043e\u043c<\/em>. \u0421\u0432\u044f\u0437\u043d\u044b\u0439 \u043b\u0435\u0441 \u043d\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f <em>\u0434\u0435\u0440\u0435\u0432\u043e\u043c<\/em>. \u0423 \u0434\u0435\u0440\u0435\u0432\u044c\u0435\u0432 \u0435\u0441\u0442\u044c \u0434\u0440\u0443\u0433\u0438\u0435 \u044d\u043a\u0432\u0438\u0432\u0430\u043b\u0435\u043d\u0442\u043d\u044b\u0435 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f:<\/p>\n<ul>\n<li>\n<p>\u0413\u0440\u0430\u0444 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0434\u0435\u0440\u0435\u0432\u043e\u043c, \u0435\u0441\u043b\u0438 \u0432 \u043d\u0435\u043c <em>n<\/em> \u0432\u0435\u0440\u0448\u0438\u043d \u0438 (<em>n<\/em>\u22121) \u0440\u0435\u0431\u0435\u0440 \u0438 \u043d\u0435\u0442 \u0446\u0438\u043a\u043b\u043e\u0432.<\/p>\n<\/li>\n<li>\n<p>\u0413\u0440\u0430\u0444 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0434\u0435\u0440\u0435\u0432\u043e\u043c, \u0435\u0441\u043b\u0438 \u0438\u0437 \u043b\u044e\u0431\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u043c\u043e\u0436\u043d\u043e \u0434\u043e\u0439\u0442\u0438 \u0432 \u043b\u044e\u0431\u0443\u044e \u0434\u0440\u0443\u0433\u0443\u044e \u0435\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c.<\/p>\n<\/li>\n<\/ul>\n<p>\u0414\u0435\u0440\u0435\u0432\u043e \u043d\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043a\u043e\u0440\u043d\u0435\u0432\u044b\u043c, \u0435\u0441\u043b\u0438 \u0443 \u043d\u0435\u0433\u043e \u0435\u0441\u0442\u044c \u0440\u043e\u0432\u043d\u043e 1 \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u0430\u044f \u0432\u0435\u0440\u0448\u0438\u043d\u0430, \u043d\u0430\u0437\u044b\u0432\u0430\u0435\u043c\u0430\u044f \u043a\u043e\u0440\u043d\u0435\u043c. \u0427\u0430\u0441\u0442\u043e \u043f\u043e\u0434\u0440\u0430\u0437\u0443\u043c\u0435\u0432\u0430\u0435\u0442\u0441\u044f, \u0447\u0442\u043e \u0440\u0435\u0431\u0440\u0430 \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 \u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u044b \u0442\u0430\u043a, \u0447\u0442\u043e \u043e\u0442 \u043a\u043e\u0440\u043d\u044f \u043c\u043e\u0436\u043d\u043e \u0434\u043e\u0439\u0442\u0438 \u043e\u0442 \u0432\u0441\u0435\u0445 \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0445 \u0432\u0435\u0440\u0448\u0438\u043d.<\/p>\n<p>\u0413\u0440\u0430\u0444 \u043d\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043f\u043b\u0430\u043d\u0430\u0440\u043d\u044b\u043c, \u0435\u0441\u043b\u0438 \u0435\u0433\u043e \u043c\u043e\u0436\u043d\u043e \u0440\u0430\u0437\u043b\u043e\u0436\u0438\u0442\u044c \u043d\u0430 \u043f\u043b\u043e\u0441\u043a\u043e\u0441\u0442\u0438 \u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b \u0435\u0433\u043e \u0440\u0435\u0431\u0440\u0430 \u043d\u0435 \u043f\u0435\u0440\u0435\u0441\u0435\u043a\u0430\u043b\u0438\u0441\u044c \u0432 \u0442\u043e\u0447\u043a\u0430\u0445, \u043e\u0442\u043b\u0438\u0447\u043d\u044b\u0445 \u043e\u0442 \u0432\u0435\u0440\u0448\u0438\u043d. \u0413\u0440\u0430\u0444 \u043d\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f <em>\u0434\u0432\u0443\u0434\u043e\u043b\u044c\u043d\u044b\u043c<\/em>, \u0435\u0441\u043b\u0438 \u0435\u0433\u043e \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u043c\u043e\u0436\u043d\u043e \u0440\u0430\u0437\u0434\u0435\u043b\u0438\u0442\u044c \u043d\u0430 \u0434\u0432\u0430 \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u0442\u0430\u043a\u0438\u0445, \u0447\u0442\u043e \u0440\u0435\u0431\u0440\u0430 \u0441\u043e\u0435\u0434\u0438\u043d\u044f\u044e\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0438\u0437 \u0440\u0430\u0437\u043d\u044b\u0445 \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432.<\/p>\n<p>\u042d\u0442\u0438\u0445 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0439 \u043d\u0430\u043c \u043f\u043e\u043a\u0430 \u0445\u0432\u0430\u0442\u0438\u0442. \u041f\u0435\u0440\u0435\u0439\u0434\u0451\u043c \u043a \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u043c \u0433\u0440\u0430\u0444\u043e\u0432\u044b\u043c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430\u043c.<\/p>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/47a\/b7f\/88a\/47ab7f88a5b9cf4896cd6b784c0cac01.gif\" width=\"1\" height=\"1\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/47a\/b7f\/88a\/47ab7f88a5b9cf4896cd6b784c0cac01.gif\"\/><\/figure>\n<h3>\u041f\u043e\u0438\u0441\u043a \u0432 \u0448\u0438\u0440\u0438\u043d\u0443 (Breadth First Searching)<\/h3>\n<p>\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f BFS:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/110\/f34\/74c\/110f3474c27dea3684f80d3f61dd87d7.gif\" width=\"1200\" height=\"675\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/110\/f34\/74c\/110f3474c27dea3684f80d3f61dd87d7.gif\"\/><\/figure>\n<p><strong>\u041f\u043e\u0438\u0441\u043a \u0432 \u0448\u0438\u0440\u0438\u043d\u0443<\/strong> \u2014 \u043e\u0434\u0438\u043d \u0438\u0437 \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0445 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u043e\u0432 \u043d\u0430 \u0433\u0440\u0430\u0444\u0430\u0445, \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0449\u0438\u0439 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u044c \u0432\u0441\u0435 \u043a\u0440\u0430\u0442\u0447\u0430\u0439\u0448\u0438\u0435 \u043f\u0443\u0442\u0438 \u043e\u0442 \u0437\u0430\u0434\u0430\u043d\u043d\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0438 \u0440\u0435\u0448\u0430\u0442\u044c \u043c\u043d\u043e\u0433\u0438\u0435 \u0434\u0440\u0443\u0433\u0438\u0435 \u0437\u0430\u0434\u0430\u0447\u0438.<\/p>\n<p>\u041f\u043e\u0438\u0441\u043a \u0432 \u0448\u0438\u0440\u0438\u043d\u0443 \u0442\u0430\u043a\u0436\u0435 \u043d\u0430\u0437\u044b\u0432\u0430\u044e\u0442 <strong>\u043e\u0431\u0445\u043e\u0434\u043e\u043c<\/strong> \u2014 \u0442\u0430\u043a \u0436\u0435, \u043a\u0430\u043a \u043f\u043e\u0438\u0441\u043a \u0432 \u0433\u043b\u0443\u0431\u0438\u043d\u0443 \u0438 \u0432\u0441\u0435 \u0434\u0440\u0443\u0433\u0438\u0435 \u043e\u0431\u0445\u043e\u0434\u044b, \u043e\u043d \u043f\u043e\u0441\u0435\u0449\u0430\u0435\u0442 \u0432\u0441\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0433\u0440\u0430\u0444\u0430 \u043f\u043e \u043e\u0434\u043d\u043e\u043c\u0443 \u0440\u0430\u0437\u0443, \u043d\u043e \u0432 \u0434\u0440\u0443\u0433\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435: \u043f\u043e \u0443\u0432\u0435\u043b\u0438\u0447\u0435\u043d\u0438\u044e \u0440\u0430\u0441\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u0434\u043e \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b.<\/p>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/18d\/52d\/451\/18d52d4516c2f2b34543df490677cd63.gif\" width=\"1\" height=\"1\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/18d\/52d\/451\/18d52d4516c2f2b34543df490677cd63.gif\"\/><\/figure>\n<p><strong>\u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c.<\/strong> \u041d\u0430 \u0432\u0445\u043e\u0434 \u043f\u043e\u0434\u0430\u0451\u0442\u0441\u044f <em>\u043d\u0435\u0432\u0437\u0432\u0435\u0448\u0435\u043d\u043d\u044b\u0439<\/em> \u0433\u0440\u0430\u0444 \u0438 \u043d\u043e\u043c\u0435\u0440 \u0441\u0442\u0430\u0440\u0442\u043e\u0432\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b <code>s<\/code>. \u0413\u0440\u0430\u0444 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043a\u0430\u043a \u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u043c, \u0442\u0430\u043a \u0438 \u043d\u0435\u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u043c \u2014 \u0434\u043b\u044f \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 \u044d\u0442\u043e \u043d\u0435 \u0432\u0430\u0436\u043d\u043e.<\/p>\n<p>\u041e\u0441\u043d\u043e\u0432\u043d\u0443\u044e \u0438\u0434\u0435\u044e \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043d\u0438\u043c\u0430\u0442\u044c \u043a\u0430\u043a \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u00ab\u043f\u043e\u0434\u0436\u0438\u0433\u0430\u043d\u0438\u044f\u00bb \u0433\u0440\u0430\u0444\u0430: \u043d\u0430 \u043d\u0443\u043b\u0435\u0432\u043e\u043c \u0448\u0430\u0433\u0435 \u043c\u044b \u043f\u043e\u0434\u0436\u0438\u0433\u0430\u0435\u043c \u0432\u0435\u0440\u0448\u0438\u043d\u0443 <code>s<\/code>, \u0430 \u043d\u0430 \u043a\u0430\u0436\u0434\u043e\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c \u0448\u0430\u0433\u0435 \u043e\u0433\u043e\u043d\u044c \u0441 \u043a\u0430\u0436\u0434\u043e\u0439 \u0443\u0436\u0435 \u0433\u043e\u0440\u044f\u0449\u0435\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u043f\u0435\u0440\u0435\u043a\u0438\u0434\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u0432\u0441\u0435\u0445 \u0435\u0451 \u0441\u043e\u0441\u0435\u0434\u0435\u0439, \u0432 \u043a\u043e\u043d\u0435\u0447\u043d\u043e\u043c \u0441\u0447\u0435\u0442\u0435 \u043f\u043e\u0434\u0436\u0438\u0433\u0430\u044f \u0432\u0435\u0441\u044c \u0433\u0440\u0430\u0444.<\/p>\n<p>\u0415\u0441\u043b\u0438 \u043c\u043e\u0434\u0435\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u044d\u0442\u043e\u0442 \u043f\u0440\u043e\u0446\u0435\u0441\u0441, \u0442\u043e \u0437\u0430 \u043a\u0430\u0436\u0434\u0443\u044e \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u044e \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442\u044c \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435 \u00ab\u043a\u043e\u043b\u044c\u0446\u0430 \u043e\u0433\u043d\u044f\u00bb \u0432 \u0448\u0438\u0440\u0438\u043d\u0443 \u043d\u0430 \u0435\u0434\u0438\u043d\u0438\u0446\u0443. \u041d\u043e\u043c\u0435\u0440 \u0448\u0430\u0433\u0430, \u043d\u0430 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0432\u0435\u0440\u0448\u0438\u043d\u0430 <code>v<\/code> \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u0442 \u0433\u043e\u0440\u0435\u0442\u044c, \u0432 \u0442\u043e\u0447\u043d\u043e\u0441\u0442\u0438 \u0440\u0430\u0432\u0435\u043d \u0434\u043b\u0438\u043d\u0435 \u0435\u0451 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u043f\u0443\u0442\u0438 \u0438\u0437 \u0432\u0435\u0440\u0448\u0438\u043d\u044b <code>s<\/code>.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/667\/26b\/68c\/66726b68c1fe934a8fddf6db48d58e54.png\" width=\"3344\" height=\"1348\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/667\/26b\/68c\/66726b68c1fe934a8fddf6db48d58e54.png\"\/><\/figure>\n<p>\u041c\u043e\u0434\u0435\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u044d\u0442\u043e \u043c\u043e\u0436\u043d\u043e \u0442\u0430\u043a. \u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043e\u0447\u0435\u0440\u0435\u0434\u044c, \u0432 \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u0431\u0443\u0434\u0443\u0442 \u043f\u043e\u043c\u0435\u0449\u0430\u0442\u044c\u0441\u044f \u0433\u043e\u0440\u044f\u0449\u0438\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b, \u0430 \u0442\u0430\u043a\u0436\u0435 \u0437\u0430\u0432\u0435\u0434\u0451\u043c \u0431\u0443\u043b\u0435\u0432\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0431\u0443\u0434\u0435\u043c \u043e\u0442\u043c\u0435\u0447\u0430\u0442\u044c, \u0433\u043e\u0440\u0438\u0442 \u043e\u043d\u0430 \u0438\u043b\u0438 \u043d\u0435\u0442 \u2014 \u0438\u043b\u0438 \u0438\u043d\u044b\u043c\u0438 \u0441\u043b\u043e\u0432\u0430\u043c\u0438, \u0431\u044b\u043b\u0430 \u043b\u0438 \u043e\u043d\u0430 \u0443\u0436\u0435 \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u0430. \u0418\u0437\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043f\u043e\u043c\u0435\u0449\u0430\u0435\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u0432\u0435\u0440\u0448\u0438\u043d\u0430 <code>s<\/code>, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0441\u0440\u0430\u0437\u0443 \u043f\u043e\u043c\u0435\u0447\u0430\u0435\u0442\u0441\u044f \u0433\u043e\u0440\u044f\u0449\u0435\u0439.<\/p>\n<p>\u0417\u0430\u0442\u0435\u043c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0441\u043e\u0431\u043e\u0439 \u0442\u0430\u043a\u043e\u0439 \u0446\u0438\u043a\u043b: \u043f\u043e\u043a\u0430 \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043d\u0435 \u043f\u0443\u0441\u0442\u0430, \u0434\u043e\u0441\u0442\u0430\u0442\u044c \u0438\u0437 \u0435\u0451 \u0433\u043e\u043b\u043e\u0432\u044b \u043e\u0434\u043d\u0443 \u0432\u0435\u0440\u0448\u0438\u043d\u0443 <code>v<\/code>, \u043f\u0440\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0432\u0441\u0435 \u0440\u0451\u0431\u0440\u0430, \u0438\u0441\u0445\u043e\u0434\u044f\u0449\u0438\u0435 \u0438\u0437 \u044d\u0442\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b, \u0438 \u0435\u0441\u043b\u0438 \u043a\u0430\u043a\u0438\u0435-\u0442\u043e \u0438\u0437 \u0441\u043c\u0435\u0436\u043d\u044b\u0445 \u0432\u0435\u0440\u0448\u0438\u043d <code>u<\/code> \u0435\u0449\u0451 \u043d\u0435 \u0433\u043e\u0440\u044f\u0442, \u043f\u043e\u0434\u0436\u0435\u0447\u044c \u0438\u0445 \u0438 \u043f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432 \u043a\u043e\u043d\u0435\u0446 \u043e\u0447\u0435\u0440\u0435\u0434\u0438.<\/p>\n<p>\u0412 \u0438\u0442\u043e\u0433\u0435, \u043a\u043e\u0433\u0434\u0430 \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043e\u043f\u0443\u0441\u0442\u0435\u0435\u0442, \u043c\u044b \u043f\u043e \u043e\u0434\u043d\u043e\u043c\u0443 \u0440\u0430\u0437\u0443 \u043e\u0431\u043e\u0439\u0434\u0451\u043c \u0432\u0441\u0435 \u0434\u043e\u0441\u0442\u0438\u0436\u0438\u043c\u044b\u0435 \u0438\u0437 <code>s<\/code> \u0432\u0435\u0440\u0448\u0438\u043d\u044b, \u043f\u0440\u0438\u0447\u0451\u043c \u0434\u043e \u043a\u0430\u0436\u0434\u043e\u0439 \u0434\u043e\u0439\u0434\u0451\u043c \u043a\u0440\u0430\u0442\u0447\u0430\u0439\u0448\u0438\u043c \u043f\u0443\u0442\u0451\u043c. \u0414\u043b\u0438\u043d\u044b \u043a\u0440\u0430\u0442\u0447\u0430\u0439\u0448\u0438\u0445 \u043f\u0443\u0442\u0435\u0439 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0441\u0447\u0438\u0442\u0430\u0442\u044c, \u0435\u0441\u043b\u0438 \u0437\u0430\u0432\u0435\u0441\u0442\u0438 \u0434\u043b\u044f \u043d\u0438\u0445 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 <code>d<\/code> \u0438 \u043f\u0440\u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0438 \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043f\u0435\u0440\u0435\u0441\u0447\u0438\u0442\u044b\u0432\u0430\u0442\u044c \u043f\u043e \u043f\u0440\u0430\u0432\u0438\u043b\u0443 <img loading=\"lazy\" decoding=\"async\" class=\"formula inline\" source=\"d_u = d_\\upsilon + 1\" alt=\"d_u = d_\\upsilon + 1\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/a6c\/9ae\/2f5\/a6c9ae2f55e2c962b8ac185af3c1de64.svg\" width=\"98\" height=\"20\"\/>\u200b. \u0422\u0430\u043a\u0436\u0435 \u043c\u043e\u0436\u043d\u043e \u043a\u043e\u043c\u043f\u0430\u043a\u0442\u043d\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u0443\u044e \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u0434\u043b\u044f \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f \u0441\u0430\u043c\u0438\u0445 \u043f\u0443\u0442\u0435\u0439, \u0437\u0430\u0432\u0435\u0434\u044f \u043c\u0430\u0441\u0441\u0438\u0432 \u00ab\u043f\u0440\u0435\u0434\u043a\u043e\u0432\u00bb, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0445\u0440\u0430\u043d\u0438\u0442\u0441\u044f \u043d\u043e\u043c\u0435\u0440 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0438\u0437 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043c\u044b \u0432 \u043d\u0435\u0451 \u043f\u043e\u043f\u0430\u043b\u0438.<\/p>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/91a\/64b\/f7d\/91a64bf7dd95a2edbaacea086fafce28.gif\" width=\"1\" height=\"1\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/91a\/64b\/f7d\/91a64bf7dd95a2edbaacea086fafce28.gif\"\/><\/figure>\n<p>\u0420\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u043f\u043e\u0438\u0441\u043a\u0430 \u0432 \u0448\u0438\u0440\u0438\u043d\u0443 \u043d\u0430 Python \u043c\u043e\u0436\u043d\u043e \u0442\u0430\u043a:<\/p>\n<pre><code class=\"python\"># \u0421\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u044c \u0432\u0435\u0440\u0448\u0438\u043d inc = {     1: [2, 8],     2: [1, 3, 8],     3: [2, 4, 8],     4: [3, 7, 9],     5: [6, 7],     6: [5],     7: [4, 5, 8],     8: [1, 2, 3, 7],     9: [4], }  visited = set()  # \u041f\u043e\u0441\u0435\u0449\u0435\u043d\u0430 \u043b\u0438 \u0432\u0435\u0440\u0448\u0438\u043d\u0430? Q = []           # \u041e\u0447\u0435\u0440\u0435\u0434\u044c BFS = []  # \u041f\u043e\u0438\u0441\u043a \u0432 \u0448\u0438\u0440\u0438\u043d\u0443 BFS def bfs(v):     if v in visited:  # \u0415\u0441\u043b\u0438 \u0432\u0435\u0440\u0448\u0438\u043d\u0430 \u0443\u0436\u0435 \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u0430, \u0432\u044b\u0445\u043e\u0434\u0438\u043c         return     visited.add(v)    # \u041f\u043e\u0441\u0435\u0442\u0438\u043b\u0438 \u0432\u0435\u0440\u0448\u0438\u043d\u0443 v     BFS.append(v)     # \u0417\u0430\u043f\u043e\u043c\u0438\u043d\u0430\u0435\u043c \u043f\u043e\u0440\u044f\u0434\u043e\u043a \u043e\u0431\u0445\u043e\u0434\u0430      for i in inc[v]:  # \u0412\u0441\u0435 \u0441\u043c\u0435\u0436\u043d\u044b\u0435 \u0441 v \u0432\u0435\u0440\u0448\u0438\u043d\u044b         if not i in visited:             Q.append(i)     while Q:         bfs(Q.pop(0))  start = 1 bfs(start)  # start - \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u0430\u044f \u0432\u0435\u0440\u0448\u0438\u043d\u0430 \u043e\u0431\u0445\u043e\u0434\u0430 print(BFS)  # [1, 2, 8, 3, 7, 4, 5, 9, 6] <\/code><\/pre>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/2af\/c82\/df9\/2afc82df97c56274a33838e7846ea17d.gif\" width=\"1\" height=\"1\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/2af\/c82\/df9\/2afc82df97c56274a33838e7846ea17d.gif\"\/><\/figure>\n<p>\u0412\u043e\u043e\u0431\u0449\u0435, \u043e\u0434\u043d\u043e \u0438\u0437 \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u0439 BFS \u2014 \u043f\u043e\u0438\u0441\u043a \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0440\u0430\u0441\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u043c\u0435\u0436\u0434\u0443 \u0432\u0435\u0440\u0448\u0438\u043d\u0430\u043c\u0438; \u043c\u043e\u0436\u043d\u043e \u0441\u0440\u0430\u0432\u043d\u0438\u0442\u044c, \u043d\u0430\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0445\u043e\u0440\u043e\u0448\u043e \u043e\u043d \u043f\u043e\u0434\u0445\u043e\u0434\u0438\u0442 \u0434\u043b\u044f \u044d\u0442\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438 \u043f\u043e \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044e \u0441 \u0434\u0440\u0443\u0433\u0438\u043c\u0438 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430\u043c\u0438:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/c88\/a43\/349\/c88a43349b12eb961306e248fe2d9d0c.png\" alt=\"https:\/\/www.youtube.com\/watch?v=kaA3_qNfpCA\" title=\"\" width=\"3454\" height=\"1706\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/c88\/a43\/349\/c88a43349b12eb961306e248fe2d9d0c.png\"\/><\/figure>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/bb9\/4d9\/d09\/bb94d9d09735e1e1e6d72020a9b9c940.gif\" width=\"1\" height=\"1\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/bb9\/4d9\/d09\/bb94d9d09735e1e1e6d72020a9b9c940.gif\"\/><\/figure>\n<h3>\u041f\u043e\u0438\u0441\u043a \u0432 \u0433\u043b\u0443\u0431\u0438\u043d\u0443 (Depth First Searching)<\/h3>\n<p><strong>\u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u043f\u043e\u0438\u0441\u043a\u0430 \u0432 \u0433\u043b\u0443\u0431\u0438\u043d\u0443<\/strong> (DFS) \u043e\u0442\u043b\u0438\u0447\u0430\u0435\u0442\u0441\u044f \u043e\u0442 \u043f\u043e\u0438\u0441\u043a\u0430 \u0432 \u0448\u0438\u0440\u0438\u043d\u0443 \u0431\u043e\u043b\u0435\u0435 \u0430\u0433\u0440\u0435\u0441\u0441\u0438\u0432\u043d\u044b\u043c \u043f\u0440\u043e\u0434\u0432\u0438\u0436\u0435\u043d\u0438\u0435\u043c \u043f\u043e \u0433\u0440\u0430\u0444\u0443. \u041e\u043d \u0432\u0441\u0435\u0433\u0434\u0430 \u0441\u0440\u0430\u0437\u0443 \u043f\u0440\u043e\u0434\u0432\u0438\u0433\u0430\u0435\u0442\u0441\u044f \u043a \u0441\u0430\u043c\u043e\u0439 \u043e\u0442\u0434\u0430\u043b\u0435\u043d\u043d\u043e\u0439 \u043e\u0442 \u0441\u0442\u0430\u0440\u0442\u043e\u0432\u043e\u0439 \u043d\u043e\u0434\u044b \u0432\u0435\u0440\u0448\u0438\u043d\u0435 \u0438 \u0437\u0430\u0442\u0435\u043c, \u0435\u0441\u043b\u0438 \u043d\u0435 \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u043e\u0434\u0432\u0438\u043d\u0443\u0442\u044c\u0441\u044f \u0434\u0430\u043b\u044c\u0448\u0435, \u043e\u0442\u0441\u0442\u0443\u043f\u0430\u0435\u0442 \u043d\u0430\u0437\u0430\u0434.<\/p>\n<p>\u041a\u0430\u043a \u0438 BFS, DFS \u043f\u043e\u043c\u0435\u0447\u0430\u0435\u0442 \u043d\u043e\u0434\u0443 \u043a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437, \u043a\u0430\u043a \u0435\u0435 \u043e\u0431\u043d\u0430\u0440\u0443\u0436\u0438\u0432\u0430\u0435\u0442. \u041d\u0430 \u043a\u0430\u0436\u0434\u043e\u0439 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u043e\u0431\u0445\u043e\u0434\u0438\u0442 \u0432 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u043b\u044c\u043d\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435 \u043d\u043e\u0434\u044b, \u0431\u043b\u0438\u0436\u0430\u0439\u0448\u0438\u0435 \u043a \u0442\u0435\u043a\u0443\u0449\u0435\u0439. \u041d\u0430 \u043f\u0435\u0440\u0432\u043e\u0439 \u0436\u0435 \u043d\u0430\u0439\u0434\u0435\u043d\u043d\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u0435 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u0443\u0434\u0435\u0442 \u043f\u044b\u0442\u0430\u0442\u044c\u0441\u044f \u043d\u0430\u0439\u0442\u0438 \u0431\u043b\u0438\u0436\u0430\u0439\u0448\u0438\u0435 \u043d\u043e\u0434\u044b \u043a \u0443\u0436\u0435 \u0440\u0430\u0437\u0432\u0435\u0434\u0430\u043d\u043d\u043e\u0439 (\u0432 \u044d\u0442\u043e\u043c \u043e\u043d \u043e\u0442\u043b\u0438\u0447\u0430\u0435\u0442\u0441\u044f \u043e\u0442 BFS, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0438\u0441\u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u043d\u043e\u0434\u044b, \u0431\u043b\u0438\u0436\u0430\u0439\u0448\u0438\u0435 \u043a \u0441\u0442\u0430\u0440\u0442\u043e\u0432\u043e\u0439) \u0438 \u0431\u0443\u0434\u0435\u0442 \u0434\u0435\u043b\u0430\u0442\u044c \u044d\u0442\u043e \u043d\u0430 \u043a\u0430\u0436\u0434\u043e\u0439 \u043f\u043e\u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438 \u0434\u043e \u0442\u0435\u0445 \u043f\u043e\u0440, \u043f\u043e\u043a\u0430 \u043d\u0435 \u043e\u043a\u0430\u0436\u0435\u0442\u0441\u044f \u0432 \u043d\u043e\u0434\u0435, \u0438\u0437 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0435\u043c\u0443 \u043d\u0435\u043a\u0443\u0434\u0430 \u0443\u0439\u0442\u0438. \u0422\u043e\u0433\u0434\u0430 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u043e\u0442\u0441\u0442\u0443\u043f\u0430\u0435\u0442 \u043d\u0430\u0437\u0430\u0434 \u0438 \u043f\u044b\u0442\u0430\u0435\u0442\u0441\u044f \u043f\u0440\u043e\u0434\u0432\u0438\u043d\u0443\u0442\u044c\u0441\u044f \u0434\u0430\u043b\u044c\u0448\u0435 \u043f\u043e \u0434\u0440\u0443\u0433\u043e\u043c\u0443 \u043f\u0443\u0442\u0438. \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0442\u0430\u043a \u0436\u0435 \u043e\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u0442\u0441\u044f, \u043a\u043e\u0433\u0434\u0430 \u0432\u0441\u0435 \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u044b\u0435 \u043d\u043e\u0434\u044b \u0431\u0443\u0434\u0443\u0442 \u0440\u0430\u0437\u0432\u0435\u0434\u0430\u043d\u044b.<\/p>\n<p>DFS \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442\u0441\u044f \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0441\u0442\u0435\u043a\u0430 LIFO (last in, first out, \u00ab\u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u043c \u043f\u0440\u0438\u0448\u0451\u043b \u2014 \u043f\u0435\u0440\u0432\u044b\u043c \u0443\u0448\u0451\u043b\u00bb).<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/dec\/99e\/559\/dec99e55965b912be258a3eb9132b9af.png\" width=\"1378\" height=\"856\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/dec\/99e\/559\/dec99e55965b912be258a3eb9132b9af.png\"\/><\/figure>\n<p><strong>DFS \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c.<\/strong> <strong>\u0412\u0445\u043e\u0434<\/strong>: \u0433\u0440\u0430\u0444 <img loading=\"lazy\" decoding=\"async\" class=\"formula inline\" source=\"G=(V,E)\" alt=\"G=(V,E)\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/1e7\/8cc\/e1b\/1e78cce1b0773b4a7101487f2e494452.svg\" width=\"94\" height=\"22\"\/>, \u0433\u0434\u0435 <img loading=\"lazy\" decoding=\"async\" class=\"formula inline\" source=\"V\" alt=\"V\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/12f\/9ef\/77d\/12f9ef77d11ec99cde87fd224a0602ea.svg\" width=\"15\" height=\"17\"\/> \u044d\u0442\u043e \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u043d\u043e\u0434, \u0430 <img loading=\"lazy\" decoding=\"async\" class=\"formula inline\" source=\"E\" alt=\"E\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/a32\/945\/7de\/a329457de310beeb24b2c04f869753d7.svg\" width=\"15\" height=\"17\"\/> \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u0440\u0435\u0431\u0435\u0440. \u0421\u0442\u0430\u0440\u0442\u043e\u0432\u0430\u044f \u0432\u0435\u0440\u0448\u0438\u043d\u0430 <img loading=\"lazy\" decoding=\"async\" class=\"formula inline\" source=\"s\u2208V\" alt=\"s\u2208V\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/132\/0c8\/963\/1320c8963e340646f3bb70810629fce1.svg\" width=\"47\" height=\"17\"\/>.<\/p>\n<p><strong>\u0412\u044b\u0445\u043e\u0434<\/strong>: \u0433\u0440\u0430\u0444 <img loading=\"lazy\" decoding=\"async\" class=\"formula inline\" source=\"G_{explored}=(V,E)\" alt=\"G_{explored}=(V,E)\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/15e\/2f8\/634\/15e2f8634cc578bf348fa08c56c74a88.svg\" width=\"147\" height=\"23\"\/>\u200b, \u043f\u0440\u0438 \u0443\u0441\u043b\u043e\u0432\u0438\u0438, \u0447\u0442\u043e \u043a\u0430\u0436\u0434\u0430\u044f \u0432\u0435\u0440\u0448\u0438\u043d\u0430 \u0442\u0430\u043a\u043e\u0433\u043e \u0433\u0440\u0430\u0444\u0430 \u0434\u043e\u0441\u0442\u0438\u0436\u0438\u043c\u0430 \u0438\u0437 s \u0442\u043e\u0433\u0434\u0430 \u0438 \u0442\u043e\u043b\u044c\u043a\u043e \u0442\u043e\u0433\u0434\u0430, \u043a\u043e\u0433\u0434\u0430 \u043e\u043d\u0430 \u0440\u0430\u0437\u043c\u0435\u0447\u0435\u043d\u0430 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u043e\u043c \u043a\u0430\u043a \u00ab\u0440\u0430\u0437\u0432\u0435\u0434\u0430\u043d\u043d\u0430\u044f\u00bb.<\/p>\n<p><strong>\u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c DFS<\/strong><\/p>\n<ol>\n<li>\n<p>\u043f\u043e\u043c\u0435\u0442\u0438\u0442\u044c s \u043a\u0430\u043a \u0440\u0430\u0437\u0432\u0435\u0434\u0430\u043d\u043d\u0443\u044e \u0432\u0435\u0440\u0448\u0438\u043d\u0443, \u0432\u0441\u0435 \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u043a\u0430\u043a \u043d\u0435\u0440\u0430\u0437\u0432\u0435\u0434\u0430\u043d\u043d\u044b\u0435<\/p>\n<\/li>\n<li>\n<p>\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0441\u0442\u0435\u043a S, \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u043e\u0439 s<\/p>\n<\/li>\n<li>\n<p>\u0434\u043e \u0442\u0435\u0445 \u043f\u043e\u0440, \u043f\u043e\u043a\u0430 \u0441\u0442\u0435\u043a S \u043d\u0435\u043f\u0443\u0441\u0442\u043e\u0439:<\/p>\n<\/li>\n<li>\n<p>\u0443\u0434\u0430\u043b\u0438\u0442\u044c \u0432\u0435\u0440\u0448\u0438\u043d\u0443 \u0441\u0432\u0435\u0440\u0445\u0443 \u0441\u0442\u0435\u043a\u0430 S, \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0438\u0432 \u0435\u0435 \u043a\u0430\u043a v<\/p>\n<\/li>\n<li>\n<p>\u0435\u0441\u043b\u0438 v \u043d\u0435 \u0440\u0430\u0437\u0432\u0435\u0434\u0430\u043d\u0430:<\/p>\n<\/li>\n<li>\n<p>\u043f\u043e\u043c\u0435\u0442\u0438\u0442\u044c v \u043a\u0430\u043a \u0440\u0430\u0437\u0432\u0435\u0434\u0430\u043d\u043d\u0443\u044e<\/p>\n<\/li>\n<li>\n<p>\u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0440\u0435\u0431\u0440\u0430 (v,w) \u0432 \u0441\u043f\u0438\u0441\u043a\u0435 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 v:<\/p>\n<\/li>\n<li>\n<p>\u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c w \u043d\u0430\u0432\u0435\u0440\u0445 \u0441\u0442\u0435\u043a\u0430 S<\/p>\n<\/li>\n<\/ol>\n<p>\u041a\u0440\u043e\u043c\u0435 \u0442\u043e\u0433\u043e, \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e.<\/p>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/e85\/fa6\/afc\/e85fa6afc100c411247878dc55f90caf.gif\" width=\"1\" height=\"1\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/e85\/fa6\/afc\/e85fa6afc100c411247878dc55f90caf.gif\"\/><\/figure>\n<p>\u0420\u0430\u0437\u0431\u0435\u0440\u0451\u043c DFS \u043d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0435:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/ccc\/556\/9cb\/ccc5569cb7f0027e5bf0180c67b99376.png\" width=\"2066\" height=\"1978\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/ccc\/556\/9cb\/ccc5569cb7f0027e5bf0180c67b99376.png\"\/><\/figure>\n<p>\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f DFS \u043d\u0430 Python \u043c\u043e\u0436\u0435\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u043a\u0430\u043a-\u0442\u043e \u0442\u0430\u043a:<\/p>\n<pre><code class=\"python\">def dfs(graph, start, visited=None):     if visited is None:         visited = set()     visited.add(start)     for next in graph[start] - visited:         dfs(graph, next, visited)     return visited   graph = {'0': {'1', '2'},          '1': {'0', '3', '4'},          '2': {'0'},          '3': {'1'},          '4': {'2', '3'}}   visited = dfs(graph, '0') print(visited)  # {'0', '1', '4', '2', '3'} <\/code><\/pre>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/945\/98b\/823\/94598b8232740772aca303f180978f81.gif\" width=\"1\" height=\"1\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/945\/98b\/823\/94598b8232740772aca303f180978f81.gif\"\/><\/figure>\n<h3>\u0417\u0430\u0434\u0430\u0447\u0430 \u0442\u043e\u043f\u043e\u043b\u043e\u0433\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 (Topological Sorting)<\/h3>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/935\/835\/3cc\/9358353cc3c1bbd650d462e9aaa74e2d.gif\" width=\"500\" height=\"206\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/935\/835\/3cc\/9358353cc3c1bbd650d462e9aaa74e2d.gif\"\/><\/figure>\n<p><strong>\u0417\u0430\u0434\u0430\u0447\u0430 \u0442\u043e\u043f\u043e\u043b\u043e\u0433\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438<\/strong> \u2014 \u043a\u0430\u043a \u043f\u0440\u043e\u043d\u0443\u043c\u0435\u0440\u043e\u0432\u0430\u0442\u044c \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0433\u0440\u0430\u0444\u0430, \u0447\u0442\u043e\u0431\u044b \u043a\u0430\u0436\u0434\u043e\u0435 \u0440\u0435\u0431\u0440\u043e \u0432\u0435\u043b\u043e \u0438\u0437 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0441 \u043c\u0435\u043d\u044c\u0448\u0438\u043c \u043d\u043e\u043c\u0435\u0440\u043e\u043c \u0432 \u0432\u0435\u0440\u0448\u0438\u043d\u0443 \u0441 \u0431\u043e\u043b\u044c\u0448\u0438\u043c \u043d\u043e\u043c\u0435\u0440\u043e\u043c. \u0414\u043b\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u044d\u0442\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438 \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0440\u0430\u0437\u043d\u044b\u0435 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u044b, \u0432 \u0447\u0430\u0441\u0442\u043d\u043e\u0441\u0442\u0438 DFS \u0438 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u041a\u0430\u043d\u0430.<\/p>\n<p>\u0421\u0440\u0430\u0437\u0443 \u0437\u0430\u043c\u0435\u0442\u0438\u043c, \u0447\u0442\u043e \u0433\u0440\u0430\u0444 \u0441 \u0446\u0438\u043a\u043b\u043e\u043c \u0442\u043e\u043f\u043e\u043b\u043e\u0433\u0438\u0447\u0435\u0441\u043a\u0438 \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043d\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u0441\u044f \u2014 \u043a\u0430\u043a \u043d\u0438 \u0440\u0430\u0441\u043f\u043e\u043b\u0430\u0433\u0430\u0439 \u0446\u0438\u043a\u043b \u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0435, \u0432\u0441\u0435 \u0432\u0440\u0435\u043c\u044f \u0438\u0434\u0442\u0438 \u0432\u043f\u0440\u0430\u0432\u043e \u043f\u043e \u0440\u0435\u0431\u0440\u0430\u043c \u0446\u0438\u043a\u043b\u0430 \u043d\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u0441\u044f. \u0412\u0435\u0440\u043d\u043e \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u0435 \u2014 \u0435\u0441\u043b\u0438 \u0446\u0438\u043a\u043b\u0430 \u043d\u0435\u0442, \u0442\u043e \u0435\u0433\u043e \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u043c\u043e\u0436\u043d\u043e \u0442\u043e\u043f\u043e\u043b\u043e\u0433\u0438\u0447\u0435\u0441\u043a\u0438 \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c.<\/p>\n<p>\u0414\u043b\u044f \u043e\u0434\u043d\u043e\u0433\u043e \u0433\u0440\u0430\u0444\u0430 \u043c\u043e\u0436\u0435\u0442 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u043e\u0432\u0430\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u0432 \u0442\u043e\u043f\u043e\u043b\u043e\u0433\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u043a\u043e\u0433\u0434\u0430 \u043e\u043d \u043d\u0435\u0441\u0432\u044f\u0437\u043d\u044b\u0439).<\/p>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/6a8\/03f\/b35\/6a803fb3512e21addccbe6fd96a3b4fc.gif\" width=\"1\" height=\"1\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/6a8\/03f\/b35\/6a803fb3512e21addccbe6fd96a3b4fc.gif\"\/><\/figure>\n<p><strong>\u0421\u043f\u043e\u0441\u043e\u0431\u044b \u0442\u043e\u043f\u043e\u043b\u043e\u0433\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438.<\/strong> \u0417\u0430\u043c\u0435\u0442\u0438\u043c, \u0447\u0442\u043e \u0432\u0435\u0440\u0448\u0438\u043d\u0443, \u0438\u0437 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043d\u0435 \u0432\u0435\u0434\u0435\u0442 \u043d\u0438 \u043e\u0434\u043d\u043e \u0440\u0435\u0431\u0440\u043e, \u043c\u043e\u0436\u043d\u043e \u0432\u0441\u0435\u0433\u0434\u0430 \u043f\u043e\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0439, \u0430 \u0442\u0430\u043a\u0430\u044f \u0432\u0435\u0440\u0448\u0438\u043d\u0430 \u0432 \u0430\u0446\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u043e\u043c \u0433\u0440\u0430\u0444\u0435 \u0432\u0441\u0435\u0433\u0434\u0430 \u0435\u0441\u0442\u044c (\u0438\u043d\u0430\u0447\u0435 \u043c\u043e\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u0431\u044b \u0438\u0434\u0442\u0438 \u043f\u043e \u043e\u0431\u0440\u0430\u0442\u043d\u044b\u043c \u0440\u0451\u0431\u0440\u0430\u043c \u0431\u0435\u0441\u043a\u043e\u043d\u0435\u0447\u043d\u043e). \u0418\u0437 \u044d\u0442\u043e\u0433\u043e \u0441\u0440\u0430\u0437\u0443 \u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u0438\u0432\u043d\u043e\u0435 \u0434\u043e\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c\u0441\u0442\u0432\u043e: \u0431\u0443\u0434\u0435\u043c \u0438\u0442\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u043e \u043a\u043b\u0430\u0441\u0442\u044c \u0432 \u043c\u0430\u0441\u0441\u0438\u0432 \u0432\u0435\u0440\u0448\u0438\u043d\u0443, \u0438\u0437 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u0432\u0435\u0434\u0435\u0442, \u0438 \u0443\u0431\u0438\u0440\u0430\u0442\u044c \u0435\u0435 \u0438\u0437 \u0433\u0440\u0430\u0444\u0430. \u041f\u043e\u0441\u043b\u0435 \u044d\u0442\u043e\u0433\u043e \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0430 \u043c\u0430\u0441\u0441\u0438\u0432 \u043d\u0430\u0434\u043e \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0437\u0432\u0435\u0440\u043d\u0443\u0442\u044c.<\/p>\n<p>\u042d\u0442\u043e\u0442 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u043f\u0440\u043e\u0449\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c, \u043e\u0431\u0440\u0430\u0442\u0438\u0432 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043d\u0430 \u0432\u0440\u0435\u043c\u0435\u043d\u0430 \u0432\u044b\u0445\u043e\u0434\u0430 \u0432\u0435\u0440\u0448\u0438\u043d \u0432 DFS. \u0412\u0435\u0440\u0448\u0438\u043d\u0430, \u0438\u0437 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043c\u044b \u0432\u044b\u0439\u0434\u0435\u043c \u043f\u0435\u0440\u0432\u043e\u0439 \u2014 \u0442\u0430, \u0443 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043d\u0435\u0442 \u043d\u043e\u0432\u044b\u0445 \u0438\u0441\u0445\u043e\u0434\u044f\u0449\u0438\u0445 \u0440\u0435\u0431\u0435\u0440. \u0414\u0430\u043b\u044c\u0448\u0435 \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0432\u044b\u0445\u043e\u0434\u0438\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u0438\u0437 \u0442\u0435\u0445 \u0432\u0435\u0440\u0448\u0438\u043d, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0435\u0441\u043b\u0438 \u0438 \u0438\u043c\u0435\u044e\u0442 \u0438\u0441\u0445\u043e\u0434\u044f\u0449\u0438\u0435 \u0440\u0435\u0431\u0440\u0430, \u0442\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u0432 \u0442\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b, \u0438\u0437 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043c\u044b \u0443\u0436\u0435 \u0432\u044b\u0448\u043b\u0438. \u0421\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e, \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u043e \u0432\u044b\u043f\u0438\u0441\u0430\u0442\u044c \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0432 \u043f\u043e\u0440\u044f\u0434\u043a\u0435 \u0432\u044b\u0445\u043e\u0434\u0430 \u0438\u0437 DFS, \u0430 \u0437\u0430\u0442\u0435\u043c \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u0440\u0430\u0437\u0432\u0435\u0440\u043d\u0443\u0442\u044c, \u0438 \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u043a\u0430\u043a\u0443\u044e-\u0442\u043e \u0438\u0437 \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u044b\u0445 \u0442\u043e\u043f\u043e\u043b\u043e\u0433\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043e\u043a.<\/p>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/e85\/cc0\/ca3\/e85cc0ca3e0d7047d0b504dda727bc3f.gif\" width=\"1\" height=\"1\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/e85\/cc0\/ca3\/e85cc0ca3e0d7047d0b504dda727bc3f.gif\"\/><\/figure>\n<p><strong>\u0422\u043e\u043f\u043e\u043b\u043e\u0433\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043d\u0430 Python \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 DFS.<\/strong><\/p>\n<p>\u0418\u0442\u0430\u043a, \u0441\u043f\u0435\u0440\u0432\u0430 \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u043c \u0433\u043b\u0443\u0431\u0438\u043d\u0443 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0438.<\/p>\n<pre><code class=\"python\">import sys sys.setrecursionlimit(10000) <\/code><\/pre>\n<p>\u0424\u0443\u043d\u043a\u0446\u0438\u044f <code>dfs<\/code> \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442 \u043e\u0431\u0445\u043e\u0434 \u0432 \u0433\u043b\u0443\u0431\u0438\u043d\u0443 \u0438\u0437 \u0432\u0435\u0440\u0448\u0438\u043d\u044b <code>v<\/code>.<\/p>\n<pre><code class=\"python\">def dfs(v): <\/code><\/pre>\n<p>\u0417\u0430\u0448\u043b\u0438 \u0432 \u0432\u0435\u0440\u0448\u0438\u043d\u0443 v. \u0414\u0435\u043b\u0430\u0435\u043c \u0435\u0435 \u0441\u0435\u0440\u043e\u0439.<\/p>\n<pre><code class=\"python\">  used[v] = 1 <\/code><\/pre>\n<p>\u041f\u0435\u0440\u0435\u0431\u0438\u0440\u0430\u0435\u043c \u0432\u0435\u0440\u0448\u0438\u043d\u044b to, \u0432 \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u0439\u0442\u0438 \u0438\u0437 v.<\/p>\n<pre><code class=\"python\">  for to in g[v]: <\/code><\/pre>\n<p>\u0415\u0441\u043b\u0438 \u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0435 \u0440\u0435\u0431\u0440\u043e <code>(v, to)<\/code> \u0432\u0435\u0434\u0435\u0442 \u0432 \u0441\u0435\u0440\u0443\u044e \u0432\u0435\u0440\u0448\u0438\u043d\u0443, \u0442\u043e \u0432 \u0433\u0440\u0430\u0444\u0435 \u043f\u0440\u0438\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \u0446\u0438\u043a\u043b. \u0423\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c <code>flag = 1<\/code>.<\/p>\n<pre><code class=\"python\">    if used[to] == 1:       global flag       flag = 1 <\/code><\/pre>\n<p>\u0415\u0441\u043b\u0438 \u0432\u0435\u0440\u0448\u0438\u043d\u0430 to \u0435\u0449\u0435 \u043d\u0435 \u043f\u0440\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u043d\u0430, \u0442\u043e \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u0438\u0437 \u043d\u0435\u0435 \u043f\u043e\u0438\u0441\u043a \u0432 \u0433\u043b\u0443\u0431\u0438\u043d\u0443.<\/p>\n<pre><code class=\"python\">    if used[to] == 0:       dfs(to) <\/code><\/pre>\n<p>\u0417\u0430\u0432\u0435\u0440\u0448\u0430\u0435\u043c \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0443 \u0432\u0435\u0440\u0448\u0438\u043d\u044b v. \u0414\u0435\u043b\u0430\u0435\u043c \u0435\u0435 \u0447\u0435\u0440\u043d\u043e\u0439 \u0438 \u0437\u0430\u043d\u043e\u0441\u0438\u043c \u0432 \u043c\u0430\u0441\u0441\u0438\u0432 top.<\/p>\n<pre><code class=\"python\">  used[v] = 2   top.append(v) <\/code><\/pre>\n<p>\u041e\u0441\u043d\u043e\u0432\u043d\u0430\u044f \u0447\u0430\u0441\u0442\u044c \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b. \u0427\u0438\u0442\u0430\u0435\u043c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0432\u0435\u0440\u0448\u0438\u043d <code>n<\/code> \u0438 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0440\u0435\u0431\u0435\u0440 <code>m<\/code>.<\/p>\n<pre><code class=\"python\">n, m = map(int, input().split()) <\/code><\/pre>\n<p>\u0412\u0445\u043e\u0434\u043d\u043e\u0439 \u0433\u0440\u0430\u0444 \u0445\u0440\u0430\u043d\u0438\u043c \u0432 \u0441\u043f\u0438\u0441\u043a\u0435 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 <code>g<\/code>. \u041e\u0431\u044c\u044f\u0432\u0438\u043c \u0441\u043f\u0438\u0441\u043e\u043a <code>used<\/code>.<\/p>\n<pre><code class=\"python\">g = [[] for _ in range(n + 1)] used = [0] * (n + 1) <\/code><\/pre>\n<p>\u0427\u0438\u0442\u0430\u0435\u043c \u0441\u043f\u0438\u0441\u043e\u043a \u0440\u0435\u0431\u0435\u0440. \u0421\u0442\u0440\u043e\u0438\u043c \u0441\u043f\u0438\u0441\u043e\u043a \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 \u0433\u0440\u0430\u0444\u0430.<\/p>\n<pre><code class=\"python\">for _ in range(m):   a, b = map(int, input().split())   g[a].append(b) <\/code><\/pre>\n<p>\u0421\u043e\u0432\u0435\u0440\u0448\u0430\u0435\u043c \u043e\u0431\u0445\u043e\u0434 \u0432 \u0433\u043b\u0443\u0431\u0438\u043d\u0443 \u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0433\u0440\u0430\u0444\u0430.<\/p>\n<pre><code class=\"python\">flag = 0 top = [] for i in range(1, n + 1):   if used[i] == 0: dfs(i) <\/code><\/pre>\n<p>\u0415\u0441\u043b\u0438 \u0432 \u0433\u0440\u0430\u0444\u0435 \u043f\u0440\u0438\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \u0446\u0438\u043a\u043b (\u043f\u0440\u0438 \u043e\u0431\u0445\u043e\u0434\u0435 \u0432 \u0433\u043b\u0443\u0431\u0438\u043d\u0443 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d\u043e <code>flag = 1<\/code>), \u0442\u043e \u0432\u044b\u0432\u043e\u0434\u0438\u043c <code>-1<\/code>.<\/p>\n<pre><code class=\"python\">if flag == 1:   print(\"-1\") else: <\/code><\/pre>\n<p>\u0412\u044b\u0432\u043e\u0434\u0438\u043c \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0433\u0440\u0430\u0444\u0430 \u0432 \u043f\u043e\u0440\u044f\u0434\u043a\u0435, \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u043c \u0442\u043e\u043c\u0443, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u043e\u043d\u0438 \u0437\u0430\u043d\u043e\u0441\u0438\u043b\u0438\u0441\u044c \u0432 \u0441\u043f\u0438\u0441\u043e\u043a <code>top<\/code>.<\/p>\n<pre><code class=\"python\">  for i in range(len(top) - 1, -1, -1):     print(top[i], end=\" \")   print() <\/code><\/pre>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/7fd\/21e\/200\/7fd21e2005cc38e384b633af03bf5f89.gif\" width=\"1\" height=\"1\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/7fd\/21e\/200\/7fd21e2005cc38e384b633af03bf5f89.gif\"\/><\/figure>\n<p><strong>\u0422\u043e\u043f\u043e\u043b\u043e\u0433\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 \u041a\u0430\u043d\u0430.<\/strong><\/p>\n<p>\u041e\u0431\u044a\u044f\u0432\u0438\u043c \u043e\u0447\u0435\u0440\u0435\u0434\u044c <code>q<\/code>.<\/p>\n<pre><code class=\"python\">from collections import deque q = deque() <\/code><\/pre>\n<p>\u0427\u0438\u0442\u0430\u0435\u043c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0432\u0435\u0440\u0448\u0438\u043d <code>n<\/code> \u0438 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0440\u0435\u0431\u0435\u0440 <code>m<\/code>.<\/p>\n<pre><code class=\"python\">n, m = map(int, input().split()) <\/code><\/pre>\n<p>\u0412\u0445\u043e\u0434\u043d\u043e\u0439 \u0433\u0440\u0430\u0444 \u0445\u0440\u0430\u043d\u0438\u043c \u0432 \u0441\u043f\u0438\u0441\u043a\u0435 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 <code>graph<\/code>.<br \/> \u0412\u0445\u043e\u0434\u044f\u0449\u0438\u0435 \u0441\u0442\u0435\u043f\u0435\u043d\u0438 \u0432\u0435\u0440\u0448\u0438\u043d \u0445\u0440\u0430\u043d\u0438\u043c \u0432 \u0441\u043f\u0438\u0441\u043a\u0435 <code>InDegree<\/code>.<br \/> \u0422\u043e\u043f\u043e\u043b\u043e\u0433\u0438\u0447\u0435\u0441\u043a\u0438 \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0433\u0440\u0430\u0444\u0430 \u0437\u0430\u043d\u043e\u0441\u0438\u043c \u0432 \u0441\u043f\u0438\u0441\u043e\u043a <code>top<\/code>.<\/p>\n<pre><code class=\"python\">graph = [[] for _ in range(n + 1)] InDegree = [0] * (n + 1) top = [] <\/code><\/pre>\n<p>\u0427\u0438\u0442\u0430\u0435\u043c <code>m<\/code> \u0440\u0435\u0431\u0435\u0440 \u0433\u0440\u0430\u0444\u0430.<\/p>\n<pre><code class=\"python\">for _ in range(m):   a, b = map(int, input().split())   graph[a].append(b) <\/code><\/pre>\n<p>\u0414\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0440\u0435\u0431\u0440\u0430 <code>(a, b)<\/code> \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u043c <code>InDegree[b]<\/code> \u043d\u0430 1.<\/p>\n<pre><code class=\"python\">  InDegree[b] += 1 <\/code><\/pre>\n<p>\u0412\u0441\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b, \u0432\u0445\u043e\u0434\u044f\u0449\u0438\u0435 \u0441\u0442\u0435\u043f\u0435\u043d\u0438 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0440\u0430\u0432\u043d\u044b \u043d\u0443\u043b\u044e, \u0437\u0430\u043d\u043e\u0441\u0438\u043c \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c <code>q<\/code>.<\/p>\n<pre><code class=\"python\">for i in range(1, len(InDegree)):   if not InDegree[i]: q.append(i) <\/code><\/pre>\n<p>\u041f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u0435\u043c \u0440\u0430\u0431\u043e\u0442\u0443 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430, \u043f\u043e\u043a\u0430 \u043e\u0447\u0435\u0440\u0435\u0434\u044c <code>q<\/code> \u043d\u0435 \u043f\u0443\u0441\u0442\u0430.<\/p>\n<pre><code class=\"python\">while q: <\/code><\/pre>\n<p>\u0418\u0437\u0432\u043b\u0435\u043a\u0430\u0435\u043c \u0432\u0435\u0440\u0448\u0438\u043d\u0443 <code>v<\/code> \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 \u0438 \u0437\u0430\u043d\u043e\u0441\u0438\u043c \u0435\u0435 \u0432 \u043a\u043e\u043d\u0435\u0446 \u0442\u043e\u043f\u043e\u043b\u043e\u0433\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u043f\u043e\u0440\u044f\u0434\u043a\u0430.<\/p>\n<pre><code class=\"python\">  v = q.popleft()   top.append(v) <\/code><\/pre>\n<p>\u0423\u0434\u0430\u043b\u044f\u0435\u043c \u0438\u0437 \u0433\u0440\u0430\u0444\u0430 \u0440\u0435\u0431\u0440\u0430 <code>(v, to)<\/code>. \u0414\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0442\u0430\u043a\u043e\u0433\u043e \u0440\u0435\u0431\u0440\u0430 \u0443\u043c\u0435\u043d\u044c\u0448\u0430\u0435\u043c \u0432\u0445\u043e\u0434\u044f\u0449\u0443\u044e \u0441\u0442\u0435\u043f\u0435\u043d\u044c \u0432\u0435\u0440\u0448\u0438\u043d\u044b <code>to<\/code>.<\/p>\n<pre><code class=\"python\">  for to in graph[v]:     InDegree[to] -= 1 <\/code><\/pre>\n<p>\u0415\u0441\u043b\u0438 \u0441\u0442\u0435\u043f\u0435\u043d\u044c \u0432\u0435\u0440\u0448\u0438\u043d\u044b to \u0441\u0442\u0430\u043d\u0435\u0442 \u043d\u0443\u043b\u0435\u0432\u043e\u0439, \u0442\u043e \u0437\u0430\u043d\u043e\u0441\u0438\u043c \u0435\u0435 \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c, \u043e\u0442\u043a\u0443\u0434\u0430 \u043e\u043d\u0430 \u043f\u0440\u044f\u043c\u0438\u043a\u043e\u043c \u043f\u043e\u043f\u0430\u0434\u0435\u0442 \u0432 \u0441\u043f\u0438\u0441\u043e\u043a \u0442\u043e\u043f\u043e\u043b\u043e\u0433\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u043f\u043e\u0440\u044f\u0434\u043a\u0430.<\/p>\n<pre><code class=\"python\">if not InDegree[to]: q.append(to) <\/code><\/pre>\n<p>\u0415\u0441\u043b\u0438 \u0432 \u0441\u043f\u0438\u0441\u043e\u043a top \u0437\u0430\u043d\u0435\u0441\u0435\u043d\u044b \u043d\u0435 \u0432\u0441\u0435 n \u0432\u0435\u0440\u0448\u0438\u043d, \u0442\u043e \u0433\u0440\u0430\u0444 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0446\u0438\u043a\u043b \u0438 \u0442\u043e\u043f\u043e\u043b\u043e\u0433\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0430 \u043d\u0435\u0432\u043e\u0437\u043c\u043e\u0436\u043d\u0430.<\/p>\n<pre><code class=\"python\">if len(top) &lt; n:   print(\"-1\") else: <\/code><\/pre>\n<p>\u0412\u044b\u0432\u043e\u0434\u0438\u043c \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0433\u0440\u0430\u0444\u0430 \u0432 \u0442\u043e\u043f\u043e\u043b\u043e\u0433\u0438\u0447\u0435\u0441\u043a\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435.<\/p>\n<pre><code class=\"python\">  for i in top:     print(i, end=\" \")   print() <\/code><\/pre>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/e58\/e02\/60f\/e58e0260f94bf1688b6dead8a10f9486.gif\" width=\"1\" height=\"1\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/e58\/e02\/60f\/e58e0260f94bf1688b6dead8a10f9486.gif\"\/><\/figure>\n<h3>\u0417\u0430\u0434\u0430\u0447\u0430 \u0440\u0430\u0441\u043a\u0440\u0430\u0441\u043a\u0438 \u0433\u0440\u0430\u0444\u0430<\/h3>\n<p><strong>\u0420\u0430\u0441\u043a\u0440\u0430\u0441\u043a\u0430 \u0433\u0440\u0430\u0444\u0430<\/strong> \u043d\u0430\u0437\u043d\u0430\u0447\u0430\u0435\u0442 \u0446\u0432\u0435\u0442\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c \u0433\u0440\u0430\u0444\u0430, \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0438\u0432\u0430\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u0445 \u0443\u0441\u043b\u043e\u0432\u0438\u0439. \u0427\u0430\u0449\u0435 \u0432\u0441\u0435\u0433\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f <em>\u0440\u0430\u0441\u043a\u0440\u0430\u0441\u043a\u0430 \u0432\u0435\u0440\u0448\u0438\u043d<\/em>, \u043f\u0440\u0438 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043c\u044b \u043f\u044b\u0442\u0430\u0435\u043c\u0441\u044f \u0440\u0430\u0441\u043a\u0440\u0430\u0441\u0438\u0442\u044c \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0433\u0440\u0430\u0444\u0430, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f k \u0446\u0432\u0435\u0442\u043e\u0432, \u0447\u0442\u043e\u0431\u044b \u0441\u043e\u0441\u0435\u0434\u043d\u0438\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u043d\u0438\u043a\u043e\u0433\u0434\u0430 \u043d\u0435 \u0438\u043c\u0435\u043b\u0438 \u043e\u0434\u0438\u043d \u0438 \u0442\u043e\u0442 \u0436\u0435 \u0446\u0432\u0435\u0442. \u0414\u0440\u0443\u0433\u0438\u0435 \u0432\u0438\u0434\u044b \u0440\u0430\u0441\u043a\u0440\u0430\u0441\u043a\u0438 \u0432\u043a\u043b\u044e\u0447\u0430\u044e\u0442 <em>\u0440\u0430\u0441\u043a\u0440\u0430\u0441\u043a\u0443 \u0440\u0435\u0431\u0435\u0440<\/em> \u0438 <em>\u0440\u0430\u0441\u043a\u0440\u0430\u0441\u043a\u0443 \u0433\u0440\u0430\u043d\u0435\u0439<\/em>.<\/p>\n<p>\u0425\u0440\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u0447\u0438\u0441\u043b\u043e \u0433\u0440\u0430\u0444\u0430 \u2013 \u044d\u0442\u043e \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0446\u0432\u0435\u0442\u043e\u0432, \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0435 \u0434\u043b\u044f \u0435\u0433\u043e \u0440\u0430\u0441\u043a\u0440\u0430\u0441\u043a\u0438. \u0422\u0443\u0442 \u043d\u0438\u0436\u0435 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0430 \u0440\u0430\u0441\u043a\u0440\u0430\u0441\u043a\u0430 \u0432\u0435\u0440\u0448\u0438\u043d \u0433\u0440\u0430\u0444\u0430 4 \u0446\u0432\u0435\u0442\u0430\u043c\u0438.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/eac\/2b8\/dd5\/eac2b8dd52db689c1d085cdb90d53504.gif\" alt=\"https:\/\/proglib.io\/p\/10-animirovannyh-algoritmov-na-grafah-2020-09-09\" title=\"\" width=\"604\" height=\"644\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/eac\/2b8\/dd5\/eac2b8dd52db689c1d085cdb90d53504.gif\"\/><\/figure>\n<p><strong>\u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u044b, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u044b\u0435 \u0434\u043b\u044f \u0440\u0430\u0441\u043a\u0440\u0430\u0441\u043a\u0438<\/strong>:<\/p>\n<ol>\n<li>\n<p>\u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u044b, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0449\u0438\u0435 \u043f\u043e\u0438\u0441\u043a \u0432 \u0448\u0438\u0440\u0438\u043d\u0443 \u0438\u043b\u0438 \u0432 \u0433\u043b\u0443\u0431\u0438\u043d\u0443.<\/p>\n<\/li>\n<li>\n<p>&#171;\u0416\u0430\u0434\u043d\u0430\u044f&#187; \u0440\u0430\u0441\u043a\u0440\u0430\u0441\u043a\u0430.<\/p>\n<\/li>\n<\/ol>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/b0b\/6b8\/2b9\/b0b6b82b9125ae8f8a71d71d165084b8.gif\" width=\"1\" height=\"1\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/b0b\/6b8\/2b9\/b0b6b82b9125ae8f8a71d71d165084b8.gif\"\/><\/figure>\n<p>\u0420\u0430\u0441\u043a\u0440\u0430\u0441\u043a\u0430 \u0433\u0440\u0430\u0444\u043e\u0432 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f:<\/p>\n<ul>\n<li>\n<p>\u0434\u043b\u044f \u0441\u043e\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0440\u0430\u0441\u043f\u0438\u0441\u0430\u043d\u0438\u0439;<\/p>\n<\/li>\n<li>\n<p>\u0434\u043b\u044f \u043d\u0430\u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0440\u0430\u0434\u0438\u043e\u0447\u0430\u0441\u0442\u043e\u0442 \u043c\u043e\u0431\u0438\u043b\u044c\u043d\u044b\u043c \u0441\u0435\u0442\u044f\u043c;<\/p>\n<\/li>\n<li>\n<p>\u0434\u043b\u044f \u043c\u043e\u0434\u0435\u043b\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0438 \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0438\u0433\u0440 \u0432\u0440\u043e\u0434\u0435 \u0441\u0443\u0434\u043e\u043a\u0443;<\/p>\n<\/li>\n<li>\n<p>\u0434\u043b\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438, \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043b\u0438 \u0433\u0440\u0430\u0444 \u0434\u0432\u0443\u0447\u0430\u0441\u0442\u043d\u044b\u043c;<\/p>\n<\/li>\n<li>\n<p>\u0434\u043b\u044f \u0440\u0430\u0441\u043a\u0440\u0430\u0441\u043a\u0438 \u0433\u0435\u043e\u0433\u0440\u0430\u0444\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u043a\u0430\u0440\u0442, \u0447\u0442\u043e\u0431\u044b \u0441\u043e\u0441\u0435\u0434\u043d\u0438\u0435 \u0441\u0442\u0440\u0430\u043d\u044b \u0432\u0441\u0435\u0433\u0434\u0430 \u0438\u043c\u0435\u043b\u0438 \u0440\u0430\u0437\u043d\u044b\u0435 \u0446\u0432\u0435\u0442\u0430.<\/p>\n<\/li>\n<\/ul>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/44b\/bee\/2b4\/44bbee2b4a2550c470f108f653d853dd.gif\" width=\"1\" height=\"1\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/44b\/bee\/2b4\/44bbee2b4a2550c470f108f653d853dd.gif\"\/><\/figure>\n<h3>\u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0411\u0435\u043b\u043b\u043c\u0430\u043d\u0430-\u0424\u043e\u0440\u0434\u0430 (Bellman Ford&#8217;s Algorithm)<\/h3>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w780q1\/getpro\/habr\/upload_files\/f66\/dd8\/5f4\/f66dd85f4915d3d149b7206f58c705d9.jpeg\" width=\"2484\" height=\"1608\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/f66\/dd8\/5f4\/f66dd85f4915d3d149b7206f58c705d9.jpeg\" data-blurred=\"true\"\/><\/figure>\n<p><strong>\u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0424\u043e\u0440\u0434\u0430-\u0411\u0435\u043b\u043b\u043c\u0430\u043d\u0430<\/strong> \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043d\u0430\u0439\u0442\u0438 \u043a\u0440\u0430\u0442\u0447\u0430\u0439\u0448\u0438\u0435 \u043f\u0443\u0442\u0438 \u0438\u0437 \u043e\u0434\u043d\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0433\u0440\u0430\u0444\u0430 \u0434\u043e \u0432\u0441\u0435\u0445 \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0445, \u0434\u0430\u0436\u0435 \u0434\u043b\u044f \u0433\u0440\u0430\u0444\u043e\u0432, \u0432 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0432\u0435\u0441\u0430 \u0440\u0435\u0431\u0435\u0440 \u043c\u043e\u0433\u0443\u0442 \u0431\u044b\u0442\u044c \u043e\u0442\u0440\u0438\u0446\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u043c\u0438. \u0422\u0435\u043c \u043d\u0435 \u043c\u0435\u043d\u0435\u0435, \u0432 \u0433\u0440\u0430\u0444\u0435 \u043d\u0435 \u0434\u043e\u043b\u0436\u043d\u043e \u0431\u044b\u0442\u044c \u0446\u0438\u043a\u043b\u043e\u0432 \u043e\u0442\u0440\u0438\u0446\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u0432\u0435\u0441\u0430, \u0434\u043e\u0441\u0442\u0438\u0436\u0438\u043c\u044b\u0445 \u0438\u0437 \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b, \u0438\u043d\u0430\u0447\u0435 \u0432\u043e\u043f\u0440\u043e\u0441 \u043e \u043a\u0440\u0430\u0442\u0447\u0430\u0439\u0448\u0438\u0445 \u043f\u0443\u0442\u044f\u0445 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0431\u0435\u0441\u0441\u043c\u044b\u0441\u043b\u0435\u043d\u043d\u044b\u043c. \u041f\u0440\u0438 \u044d\u0442\u043e\u043c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0424\u043e\u0440\u0434\u0430-\u0411\u0435\u043b\u043b\u043c\u0430\u043d\u0430 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u043d\u0430\u043b\u0438\u0447\u0438\u0435 \u0446\u0438\u043a\u043b\u043e\u0432 \u043e\u0442\u0440\u0438\u0446\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u0432\u0435\u0441\u0430, \u0434\u043e\u0441\u0442\u0438\u0436\u0438\u043c\u044b\u0445 \u0438\u0437 \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b.<\/p>\n<p>\u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0411\u0435\u043b\u043b\u043c\u0430\u043d\u0430-\u0424\u043e\u0440\u0434\u0430 \u043c\u043e\u0436\u043d\u043e \u043e\u043f\u0438\u0441\u0430\u0442\u044c \u0442\u0430\u043a:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/c52\/596\/6b7\/c525966b7f48658df98854caea0fb25e.png\" alt=\"https:\/\/users.math-cs.spbu.ru\/~okhotin\/teaching\/algorithms_2020\/okhotin_algorithms_2020_l4.pdf\" width=\"1267\" height=\"440\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/c52\/596\/6b7\/c525966b7f48658df98854caea0fb25e.png\"\/><\/p>\n<div><figcaption>https:\/\/users.math-cs.spbu.ru\/~okhotin\/teaching\/algorithms_2020\/okhotin_algorithms_2020_l4.pdf<\/figcaption><\/div>\n<\/figure>\n<p>\u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435. \u0412\u0432\u0435\u0434\u0435\u043c <code>F<\/code>:<\/p>\n<p><code>F[k][i]<\/code> \u2014 \u0434\u043b\u0438\u043d\u0430 \u043a\u0440\u0430\u0442\u0447\u0430\u0439\u0448\u0435\u0433\u043e \u043f\u0443\u0442\u0438 \u0438\u0437 \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0434\u043e \u0432\u0435\u0440\u0448\u0438\u043d\u044b <code>i<\/code>, \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0449\u0435\u0433\u043e \u043d\u0435 \u0431\u043e\u043b\u0435\u0435 <code>k<\/code> \u0440\u0435\u0431\u0435\u0440.<\/p>\n<p>\u041d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0437\u0430\u0434\u0430\u0434\u0438\u043c \u0434\u043b\u044f \u0441\u043b\u0443\u0447\u0430\u044f <code>k=0<\/code>. \u0412 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 <code>F[0][start] = 0<\/code>, \u0430 \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0445 \u0432\u0435\u0440\u0448\u0438\u043d <code>i<\/code> <code>F[0][i] = INF<\/code>, \u0442\u043e \u0435\u0441\u0442\u044c \u043f\u0443\u0442\u044c, \u0441\u043e\u0441\u0442\u043e\u044f\u0449\u0438\u0439 \u0438\u0437 \u043d\u0443\u043b\u044f \u0440\u0435\u0431\u0435\u0440 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0442 \u0432\u0435\u0440\u0448\u0438\u043d\u044b <code>start<\/code> \u0434\u043e \u0432\u0435\u0440\u0448\u0438\u043d\u044b <code>start<\/code>, \u0430 \u0434\u043e \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0445 \u0432\u0435\u0440\u0448\u0438\u043d \u043f\u0443\u0442\u0438 \u0438\u0437 \u043d\u0443\u043b\u044f \u0440\u0435\u0431\u0435\u0440 \u043d\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442, \u0447\u0442\u043e \u0431\u0443\u0434\u0435\u043c \u043e\u0442\u043c\u0435\u0447\u0430\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c <code>INF<\/code>.<\/p>\n<p>\u0414\u0430\u043b\u0435\u0435 \u0431\u0443\u0434\u0435\u043c \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>F<\/code> \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0432\u0430\u044f \u0447\u0438\u0441\u043b\u043e \u0440\u0435\u0431\u0435\u0440 \u0432 \u043f\u0443\u0442\u0438 <code>k<\/code>, \u0442\u043e \u0435\u0441\u0442\u044c \u0432\u044b\u0447\u0438\u0441\u043b\u0438\u043c \u043a\u0440\u0430\u0442\u0447\u0430\u0439\u0448\u0438\u0435 \u043f\u0443\u0442\u0438, \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0449\u0438\u0435 \u043d\u0435 \u0431\u043e\u043b\u0435\u0435 1 \u0440\u0435\u0431\u0440\u0430, \u043a\u0440\u0430\u0442\u0447\u0430\u0439\u0448\u0438\u0435 \u043f\u0443\u0442\u0438, \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0449\u0438\u0435 \u043d\u0435 \u0431\u043e\u043b\u0435\u0435 2 \u0440\u0435\u0431\u0435\u0440 \u0438 \u0442. \u0434. \u0415\u0441\u043b\u0438 \u0432 \u0433\u0440\u0430\u0444\u0435 \u043d\u0435\u0442 \u0446\u0438\u043a\u043b\u043e\u0432 \u043e\u0442\u0440\u0438\u0446\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0433\u043e \u0432\u0435\u0441\u0430, \u0442\u043e \u043a\u0440\u0430\u0442\u0447\u0430\u0439\u0448\u0438\u0439 \u043f\u0443\u0442\u044c \u043c\u0435\u0436\u0434\u0443 \u043b\u044e\u0431\u044b\u043c\u0438 \u0434\u0432\u0443\u043c\u044f \u0432\u0435\u0440\u0448\u0438\u043d\u0430\u043c\u0438 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u043d\u0435 \u0431\u043e\u043b\u0435\u0435 <code>n-1<\/code> \u0440\u0435\u0431\u0440\u0430 (<code>n<\/code> &#8212; \u0447\u0438\u0441\u043b\u043e \u0432\u0435\u0440\u0448\u0438\u043d \u0432 \u0433\u0440\u0430\u0444\u0435), \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043d\u0443\u0436\u043d\u043e \u0432\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f <code>F[n-1][i]<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0438 \u0431\u0443\u0434\u0443\u0442 \u0434\u043b\u0438\u043d\u0430\u043c\u0438 \u043a\u0440\u0430\u0442\u0447\u0430\u0439\u0448\u0438\u0445 \u043f\u0443\u0442\u0435\u0439 \u043e\u0442 \u0432\u0435\u0440\u0448\u0438\u043d\u044b <code>start<\/code> \u0434\u043e \u0432\u0435\u0440\u0448\u0438\u043d\u044b <code>i<\/code>).<\/p>\n<p>\u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c, \u043a\u0430\u043a \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0435\u0442\u0441\u044f \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 <code>F[k][i]<\/code>. \u041f\u0443\u0441\u0442\u044c \u0435\u0441\u0442\u044c \u043a\u0440\u0430\u0442\u0447\u0430\u0439\u0448\u0438\u0439 \u043c\u0430\u0440\u0448\u0440\u0443\u0442 \u0438\u0437 \u0432\u0435\u0440\u0448\u0438\u043d\u044b <code>start<\/code> \u0434\u043e \u0432\u0435\u0440\u0448\u0438\u043d\u044b <code>i<\/code>, \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0449\u0438\u0439 \u043d\u0435 \u0431\u043e\u043b\u0435\u0435 <code>k<\/code> \u0440\u0435\u0431\u0435\u0440. \u041f\u0443\u0441\u0442\u044c \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0435 \u0440\u0435\u0431\u0440\u043e \u044d\u0442\u043e\u0433\u043e \u043c\u0430\u0440\u0448\u0440\u0443\u0442\u0430 \u0435\u0441\u0442\u044c \u0440\u0435\u0431\u0440\u043e <code>j-i<\/code>. \u0422\u043e\u0433\u0434\u0430 \u043f\u0443\u0442\u044c \u0434\u043e \u0432\u0435\u0440\u0448\u0438\u043d\u044b <code>j<\/code> \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u043d\u0435 \u0431\u043e\u043b\u0435\u0435 <code>k-1<\/code> \u0440\u0435\u0431\u0440\u0430 \u0438 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043a\u0440\u0430\u0442\u0447\u0430\u0439\u0448\u0438\u043c \u043f\u0443\u0442\u0435\u043c \u0438\u0437 \u0432\u0441\u0435\u0445 \u0442\u0430\u043a\u0438\u0445 \u043f\u0443\u0442\u0435\u0439, \u0437\u043d\u0430\u0447\u0438\u0442, \u0435\u0433\u043e \u0434\u043b\u0438\u043d\u0430 \u0440\u0430\u0432\u043d\u0430 <code>F[k-1][j]<\/code>, \u0430 \u0434\u043b\u0438\u043d\u0430 \u043f\u0443\u0442\u0438 \u0434\u043e \u0432\u0435\u0440\u0448\u0438\u043d\u044b <code>i<\/code> \u0440\u0430\u0432\u043d\u0430 <code>F[k-1][j] + W[j][i]<\/code>, \u0433\u0434\u0435 <code>W[j][i]<\/code> \u0435\u0441\u0442\u044c \u0432\u0435\u0441 \u0440\u0435\u0431\u0440\u0430 <code>j-i<\/code>. \u0414\u0430\u043b\u044c\u0448\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043f\u0435\u0440\u0435\u0431\u0440\u0430\u0442\u044c \u0432\u0441\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b <code>j<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u043e\u0433\u0443\u0442 \u0432\u044b\u0441\u0442\u0443\u043f\u0430\u0442\u044c \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0445, \u0438 \u0432\u044b\u0431\u0440\u0430\u0442\u044c \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 <code>F[k-1][j] + W[j][i]<\/code>.<\/p>\n<p>\u041f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c:<\/p>\n<pre><code class=\"python\">INF = 10 ** 9 F = [[INF] * N for i in range(N)] F[0][start] = 0  for k in range(1, N):      for i in range(N):           F[k][i] = F[k - 1][i]          for j in range(N):              if F[k - 1][j] + W[j][i] &lt; F[k][i]:                  F[k][i] = F[k - 1][j] + W[j][i] <\/code><\/pre>\n<p>\u041e\u0447\u0435\u0432\u0438\u0434\u043d\u043e, \u0447\u0442\u043e \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0442\u0430\u043a\u043e\u0433\u043e \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 <code>O(n^3)<\/code>.<\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043c\u043e\u0434\u0438\u0444\u0438\u0446\u0438\u0440\u0443\u0435\u043c \u044d\u0442\u043e\u0442 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c. \u041f\u0440\u0435\u0436\u0434\u0435 \u0432\u0441\u0435\u0433\u043e, \u0441\u0434\u0435\u043b\u0430\u0435\u043c \u043c\u0430\u0441\u0441\u0438\u0432 <code>F<\/code> \u043e\u0434\u043d\u043e\u043c\u0435\u0440\u043d\u044b\u043c &#8212; \u00ab\u0441\u043a\u043b\u0435\u0438\u043c\u00bb \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f <code>F[k][i]<\/code> \u0434\u043b\u044f \u0440\u0430\u0437\u043d\u044b\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 <code>k<\/code>, \u0431\u0443\u0434\u0435\u043c \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0435 <code>F[i]<\/code> \u043a\u0440\u0430\u0442\u0447\u0430\u0439\u0448\u0435\u0435 \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u043e\u0435 \u0440\u0430\u0441\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0434\u043e \u0432\u0435\u0440\u0448\u0438\u043d\u044b <code>i<\/code>, \u0443\u043b\u0443\u0447\u0448\u0430\u044f \u0435\u0433\u043e \u043f\u043e \u0445\u043e\u0434\u0443. \u041f\u043e\u043b\u0443\u0447\u0438\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u043a\u043e\u0434:<\/p>\n<pre><code class=\"python\">INF = 10 ** 9  F = [INF] * N  F[start] = 0  for k in range(1, N):     for i in range(N):         for j in range(N):             if F[j] + W[j][i] &lt; F[i]:                 F[i] = F[j] + W[j][i] <\/code><\/pre>\n<p>\u041f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0435 \u0434\u0432\u0435 \u0441\u0442\u0440\u043e\u0447\u043a\u0438 \u0435\u0441\u0442\u044c \u043d\u0438 \u0447\u0442\u043e \u0438\u043d\u043e\u0435, \u043a\u0430\u043a \u0440\u0435\u043b\u0430\u043a\u0441\u0430\u0446\u0438\u044f \u0440\u0435\u0431\u0440\u0430 <code>j-i<\/code>, \u043a\u0430\u043a \u044d\u0442\u043e \u0434\u0435\u043b\u0430\u0435\u0442\u0441\u044f \u0432 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0435 \u0414\u0435\u0439\u043a\u0441\u0442\u0440\u044b. \u0410 \u0434\u0432\u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0445 \u0446\u0438\u043a\u043b\u0430 \u043f\u043e \u0432\u0435\u0440\u0448\u0438\u043d\u0430\u043c <code>j<\/code> \u0438 <code>i<\/code> \u0441 \u0440\u0435\u043b\u0430\u043a\u0441\u0430\u0446\u0438\u0435\u0439 \u0440\u0435\u0431\u0440\u0430 <code>j-i<\/code> \u043f\u0440\u043e\u0441\u0442\u043e \u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u0440\u0435\u043b\u0430\u043a\u0441\u0430\u0446\u0438\u0435\u0439 \u0432\u0441\u0435\u0445 \u0440\u0435\u0431\u0435\u0440 \u0432 \u0433\u0440\u0430\u0444\u0435. \u041d\u043e \u0435\u0441\u043b\u0438 \u0433\u0440\u0430\u0444 \u00ab\u0440\u0430\u0437\u0440\u0435\u0436\u0435\u043d\u043d\u044b\u0439\u00bb, \u0442\u043e \u0435\u0433\u043e \u0443\u0434\u043e\u0431\u043d\u043e \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u043d\u0435 \u0432 \u0432\u0438\u0434\u0435 \u043c\u0430\u0442\u0440\u0438\u0446\u044b \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438, \u0430 \u0432 \u0432\u0438\u0434\u0435 \u0441\u043f\u0438\u0441\u043a\u043e\u0432 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438, \u0442\u043e\u0433\u0434\u0430 \u043f\u0435\u0440\u0435\u0431\u043e\u0440 \u0432\u0441\u0435\u0445 \u0440\u0435\u0431\u0435\u0440 \u0432 \u0433\u0440\u0430\u0444\u0435 \u043c\u043e\u0436\u043d\u043e \u043e\u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0438\u0442\u044c \u0431\u044b\u0441\u0442\u0440\u0435\u0435, \u0447\u0435\u043c \u043f\u0435\u0440\u0435\u0431\u0438\u0440\u0430\u044f \u0432\u0441\u0435 \u043f\u0430\u0440\u044b \u0432\u0435\u0440\u0448\u0438\u043d.<\/p>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/0e1\/ee9\/cd0\/0e1ee9cd0ff171ee246ad5844ef4cadf.gif\" width=\"1\" height=\"1\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/0e1\/ee9\/cd0\/0e1ee9cd0ff171ee246ad5844ef4cadf.gif\"\/><\/figure>\n<p>\u041a\u0441\u0442\u0430\u0442\u0438, \u0432\u043e\u0442 <a href=\"https:\/\/habr.com\/ru\/articles\/487742\/\" rel=\"noopener noreferrer nofollow\">\u0433\u043e\u0434\u043d\u0430\u044f \u0445\u0430\u0431\u0440\u043e\u0441\u0442\u0430\u0442\u044c\u044f<\/a> \u043e \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u0438 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 \u0411\u0435\u043b\u043b\u043c\u0430\u043d\u0430-\u0424\u043e\u0440\u0434\u0430 \u0432 \u0430\u0440\u0431\u0438\u0442\u0440\u0430\u0436\u043d\u043e\u0439 \u0442\u043e\u0440\u0433\u043e\u0432\u043b\u0435.<\/p>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/2e7\/b3a\/895\/2e7b3a895f9b1bb28f4a88846eee534e.gif\" width=\"1\" height=\"1\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/2e7\/b3a\/895\/2e7b3a895f9b1bb28f4a88846eee534e.gif\"\/><\/figure>\n<h3>\u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0424\u043b\u043e\u0439\u0434\u0430-\u0423\u043e\u0440\u0448\u0435\u043b\u043b\u0430 (Floyd-Warshall Algorithm)<\/h3>\n<p><strong>\u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0424\u043b\u043e\u0439\u0434\u0430\u2013\u0423\u043e\u0440\u0448\u0435\u043b\u043b\u0430<\/strong> \u2014 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u043d\u0430\u0445\u043e\u0436\u0434\u0435\u043d\u0438\u044f \u0434\u043b\u0438\u043d \u043a\u0440\u0430\u0442\u0447\u0430\u0439\u0448\u0438\u0445 \u043f\u0443\u0442\u0435\u0439 \u043c\u0435\u0436\u0434\u0443 \u0432\u0441\u0435\u043c\u0438 \u043f\u0430\u0440\u0430\u043c\u0438 \u0432\u0435\u0440\u0448\u0438\u043d \u0432\u043e \u0432\u0437\u0432\u0435\u0448\u0435\u043d\u043d\u043e\u043c \u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u043c \u0433\u0440\u0430\u0444\u0435. \u0420\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u043e, \u0435\u0441\u043b\u0438 \u0432 \u0433\u0440\u0430\u0444\u0435 \u043d\u0435\u0442 \u0446\u0438\u043a\u043b\u043e\u0432 \u043e\u0442\u0440\u0438\u0446\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0439 \u0432\u0435\u043b\u0438\u0447\u0438\u043d\u044b, \u0430 \u0432 \u0441\u043b\u0443\u0447\u0430\u0435, \u043a\u043e\u0433\u0434\u0430 \u0442\u0430\u043a\u043e\u0439 \u0446\u0438\u043a\u043b \u0435\u0441\u0442\u044c, \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043d\u0430\u0439\u0442\u0438 \u0445\u043e\u0442\u044f \u0431\u044b \u043e\u0434\u0438\u043d \u0442\u0430\u043a\u043e\u0439 \u0446\u0438\u043a\u043b. \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0437\u0430 <code>O(n^3)<\/code> \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 <code>O(n^2)<\/code> \u043f\u0430\u043c\u044f\u0442\u0438.<\/p>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 \u0440\u0430\u0431\u043e\u0442\u044b \u0442\u0430\u043a\u043e\u0433\u043e \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/fd7\/51b\/74e\/fd751b74e2b268994c69c2b2a105c0e3.png\" width=\"5166\" height=\"1344\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/fd7\/51b\/74e\/fd751b74e2b268994c69c2b2a105c0e3.png\"\/><\/figure>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/0dc\/0ba\/df4\/0dc0badf404397d28aa2c575e5e8886f.gif\" width=\"500\" height=\"241\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/0dc\/0ba\/df4\/0dc0badf404397d28aa2c575e5e8886f.gif\"\/><\/figure>\n<p>\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043d\u0430 Python \u043e\u0447\u0435\u043d\u044c \u043f\u0440\u043e\u0441\u0442\u0430:<\/p>\n<pre><code class=\"python\">A = [[[INF for j in range(n)] for i in range(n)] for k in range(n + 1)] for i in range(n):     for j in range(n):         A[0][i][j] = W[i][j]  for k in range(1, n + 1):     for i in range(n):         for j in range(n):             A[k][i][j] = min(A[k-1][i][j], A[k-1][i][k-1] + A[k-1][k-1][j]) <\/code><\/pre>\n<p>\u0412\u043d\u0435\u0448\u043d\u0438\u0439 \u0446\u0438\u043a\u043b \u0432 \u044d\u0442\u043e\u043c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0435 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u043f\u0435\u0440\u0435\u0431\u0438\u0440\u0430\u0435\u0442 \u0432\u0441\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b, \u0437\u0430\u0442\u0435\u043c \u043f\u044b\u0442\u0430\u0435\u0442\u0441\u044f \u0443\u043b\u0443\u0447\u0448\u0438\u0442\u044c \u043f\u0443\u0442\u0438 \u0438\u0437 <code>i<\/code> \u0432 <code>j<\/code>, \u0440\u0430\u0437\u0440\u0435\u0448\u0438\u0432 \u0438\u043c \u043f\u0440\u043e\u0445\u043e\u0434\u0438\u0442\u044c \u0447\u0435\u0440\u0435\u0437 \u0432\u044b\u0431\u0440\u0430\u043d\u043d\u0443\u044e \u0432\u0435\u0440\u0448\u0438\u043d\u0443.<\/p>\n<p>\u0423\u043f\u0440\u043e\u0441\u0442\u0438\u043c \u044d\u0442\u043e\u0442 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c, \u0438\u0437\u0431\u0430\u0432\u0438\u0432\u0448\u0438\u0441\u044c \u043e\u0442 \u00ab\u0442\u0440\u0435\u0445\u043c\u0435\u0440\u043d\u043e\u0441\u0442\u0438\u00bb \u043c\u0430\u0441\u0441\u0438\u0432\u0430 <code>A<\/code>: \u0431\u0443\u0434\u0435\u043c \u0442\u043e\u043b\u044c\u043a\u043e \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043a\u0440\u0430\u0442\u0447\u0430\u0439\u0448\u0435\u0433\u043e \u043f\u0443\u0442\u0438 \u0438\u0437 <code>i<\/code> \u0432 <code>j<\/code> \u0432 <code>A[i][j]<\/code>, \u0430 \u043f\u0440\u0438 \u0443\u043b\u0443\u0447\u0448\u0435\u043d\u0438\u0438 \u043f\u0443\u0442\u0438 \u0431\u0443\u0434\u0435\u043c \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043d\u043e\u0432\u0443\u044e \u0434\u043b\u0438\u043d\u0443 \u043f\u0443\u0442\u0438 \u0442\u0430\u043a\u0436\u0435 \u0432 <code>A[i][j]<\/code>. \u0422\u0430\u043a\u0436\u0435 \u0438\u0437\u043c\u0435\u043d\u0438\u043c \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0446\u0438\u043a\u043b\u0430 \u043f\u043e \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 <code>k<\/code>, \u0437\u0430\u043c\u0435\u043d\u0438\u0432 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 <code>k-1<\/code> \u043d\u0430 <code>k<\/code>. \u041f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f \u0442\u0430\u043a:<\/p>\n<pre><code class=\"python\">A = [[W[i][j] for j in range(n)] for i in range(n)]  for k in range(n):      for i in range(n):         for j in range(n):              A[i][j] = min(A[i][j], A[i][k] + A[k][j]) <\/code><\/pre>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/bc6\/58b\/ac7\/bc658bac7fa56b97126bf386766eda90.gif\" width=\"1\" height=\"1\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/bc6\/58b\/ac7\/bc658bac7fa56b97126bf386766eda90.gif\"\/><\/figure>\n<h3>\u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u041f\u0440\u0438\u043c\u0430 (Prim&#8217;s Algorithm)<\/h3>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/c33\/22f\/56c\/c3322f56cc0636cd7de45def9c8ce2ee.png\" width=\"1972\" height=\"1524\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/c33\/22f\/56c\/c3322f56cc0636cd7de45def9c8ce2ee.png\"\/><\/figure>\n<p><strong>\u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u041f\u0440\u0438\u043c\u0430<\/strong> \u2014 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u044f \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u043e\u0441\u0442\u043e\u0432\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 \u0432\u0437\u0432\u0435\u0448\u0435\u043d\u043d\u043e\u0433\u043e \u0441\u0432\u044f\u0437\u043d\u043e\u0433\u043e \u043d\u0435\u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0433\u0440\u0430\u0444\u0430. \u0411\u044b\u043b \u043e\u0442\u043a\u0440\u044b\u0442 \u042f\u0440\u043d\u0438\u043a\u043e\u043c, \u043f\u043e\u0442\u043e\u043c \u043f\u0435\u0440\u0435\u043e\u0442\u043a\u0440\u044b\u0442 \u041f\u0440\u0438\u043c\u043e\u043c \u0438 \u0414\u0435\u0439\u043a\u0441\u0442\u0440\u043e\u0439, \u043d\u043e \u0431\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u0443 \u0438\u0437\u0432\u0435\u0441\u0442\u0435\u043d \u0438\u043c\u0435\u043d\u043d\u043e \u043a\u0430\u043a \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u041f\u0440\u0438\u043c\u0430.<\/p>\n<p>\u041f\u043e\u0445\u043e\u0436 \u043d\u0430 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0414\u0435\u0439\u043a\u0441\u0442\u0440\u044b \u0434\u043b\u044f \u043d\u0430\u0445\u043e\u0436\u0434\u0435\u043d\u0438\u044f \u043f\u0443\u0442\u0435\u0439 \u0438\u0437 \u043e\u0434\u043d\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b. \u0417\u0434\u0435\u0441\u044c \u0434\u0435\u0440\u0435\u0432\u043e \u0441\u0442\u0440\u043e\u0438\u0442\u0441\u044f, \u043d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u043b\u044c\u043d\u043e \u0432\u044b\u0431\u0440\u0430\u043d\u043d\u043e\u0439 \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b. \u0422\u0435\u043a\u0443\u0449\u0435\u0435 \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u2014 \u0432\u0441\u0435\u0433\u0434\u0430 \u0434\u0435\u0440\u0435\u0432\u043e. \u041d\u0430 \u043a\u0430\u0436\u0434\u043e\u043c \u0448\u0430\u0433\u0435 \u043a \u0434\u0435\u0440\u0435\u0432\u0443 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0440\u0435\u0431\u0440\u043e \u0438\u0437 \u043e\u0434\u043d\u043e\u0439 \u0438\u0437 \u0443\u0436\u0435 \u0438\u043c\u0435\u044e\u0449\u0438\u0445\u0441\u044f \u0432 \u043d\u0451\u043c \u0432\u0435\u0440\u0448\u0438\u043d \u0432 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u0443\u044e \u0435\u0449\u0451 \u043d\u0435 \u0437\u0430\u0434\u0435\u0439\u0441\u0442\u0432\u043e\u0432\u0430\u043d\u043d\u0443\u044e \u0432\u0435\u0440\u0448\u0438\u043d\u0443 \u2014 \u043a\u043e\u0440\u043e\u0447\u0435, \u0442\u0430\u043a\u043e\u0435 \u0440\u0435\u0431\u0440\u043e, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043d\u0435 \u043f\u0440\u0438\u0432\u0435\u0434\u0451\u0442 \u043a \u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u044e \u0446\u0438\u043a\u043b\u0430. \u0418\u0437 \u0432\u0441\u0435\u0445 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0445 \u0440\u0451\u0431\u0435\u0440 \u0432\u044b\u0431\u0438\u0440\u0430\u0435\u0442\u0441\u044f \u0440\u0435\u0431\u0440\u043e \u0441 \u043d\u0430\u0438\u043c\u0435\u043d\u044c\u0448\u0438\u043c \u0432\u0435\u0441\u043e\u043c.<\/p>\n<p>\u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0434\u0430\u043d\u043d\u044b\u0445: \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0441 \u043f\u0440\u0438\u043e\u0440\u0438\u0442\u0435\u0442\u0430\u043c\u0438, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0445\u0440\u0430\u043d\u044f\u0442\u0441\u044f \u0432\u0441\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b, \u0435\u0449\u0451 \u043d\u0435 \u043f\u043e\u043f\u0430\u0432\u0448\u0438\u0435 \u0432 \u0434\u0435\u0440\u0435\u0432\u043e. \u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043a\u0430\u0436\u0434\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u2014 \u044d\u0442\u043e \u043d\u0430\u0438\u043c\u0435\u043d\u044c\u0448\u0438\u0439 \u0432\u0435\u0441 \u0440\u0435\u0431\u0440\u0430, \u0441\u043e\u0435\u0434\u0438\u043d\u044f\u044e\u0449\u0438\u0439 \u0435\u0451 \u0441 \u0434\u0435\u0440\u0435\u0432\u043e\u043c. \u0412\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f <code>|V|<\/code> \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0439 \u0432\u043d\u0435\u0448\u043d\u0435\u0433\u043e \u0446\u0438\u043a\u043b\u0430. \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u043d\u0430\u0434 \u043e\u0447\u0435\u0440\u0435\u0434\u044c\u044e \u0441 \u043f\u0440\u0438\u043e\u0440\u0438\u0442\u0435\u0442\u0430\u043c\u0438 \u2014 <code>log |V|<\/code>. \u0412\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0439 \u0446\u0438\u043a\u043b \u043f\u043e <code>v<\/code>: \u0437\u0430 \u0432\u0441\u0451 \u0432\u0440\u0435\u043c\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f <code>2|E|<\/code> \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0439, \u0430 \u0432\u043d\u0443\u0442\u0440\u0438 \u2014 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f \u043d\u0430\u0434 \u043e\u0447\u0435\u0440\u0435\u0434\u044c\u044e \u0441 \u043f\u0440\u0438\u043e\u0440\u0438\u0442\u0435\u0442\u0430\u043c\u0438, <code>log |V|<\/code>. \u0412\u0440\u0435\u043c\u044f: <code>|E| log |V|<\/code>.<\/p>\n<p>\u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u043c\u043e\u0436\u043d\u043e \u043e\u043f\u0438\u0441\u0430\u0442\u044c \u0442\u0430\u043a:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/847\/92f\/965\/84792f96516c674a7a02892ce7536698.png\" width=\"2606\" height=\"1142\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/847\/92f\/965\/84792f96516c674a7a02892ce7536698.png\"\/><\/figure>\n<p>\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 \u041f\u0440\u0438\u043c\u0430 \u043d\u0430 Python:<\/p>\n<pre><code class=\"python\">import math  def get_min(R, U):     rm = (math.inf, -1, -1)     for v in U:         rr = min(R, key=lambda x: x[0] if (x[1] == v or x[2] == v) and (x[1] not in U or x[2] not in U) else math.inf)         if rm[0] > rr[0]:             rm = rr      return rm   # \u0441\u043f\u0438\u0441\u043e\u043a \u0440\u0435\u0431\u0435\u0440 \u0433\u0440\u0430\u0444\u0430 (\u0434\u043b\u0438\u043d\u0430, \u0432\u0435\u0440\u0448\u0438\u043d\u0430 1, \u0432\u0435\u0440\u0448\u0438\u043d\u0430 2) # \u043f\u0435\u0440\u0432\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f, \u0435\u0441\u043b\u0438 \u043d\u0435\u0442 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0445 \u0440\u0435\u0431\u0435\u0440 R = [(math.inf, -1, -1), (13, 1, 2), (18, 1, 3), (17, 1, 4), (14, 1, 5), (22, 1, 6),      (26, 2, 3), (19, 2, 5), (30, 3, 4), (22, 4, 6)]  N = 6    # \u0447\u0438\u0441\u043b\u043e \u0432\u0435\u0440\u0448\u0438\u043d \u0432 \u0433\u0440\u0430\u0444\u0435 U = {1}  # \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u043d\u044b\u0445 \u0432\u0435\u0440\u0448\u0438\u043d T = []   # \u0441\u043f\u0438\u0441\u043e\u043a \u0440\u0435\u0431\u0435\u0440 \u043e\u0441\u0442\u043e\u0432\u0430  while len(U) &lt; N:     r = get_min(R, U)     # \u0440\u0435\u0431\u0440\u043e \u0441 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u043c \u0432\u0435\u0441\u043e\u043c     if r[0] == math.inf:  # \u0435\u0441\u043b\u0438 \u0440\u0435\u0431\u0435\u0440 \u043d\u0435\u0442, \u0442\u043e \u043e\u0441\u0442\u043e\u0432 \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d         break      T.append(r)  # \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0440\u0435\u0431\u0440\u043e \u0432 \u043e\u0441\u0442\u043e\u0432     U.add(r[1])  # \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0432 \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e U     U.add(r[2])  print(T) # [(13, 1, 2), (14, 1, 5), (17, 1, 4), (18, 1, 3), (22, 1, 6)] <\/code><\/pre>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/2c8\/c3c\/f2a\/2c8c3cf2a5b39368b287d288e69ca529.gif\" width=\"1\" height=\"1\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/2c8\/c3c\/f2a\/2c8c3cf2a5b39368b287d288e69ca529.gif\"\/><\/figure>\n<h3>\u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u041a\u0440\u0430\u0441\u043a\u0430\u043b\u0430 (Kruskal&#8217;s Algorithm)<\/h3>\n<p>\u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u041a\u0440\u0430\u0441\u043a\u0430\u043b\u0430 \u043c\u043e\u0436\u043d\u043e \u043e\u043f\u0438\u0441\u0430\u0442\u044c \u0442\u0430\u043a:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/efa\/657\/d2c\/efa657d2c3fafc1ee42867863a2d5a7f.png\" width=\"2606\" height=\"800\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/efa\/657\/d2c\/efa657d2c3fafc1ee42867863a2d5a7f.png\"\/><\/figure>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/7e3\/ad4\/dbe\/7e3ad4dbe5e440853c111c7821f1221e.gif\" width=\"500\" height=\"231\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/7e3\/ad4\/dbe\/7e3ad4dbe5e440853c111c7821f1221e.gif\"\/><\/figure>\n<p><strong>\u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u041a\u0440\u0430\u0441\u043a\u0430\u043b\u0430<\/strong> \u2014 \u044d\u0442\u043e \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u043f\u043e\u0438\u0441\u043a\u0430 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u043e\u0441\u0442\u043e\u0432\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430<img loading=\"lazy\" decoding=\"async\" class=\"formula inline\" source=\"^*\" alt=\"^*\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/bef\/b12\/4ac\/befb124ac927f8e2a18eeb2172d1ae24.svg\" width=\"9\" height=\"17\"\/> (minimum spanning tree, MST) \u0432\u043e \u0432\u0437\u0432\u0435\u0448\u0435\u043d\u043d\u043e\u043c <a href=\"https:\/\/neerc.ifmo.ru\/wiki\/index.php?title=%D0%9E%D1%81%D0%BD%D0%BE%D0%B2%D0%BD%D1%8B%D0%B5_%D0%BE%D0%BF%D1%80%D0%B5%D0%B4%D0%B5%D0%BB%D0%B5%D0%BD%D0%B8%D1%8F_%D1%82%D0%B5%D0%BE%D1%80%D0%B8%D0%B8_%D0%B3%D1%80%D0%B0%D1%84%D0%BE%D0%B2#.D0.9D.D0.B5.D0.BE.D1.80.D0.B8.D0.B5.D0.BD.D1.82.D0.B8.D1.80.D0.BE.D0.B2.D0.B0.D0.BD.D0.BD.D1.8B.D0.B5_.D0.B3.D1.80.D0.B0.D1.84.D1.8B\" rel=\"noopener noreferrer nofollow\">\u043d\u0435\u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u043c \u0441\u0432\u044f\u0437\u043d\u043e\u043c \u0433\u0440\u0430\u0444\u0435<\/a>.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"formula inline\" source=\"^*\" alt=\"^*\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/5cb\/e4b\/77b\/5cbe4b77ba5e49ef98e5ccd9c0e18ab3.svg\" width=\"9\" height=\"17\"\/>\u200b\u200b\u200b\u043e\u0441\u0442\u043e\u0432\u043d\u044b\u043c \u0434\u0435\u0440\u0435\u0432\u043e\u043c \u0433\u0440\u0430\u0444\u0430 \u043d\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0434\u0435\u0440\u0435\u0432\u043e, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u0437 \u043d\u0435\u0433\u043e \u043f\u0443\u0442\u0451\u043c \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0440\u0451\u0431\u0435\u0440. \u0423 \u0433\u0440\u0430\u0444\u0430 \u043c\u043e\u0436\u0435\u0442 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u043e\u0432\u0430\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043e\u0441\u0442\u043e\u0432\u043d\u044b\u0445 \u0434\u0435\u0440\u0435\u0432\u044c\u0435\u0432, \u0438 \u0447\u0430\u0449\u0435 \u0432\u0441\u0435\u0445 \u0438\u0445 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043c\u043d\u043e\u0433\u043e.<\/p>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/48c\/10c\/1a1\/48c10c1a15468697753eefc8a2b10399.gif\" width=\"1\" height=\"1\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/48c\/10c\/1a1\/48c10c1a15468697753eefc8a2b10399.gif\"\/><\/figure>\n<p>\u0412\u043e\u0442, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043e\u0434\u043d\u043e \u0438\u0437 \u043e\u0441\u0442\u043e\u0432\u043d\u044b\u0445 \u0434\u0435\u0440\u0435\u0432\u044c\u0435\u0432 (\u0440\u0451\u0431\u0440\u0430 \u0432\u044b\u0434\u0435\u043b\u0435\u043d\u044b \u0441\u0438\u043d\u0438\u043c \u0446\u0432\u0435\u0442\u043e\u043c) \u0440\u0435\u0448\u0451\u0442\u043a\u043e\u043e\u0431\u0440\u0430\u0437\u043d\u043e\u0433\u043e \u0433\u0440\u0430\u0444\u0430.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/b22\/510\/7e5\/b225107e56c74fc1a1e49372bd0bace6.png\" width=\"2606\" height=\"960\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/b22\/510\/7e5\/b225107e56c74fc1a1e49372bd0bace6.png\"\/><\/figure>\n<p>\u0414\u043b\u044f \u0432\u0437\u0432\u0435\u0448\u0435\u043d\u043d\u044b\u0445 \u0433\u0440\u0430\u0444\u043e\u0432 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u043f\u043e\u043d\u044f\u0442\u0438\u0435 <em>\u0432\u0435\u0441\u0430<\/em> \u043e\u0441\u0442\u043e\u0432\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043e \u043a\u0430\u043a \u0441\u0443\u043c\u043c\u0430 \u0432\u0435\u0441\u043e\u0432 \u0432\u0441\u0435\u0445 \u0440\u0451\u0431\u0435\u0440, \u0432\u0445\u043e\u0434\u044f\u0449\u0438\u0445 \u0432 \u043e\u0441\u0442\u043e\u0432\u043d\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e. \u0418\u0437 \u043d\u0435\u0433\u043e \u043d\u0430\u0442\u0443\u0440\u0430\u043b\u044c\u043d\u043e \u0432\u044b\u0442\u0435\u043a\u0430\u0435\u0442 \u043f\u043e\u043d\u044f\u0442\u0438\u0435 <strong>\u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u043e\u0441\u0442\u043e\u0432\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430<\/strong> \u2014 \u043e\u0441\u0442\u043e\u0432\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 \u0441 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u043c \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u043c \u0432\u0435\u0441\u043e\u043c.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/ab3\/296\/013\/ab329601307dddae06880a2af7defd0f.png\" width=\"1994\" height=\"960\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/ab3\/296\/013\/ab329601307dddae06880a2af7defd0f.png\"\/><\/figure>\n<p>\u0414\u043b\u044f \u043d\u0430\u0445\u043e\u0436\u0434\u0435\u043d\u0438\u044f \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u043e\u0441\u0442\u043e\u0432\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 \u0433\u0440\u0430\u0444\u0430 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0442 \u0434\u0432\u0430 \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0445 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430: \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u041f\u0440\u0438\u043c\u0430 \u0438 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u041a\u0440\u0430\u0441\u043a\u0430\u043b\u0430. \u041e\u043d\u0438 \u043e\u0431\u0430 \u0438\u043c\u0435\u044e\u0442 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c <code>O(M log(N))<\/code>, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0432\u044b\u0431\u043e\u0440 \u043e\u0434\u043d\u043e\u0433\u043e \u0438\u0437 \u043d\u0438\u0445 \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u0432\u0430\u0448\u0438\u0445 \u043b\u0438\u0447\u043d\u044b\u0445 \u043f\u0440\u0435\u0434\u043f\u043e\u0447\u0442\u0435\u043d\u0438\u0439.<\/p>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/8a1\/780\/05f\/8a178005f3867a34a3637c60063a3b2c.gif\" width=\"1\" height=\"1\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/8a1\/780\/05f\/8a178005f3867a34a3637c60063a3b2c.gif\"\/><\/figure>\n<p>\u0415\u0449\u0451 \u043e\u0434\u043d\u0430 \u0432\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 \u041a\u0440\u0430\u0441\u043a\u0430\u043b\u0430:<\/p>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/fcc\/a87\/c20\/fcca87c206015aaa1797597595c51e2b.gif\" width=\"480\" height=\"480\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/fcc\/a87\/c20\/fcca87c206015aaa1797597595c51e2b.gif\"\/><\/figure>\n<p>\u0410 \u0432\u043e\u0442 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f \u043f\u043e \u0448\u0430\u0433\u0430\u043c:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/766\/8e6\/6f0\/7668e66f0ff1dca354ba33f79133fa93.png\" width=\"1376\" height=\"1764\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/766\/8e6\/6f0\/7668e66f0ff1dca354ba33f79133fa93.png\"\/><\/figure>\n<p>\u0420\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 \u041a\u0440\u0430\u0441\u043a\u0430\u043b\u0430 \u043d\u0430 Python \u043c\u043e\u0436\u043d\u043e \u0442\u0430\u043a, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<pre><code class=\"python\"># \u0441\u043f\u0438\u0441\u043e\u043a \u0440\u0435\u0431\u0435\u0440 \u0433\u0440\u0430\u0444\u0430 (\u0434\u043b\u0438\u043d\u0430, \u0432\u0435\u0440\u0448\u0438\u043d\u0430 1, \u0432\u0435\u0440\u0448\u0438\u043d\u0430 2) R = [(13, 1, 2), (18, 1, 3), (17, 1, 4), (14, 1, 5), (22, 1, 6),      (26, 2, 3), (22, 2, 5), (3, 3, 4), (19, 4, 6)]  Rs = sorted(R, key=lambda x: x[0]) U = set()   # \u0441\u043f\u0438\u0441\u043e\u043a \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u043d\u044b\u0445 \u0432\u0435\u0440\u0448\u0438\u043d D = {}      # \u0441\u043b\u043e\u0432\u0430\u0440\u044c \u0441\u043f\u0438\u0441\u043a\u0430 \u0438\u0437\u043e\u043b\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0445 \u0433\u0440\u0443\u043f\u043f \u0432\u0435\u0440\u0448\u0438\u043d T = []      # \u0441\u043f\u0438\u0441\u043e\u043a \u0440\u0435\u0431\u0435\u0440 \u043e\u0441\u0442\u043e\u0432\u0430  for r in Rs:     if r[1] not in U or r[2] not in U:  # \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u0434\u043b\u044f \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f \u0446\u0438\u043a\u043b\u043e\u0432 \u0432 \u043e\u0441\u0442\u043e\u0432\u0435         if r[1] not in U and r[2] not in U: # \u0435\u0441\u043b\u0438 \u043e\u0431\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u043d\u0435 \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u044b, \u0442\u043e             D[r[1]] = [r[1], r[2]]          # \u0444\u043e\u0440\u043c\u0438\u0440\u0443\u0435\u043c \u0432 \u0441\u043b\u043e\u0432\u0430\u0440\u0435 \u043a\u043b\u044e\u0447 \u0441 \u043d\u043e\u043c\u0435\u0440\u0430\u043c\u0438 \u0432\u0435\u0440\u0448\u0438\u043d             D[r[2]] = D[r[1]]               # \u0438 \u0441\u0432\u044f\u0437\u044b\u0432\u0430\u0435\u043c \u0438\u0445 \u0441 \u043e\u0434\u043d\u0438\u043c\u0438 \u0438 \u0442\u0435\u043c\u0438 \u0436\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u0430\u043c\u0438          else:                           # \u0438\u043d\u0430\u0447\u0435             if not D.get(r[1]):             # \u0435\u0441\u043b\u0438 \u0432 \u0441\u043b\u043e\u0432\u0430\u0440\u0435 \u043d\u0435\u0442 \u043f\u0435\u0440\u0432\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b, \u0442\u043e                 D[r[2]].append(r[1])        # \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0432 \u0441\u043f\u0438\u0441\u043e\u043a \u043f\u0435\u0440\u0432\u0443\u044e \u0432\u0435\u0440\u0448\u0438\u043d\u0443                 D[r[1]] = D[r[2]]           # \u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u043a\u043b\u044e\u0447 \u0441 \u043d\u043e\u043c\u0435\u0440\u043e\u043c \u043f\u0435\u0440\u0432\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b             else:                 D[r[1]].append(r[2])        # \u0438\u043d\u0430\u0447\u0435, \u0432\u0441\u0435 \u0442\u043e \u0436\u0435 \u0441\u0430\u043c\u043e\u0435 \u0434\u0435\u043b\u0430\u0435\u043c \u0441\u043e 2 \u0432\u0435\u0440\u0448\u0438\u043d\u043e\u0439                 D[r[2]] = D[r[1]]          T.append(r)             # \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0440\u0435\u0431\u0440\u043e \u0432 \u043e\u0441\u0442\u043e\u0432         U.add(r[1])             # \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0432 \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e U         U.add(r[2])  for r in Rs:    # \u043f\u0440\u043e\u0445\u043e\u0434\u0438\u043c \u043f\u043e \u0440\u0435\u0431\u0440\u0430\u043c \u0432\u0442\u043e\u0440\u043e\u0439 \u0440\u0430\u0437 \u0438 \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u044f\u0435\u043c \u0440\u0430\u0437\u0440\u043e\u0437\u043d\u0435\u043d\u043d\u044b\u0435 \u0433\u0440\u0443\u043f\u043f\u044b \u0432\u0435\u0440\u0448\u0438\u043d     if r[2] not in D[r[1]]:     # \u0435\u0441\u043b\u0438 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u043f\u0440\u0438\u043d\u0430\u0434\u043b\u0435\u0436\u0430\u0442 \u0440\u0430\u0437\u043d\u044b\u043c \u0433\u0440\u0443\u043f\u043f\u0430\u043c, \u0442\u043e \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u044f\u0435\u043c         T.append(r)             # \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0440\u0435\u0431\u0440\u043e \u0432 \u043e\u0441\u0442\u043e\u0432         gr1 = D[r[1]]         D[r[1]] += D[r[2]]      # \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u0435\u043c \u0441\u043f\u0438\u0441\u043a\u0438 \u0434\u0432\u0443\u0445 \u0433\u0440\u0443\u043f\u043f \u0432\u0435\u0440\u0448\u0438\u043d         D[r[2]] += gr1  print(T) # [(3, 3, 4), (13, 1, 2), (14, 1, 5), (19, 4, 6), (17, 1, 4)] <\/code><\/pre>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/336\/658\/7e2\/3366587e24fb1dd2f1fd6f8b731d8fde.gif\" width=\"1\" height=\"1\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/336\/658\/7e2\/3366587e24fb1dd2f1fd6f8b731d8fde.gif\"\/><\/figure>\n<h3>\u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u041a\u043e\u0441\u0430\u0440\u0430\u0434\u0436\u0443 (Kosaraju&#8217;s Algorithm)<\/h3>\n<p><strong>\u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u041a\u043e\u0441\u0430\u0440\u0430\u0434\u0436\u0443<\/strong> \u2014 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u043f\u043e\u0438\u0441\u043a\u0430 \u043e\u0431\u043b\u0430\u0441\u0442\u0435\u0439 \u0441\u0438\u043b\u044c\u043d\u043e\u0439 \u0441\u0432\u044f\u0437\u043d\u043e\u0441\u0442\u0438 \u0432 \u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u043c \u0433\u0440\u0430\u0444\u0435.<\/p>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/1e5\/c03\/097\/1e5c03097c810f3b1c08db143ba913ab.gif\" width=\"500\" height=\"259\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/1e5\/c03\/097\/1e5c03097c810f3b1c08db143ba913ab.gif\"\/><\/figure>\n<p><strong>\u041f\u0440\u0438\u043c\u0435\u0440 \u0440\u0430\u0431\u043e\u0442\u044b \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 \u041a\u043e\u0441\u0430\u0440\u0430\u0434\u0436\u0443 \u043f\u043e\u0448\u0430\u0433\u043e\u0432\u043e.<\/strong> \u0414\u0430\u043d \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0433\u0440\u0430\u0444:<\/p>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/817\/ea9\/e6d\/817ea9e6d17a833366f17e4807e17e50.png\" alt=\"img\" width=\"392\" height=\"211\" data-src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/817\/ea9\/e6d\/817ea9e6d17a833366f17e4807e17e50.png\"\/><\/p>\n<div><figcaption>img<\/figcaption><\/div>\n<\/figure>\n<p><strong>\u0428\u0430\u0433 1.<\/strong> \u041d\u0435\u043f\u043e\u043c\u0435\u0447\u0435\u043d\u043d\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0438\u043c\u0435\u044e\u0442 \u043c\u0435\u0442\u043a\u0443 &#8216;?&#8217; \u0421\u043f\u0440\u0430\u0432\u0430 \u043e\u0442 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u0433\u0440\u0430\u0444\u0430 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043b\u0435\u0441 DFS. \u0421\u0442\u0430\u0440\u0442\u043e\u0432\u0430\u044f \u0432\u0435\u0440\u0448\u0438\u043d\u0430 \u0432\u044b\u0431\u0438\u0440\u0430\u0435\u0442\u0441\u044f \u0432 \u043f\u043e\u0440\u044f\u0434\u043a\u0435 \u0432\u043e\u0437\u0440\u0430\u0441\u0442\u0430\u043d\u0438\u044f \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u0432.<\/p>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/bf4\/898\/44e\/bf489844e5b902b7406ae55a14398589.gif\" alt=\"img\" width=\"392\" height=\"211\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/bf4\/898\/44e\/bf489844e5b902b7406ae55a14398589.gif\"\/><\/p>\n<div><figcaption>img<\/figcaption><\/div>\n<\/figure>\n<p><strong>\u0428\u0430\u0433 2.<\/strong> \u0418\u043d\u0432\u0435\u0440\u0442\u0438\u0440\u0443\u0435\u043c \u0440\u0451\u0431\u0440\u0430<\/p>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/45f\/04d\/705\/45f04d7051f4791013d93657c44acdd1.png\" alt=\"img\" width=\"382\" height=\"178\" data-src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/45f\/04d\/705\/45f04d7051f4791013d93657c44acdd1.png\"\/><\/p>\n<div><figcaption>img<\/figcaption><\/div>\n<\/figure>\n<p><strong>\u0428\u0430\u0433 3.<\/strong> \u0412\u044b\u0431\u0438\u0440\u0430\u0435\u043c \u0441\u0442\u0430\u0440\u0442\u043e\u0432\u0443\u044e \u0432\u0435\u0440\u0448\u0438\u043d\u0443 \u0432 \u043f\u043e\u0440\u044f\u0434\u043a\u0435 \u0443\u0431\u044b\u0432\u0430\u043d\u0438\u044f \u043c\u0435\u0442\u043e\u043a. \u0422\u0430\u043a \u043a\u0430\u043a \u0442\u0435\u043f\u0435\u0440\u044c \u043d\u0430\u043c \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u0435\u043d \u043b\u0438\u0448\u044c \u043b\u0435\u0441 \u0442\u0430\u043a\u0442\u043e\u0432 DFS, \u0442\u043e \u043d\u0435\u0442 \u0441\u043c\u044b\u0441\u043b\u0430 \u0441\u0447\u0438\u0442\u0430\u0442\u044c \u0432\u0440\u0435\u043c\u044f \u0432\u044b\u0445\u043e\u0434\u0430 \u0438\u0437 \u0432\u0435\u0440\u0448\u0438\u043d, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u044f \u043d\u0435 \u043f\u043e\u043a\u0430\u0437\u0430\u043b \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0445 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u043e\u0432.<br \/> \u0421\u043f\u0440\u0430\u0432\u0430 \u043e\u0442 \u0438\u043d\u0432\u0435\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0433\u0440\u0430\u0444\u0430 \u043f\u043e\u043a\u0430\u0437\u0430\u043d \u043b\u0435\u0441 \u0442\u0430\u043a\u0442\u043e\u0432 DFS.<\/p>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/831\/120\/4d1\/8311204d160cfeae46a87198cbdcd4b8.gif\" alt=\"img\" width=\"382\" height=\"178\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/831\/120\/4d1\/8311204d160cfeae46a87198cbdcd4b8.gif\"\/><\/p>\n<div><figcaption>img<\/figcaption><\/div>\n<\/figure>\n<p><strong>\u0413\u043e\u0442\u043e\u0432\u043e.<\/strong> \u041f\u043e\u0441\u043b\u0435 \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 \u0438\u043c\u0435\u0435\u043c 3 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u044b \u0441\u0438\u043b\u044c\u043d\u043e\u0439 \u0441\u0432\u044f\u0437\u043d\u043e\u0441\u0442\u0438.<\/p>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/925\/691\/83e\/92569183e5a44fc53102e3c021a423f6.png\" alt=\"img\" width=\"372\" height=\"202\" data-src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/925\/691\/83e\/92569183e5a44fc53102e3c021a423f6.png\"\/><\/p>\n<div><figcaption>img<\/figcaption><\/div>\n<\/figure>\n<p>\u0418\u043c\u043f\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044f \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 \u041a\u043e\u0441\u0430\u0440\u0430\u0434\u0436\u0443 \u043d\u0430 Python \u043c\u043e\u0436\u0435\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u0442\u0430\u043a:<\/p>\n<pre><code class=\"python\">class Graph:   def __init__(self, edge_list, num_nodes):     self.graph = edge_list     self.rev_graph = self.reverse_graph()     self.num_nodes = num_nodes      self.traversed_nodes_p1 = []     self.traversed_nodes_p2 = []      self.counter = 0;     self.scc_size_list = []      self.scc_sl()    def reverse_graph(self):     r_graph = [];     for e in self.graph:       tail = e[0];        head = e[1];       r_graph.append([head, tail])     r_graph = sorted(r_graph, key = lambda x: x[0])     return r_graph    def dfs_p1(self, starting_node, g, t_nodes):      if (starting_node not in t_nodes):       t_nodes.insert(0, starting_node)     for i in range (len(g)):        if (g[i][0] == starting_node and g[i][1] not in t_nodes):         self.dfs_p1(g[i][1], g, t_nodes)      def dfs_loop_p1 (self):     for i in range (1, self.num_nodes+1):       self.dfs_p1(i, self.rev_graph, self.traversed_nodes_p1)    def dfs_p2(self, starting_node, g, t_nodes):      if (starting_node not in t_nodes):       self.counter += 1       t_nodes.append(starting_node)     for i in range (len(g)):        if (g[i][0] == starting_node and g[i][1] not in t_nodes):         self.dfs_p2(g[i][1], g, t_nodes)      def dfs_loop_p2 (self):     for i in self.traversed_nodes_p1:       self.counter = 0       self.dfs_p2(i, self.graph, self.traversed_nodes_p2)       if self.counter > 0:         self.scc_size_list.append(self.counter)      def scc_sl (self):     self.dfs_loop_p1()     self.dfs_loop_p2()      self.scc_size_list.sort()     self.scc_size_list.reverse()   test_edge_list = [[1,2], [1,9], [2,3], [2,8], [3,4], [3,5], [4,5], [6,5], [6,8], [7,6], [7,9], [8,3], [8,7], [9,2], [9,8]] test = Graph(test_edge_list, 9) print(test.scc_size_list)      # [5, 1, 1, 1, 1]  <\/code><\/pre>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/67b\/e5a\/cf2\/67be5acf26032ddac376400adb012ee4.gif\" width=\"1\" height=\"1\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/67b\/e5a\/cf2\/67be5acf26032ddac376400adb012ee4.gif\"\/><\/figure>\n<h2>\u0421\u0442\u0440\u043e\u043a\u043e\u0432\u044b\u0435 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u044b<\/h2>\n<h3>\u0417\u0430\u0434\u0430\u0447\u0430 \u043f\u043e\u0438\u0441\u043a\u0430 \u043f\u043e\u0434\u0441\u0442\u0440\u043e\u043a\u0438<\/h3>\n<p>\u0421\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0435 \u0440\u0430\u0437\u043d\u044b\u0445 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u043e\u0432 \u043f\u043e\u0438\u0441\u043a\u0430 \u043f\u043e\u0434\u0441\u0442\u0440\u043e\u043a\u0438 (<a href=\"https:\/\/neerc.ifmo.ru\/wiki\/index.php?title=%D0%9F%D0%BE%D0%B8%D1%81%D0%BA_%D0%BF%D0%BE%D0%B4%D1%81%D1%82%D1%80%D0%BE%D0%BA%D0%B8_%D0%B2_%D1%81%D1%82%D1%80%D0%BE%D0%BA%D0%B5\" rel=\"noopener noreferrer nofollow\">\u043f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435 \u0437\u0434\u0435\u0441\u044c<\/a>)<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/472\/4e1\/d7e\/4724e1d7e13d2b05d423491ee9fdee14.png\" width=\"2718\" height=\"3140\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/472\/4e1\/d7e\/4724e1d7e13d2b05d423491ee9fdee14.png\"\/><\/figure>\n<p>\u0412\u0430\u0436\u043d\u043e\u0441\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0438 \u043f\u043e\u0438\u0441\u043a\u0430 \u043f\u043e\u0434\u0441\u0442\u0440\u043e\u043a\u0438 \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u0430 \u2014 \u044d\u0442\u043e \u043d\u0435\u043e\u0442\u044a\u0435\u043c\u043b\u0435\u043c\u044b\u0439 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 \u043f\u043e\u0438\u0441\u043a\u043e\u0432\u044b\u0445 \u0434\u0432\u0438\u0436\u043a\u043e\u0432, \u0421\u0423\u0411\u0414, \u0442\u0435\u043a\u0441\u0442\u043e\u0432\u044b\u0445 \u0440\u0435\u0434\u0430\u043a\u0442\u043e\u0440\u043e\u0432 \u0438 \u0435\u0449\u0451 \u043c\u043d\u043e\u0433\u043e \u0447\u0435\u0433\u043e. \u0412 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043e\u0431\u043b\u0430\u0441\u0442\u044f\u0445 \u044d\u0442\u043e \u043e\u0434\u043d\u0430 \u0438\u0437 \u0431\u0430\u0437\u043e\u0432\u044b\u0445 \u0437\u0430\u0434\u0430\u0447, \u0441\u043a\u0430\u0436\u0435\u043c, \u0432 \u0431\u0438\u043e\u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0442\u0438\u043a\u0435.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/ca6\/655\/1e0\/ca66551e0d6b819f46383302ee9b4cca.png\" width=\"1522\" height=\"570\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/ca6\/655\/1e0\/ca66551e0d6b819f46383302ee9b4cca.png\"\/><\/figure>\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0436\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u044b \u043f\u043e\u0438\u0441\u043a\u0430 \u043f\u043e\u0434\u0441\u0442\u0440\u043e\u043a\u0438.<\/p>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/474\/f85\/a74\/474f85a746c7b47fc78474f3dac4053c.gif\" width=\"1\" height=\"1\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/474\/f85\/a74\/474f85a746c7b47fc78474f3dac4053c.gif\"\/><\/figure>\n<h3>\u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u041a\u043d\u0443\u0442\u0430-\u041c\u043e\u0440\u0440\u0438\u0441\u0430-\u041f\u0440\u0430\u0442\u0442\u0430<\/h3>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/d72\/5cb\/77a\/d725cb77a48b0a0d8f4ed09f56ecd548.gif\" width=\"400\" height=\"142\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/d72\/5cb\/77a\/d725cb77a48b0a0d8f4ed09f56ecd548.gif\"\/><\/figure>\n<p><strong>\u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u041a\u043d\u0443\u0442\u0430-\u041c\u043e\u0440\u0440\u0438\u0441\u0430-\u041f\u0440\u0430\u0442\u0442\u0430<\/strong> (\u041a\u041c\u041f) \u2014 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u044b\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c, \u043e\u0441\u0443\u0449\u0435\u0441\u0442\u0432\u043b\u044f\u044e\u0449\u0438\u0439 \u043f\u043e\u0438\u0441\u043a \u043f\u043e\u0434\u0441\u0442\u0440\u043e\u043a\u0438 \u0432 \u0441\u0442\u0440\u043e\u043a\u0435<a href=\"https:\/\/uproger.com\/shpargalka-dlya-algosobesa-2-grafovye-i-strokovye-algoritmy\/\" rel=\"noopener noreferrer nofollow\">.<\/a> \u0412\u0440\u0435\u043c\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 \u043b\u0438\u043d\u0435\u0439\u043d\u043e \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u043e\u0431\u044a\u0451\u043c\u0430 \u0432\u0445\u043e\u0434\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/280\/241\/de9\/280241de9dcc6a9b2941d66fa5e16b05.png\" width=\"3524\" height=\"1868\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/280\/241\/de9\/280241de9dcc6a9b2941d66fa5e16b05.png\"\/><\/figure>\n<p>\u041a\u041c\u041f-\u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u043e\u0441\u043d\u043e\u0432\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u0442\u043e\u043c \u0441\u043e\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0438, \u0447\u0442\u043e \u043f\u043e\u0441\u043b\u0435 \u0447\u0430\u0441\u0442\u0438\u0447\u043d\u043e\u0433\u043e \u0441\u043e\u0432\u043f\u0430\u0434\u0435\u043d\u0438\u044f \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0439 \u0447\u0430\u0441\u0442\u0438 \u043f\u043e\u0434\u0441\u0442\u0440\u043e\u043a\u0438 \u0441 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u043c\u0438 \u0441\u0438\u043c\u0432\u043e\u043b\u0430\u043c\u0438 \u0442\u0435\u043a\u0441\u0442\u0430 \u0444\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u0430 \u043f\u0440\u043e\u0439\u0434\u0435\u043d\u043d\u0430\u044f \u0447\u0430\u0441\u0442\u044c \u0442\u0435\u043a\u0441\u0442\u0430 \u0438 \u043c\u043e\u0436\u043d\u043e \u201c\u0432\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c\u201d \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0441\u0432\u0435\u0434\u0435\u043d\u0438\u044f (\u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0441\u0430\u043c\u043e\u0439 \u043f\u043e\u0434\u0441\u0442\u0440\u043e\u043a\u0438), \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043f\u043e\u0442\u043e\u043c \u043c\u043e\u0436\u043d\u043e \u0431\u044b\u0441\u0442\u0440\u043e \u043f\u0440\u043e\u0434\u0432\u0438\u043d\u0443\u0442\u044c\u0441\u044f \u043f\u043e \u0442\u0435\u043a\u0441\u0442\u0443.<\/p>\n<p>\u041e\u0441\u043d\u043e\u0432\u043d\u044b\u043c \u043e\u0442\u043b\u0438\u0447\u0438\u0435\u043c \u041a\u041c\u041f-\u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 \u043e\u0442 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 \u043f\u0440\u044f\u043c\u043e\u0433\u043e \u043f\u043e\u0438\u0441\u043a\u0430 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043e\u0441\u0443\u0449\u0435\u0441\u0442\u0432\u043b\u0435\u043d\u0438\u044f \u0441\u0434\u0432\u0438\u0433\u0430 \u0441\u043b\u043e\u0432\u0430 \u043d\u0435 \u043d\u0430 \u043e\u0434\u0438\u043d \u0441\u0438\u043c\u0432\u043e\u043b \u043d\u0430 \u043a\u0430\u0436\u0434\u043e\u043c \u0448\u0430\u0433\u0435 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430, \u0430 \u043d\u0430 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432. \u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u043f\u0435\u0440\u0435\u0434 \u0442\u0435\u043c \u043a\u0430\u043a \u043e\u0441\u0443\u0449\u0435\u0441\u0442\u0432\u043b\u044f\u0442\u044c \u043e\u0447\u0435\u0440\u0435\u0434\u043d\u043e\u0439 \u0441\u0434\u0432\u0438\u0433<a href=\"https:\/\/uproger.com\/shpargalka-dlya-algosobesa-2-grafovye-i-strokovye-algoritmy\/\" rel=\"noopener noreferrer nofollow\">,<\/a> \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0432\u0435\u043b\u0438\u0447\u0438\u043d\u0443 \u0441\u0434\u0432\u0438\u0433\u0430. \u0414\u043b\u044f \u043f\u043e\u0432\u044b\u0448\u0435\u043d\u0438\u044f \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u0438 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e, \u0447\u0442\u043e\u0431\u044b \u0441\u0434\u0432\u0438\u0433 \u043d\u0430 \u043a\u0430\u0436\u0434\u043e\u043c \u0448\u0430\u0433\u0435 \u0431\u044b\u043b \u0431\u044b \u043a\u0430\u043a \u043c\u043e\u0436\u043d\u043e \u0431\u043e\u043b\u044c\u0448\u0438\u043c.<\/p>\n<p>\u0415\u0441\u043b\u0438 j \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442 \u043f\u043e\u0437\u0438\u0446\u0438\u044e \u0432 \u043f\u043e\u0434\u0441\u0442\u0440\u043e\u043a\u0435 W, \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0449\u0443\u044e \u043f\u0435\u0440\u0432\u044b\u0439 \u043d\u0435\u0441\u043e\u0432\u043f\u0430\u0434\u0430\u044e\u0449\u0438\u0439 \u0441\u0438\u043c\u0432\u043e\u043b (\u043a\u0430\u043a \u0432 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0435 \u043f\u0440\u044f\u043c\u043e\u0433\u043e \u043f\u043e\u0438\u0441\u043a\u0430), \u0442\u043e \u0432\u0435\u043b\u0438\u0447\u0438\u043d\u0430 \u0441\u0434\u0432\u0438\u0433\u0430 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442\u0441\u044f \u043a\u0430\u043a j-LPS. \u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435 LPS \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442\u0441\u044f \u043a\u0430\u043a \u0440\u0430\u0437\u043c\u0435\u0440 \u0441\u0430\u043c\u043e\u0439 \u0434\u043b\u0438\u043d\u043d\u043e\u0439 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432 \u0441\u043b\u043e\u0432\u0430, \u043d\u0435\u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u043f\u0440\u0435\u0434\u0448\u0435\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0445 \u043f\u043e\u0437\u0438\u0446\u0438\u0438 j, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e \u0441\u043e\u0432\u043f\u0430\u0434\u0430\u0435\u0442 \u0441 \u043d\u0430\u0447\u0430\u043b\u043e\u043c \u043f\u043e\u0434\u0441\u0442\u0440\u043e\u043a\u0438. LPS \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0442 \u043f\u043e\u0434\u0441\u0442\u0440\u043e\u043a\u0438 \u0438 \u043d\u0435 \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u0442\u0435\u043a\u0441\u0442\u0430. \u0414\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e j \u0431\u0443\u0434\u0435\u0442 \u0441\u0432\u043e\u044f \u0432\u0435\u043b\u0438\u0447\u0438\u043d\u0430 LPS.<\/p>\n<p>\u0422\u0430\u043a \u043a\u0430\u043a \u0432\u0435\u043b\u0438\u0447\u0438\u043d\u044b LPS \u0437\u0430\u0432\u0438\u0441\u044f\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0442 \u043f\u043e\u0434\u0441\u0442\u0440\u043e\u043a\u0438 W, \u0442\u043e \u043f\u0435\u0440\u0435\u0434 \u043d\u0430\u0447\u0430\u043b\u043e\u043c \u0444\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u043f\u043e\u0438\u0441\u043a\u0430 \u043d\u0443\u0436\u043d\u043e \u0432\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 \u0438\u0437 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0445 LPS \u0432\u043e\u0442 \u0442\u0430\u043a:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/b43\/c76\/d0e\/b43c76d0ead24a897dfadaaca3c99ff1.png\" width=\"2606\" height=\"1238\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/b43\/c76\/d0e\/b43c76d0ead24a897dfadaaca3c99ff1.png\"\/><\/figure>\n<p>\u041f\u043e\u0441\u043b\u0435 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f LPS-\u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0434\u043b\u044f \u043f\u043e\u0434\u0441\u0442\u0440\u043e\u043a\u0438 W \u043c\u043e\u0436\u043d\u043e \u043f\u0435\u0440\u0435\u0439\u0442\u0438 \u043a \u043d\u0435\u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u043c\u0443 \u043f\u043e\u0438\u0441\u043a\u0443 W \u0432 \u0442\u0435\u043a\u0441\u0442\u0435:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/223\/f73\/9c5\/223f739c5153ae2927cb1fc2d84ddeb4.png\" width=\"2450\" height=\"1342\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/223\/f73\/9c5\/223f739c5153ae2927cb1fc2d84ddeb4.png\"\/><\/figure>\n<p>\u041e\u0431\u0449\u0430\u044f \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0434\u043b\u044f \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 KMP \u0441\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 O(n+m), \u0433\u0434\u0435 n &#8212; \u0434\u043b\u0438\u043d\u0430 \u0442\u0435\u043a\u0441\u0442\u0430 T, \u0430 m &#8212; \u0434\u043b\u0438\u043d\u0430 \u0441\u043b\u043e\u0432\u0430 W. \u042d\u0442\u043e \u0441\u0432\u044f\u0437\u0430\u043d\u043e \u0441 \u0442\u0435\u043c, \u0447\u0442\u043e \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043f\u043e\u0438\u0441\u043a\u0430 KMP \u043c\u044b \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0435\u043c \u043c\u0430\u0441\u0441\u0438\u0432 LPS, \u0430 \u0437\u0430\u0442\u0435\u043c \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u043c \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u043f\u043e\u0438\u0441\u043a\u0430 KMP.<\/p>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/7c7\/630\/6b8\/7c76306b851b9dea7d5bb95314254194.gif\" width=\"1\" height=\"1\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/7c7\/630\/6b8\/7c76306b851b9dea7d5bb95314254194.gif\"\/><\/figure>\n<p><strong>\u041f\u0440\u0438\u043c\u0435\u0440.<\/strong> \u041f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u043c, \u0447\u0442\u043e \u0442\u0435\u043a\u0441\u0442 T \u0440\u0430\u0432\u0435\u043d <code>aaaab<\/code>, \u0430 \u0441\u043b\u043e\u0432\u043e W \u0440\u0430\u0432\u043d\u043e <code>aab<\/code>. \u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0432\u0437\u0433\u043b\u044f\u043d\u0435\u043c \u043d\u0430 \u0442\u0430\u0431\u043b\u0438\u0446\u0443 LPS \u0434\u043b\u044f \u0441\u043b\u043e\u0432\u0430 W.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/61f\/606\/c5d\/61f606c5db08f52ea41bc392bb743cfb.png\" width=\"3462\" height=\"332\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/61f\/606\/c5d\/61f606c5db08f52ea41bc392bb743cfb.png\"\/><\/figure>\n<p>&#8230;\u0438 \u043d\u0430 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 \u043f\u043e\u0438\u0441\u043a\u0430 KMP \u043a \u0437\u0430\u0434\u0430\u043d\u043d\u043e\u043c\u0443 \u0442\u0435\u043a\u0441\u0442\u0443.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/317\/2fd\/d63\/3172fdd63efa0bc8c863c167f30a1deb.png\" width=\"1351\" height=\"372\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/317\/2fd\/d63\/3172fdd63efa0bc8c863c167f30a1deb.png\"\/><\/figure>\n<p>\u041a\u0430\u043a \u043c\u044b \u0432\u0438\u0434\u0438\u043c, \u043d\u0430 3 \u0438 4 \u044d\u0442\u0430\u043f\u0430\u0445 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b LPS \u043c\u0430\u0441\u0441\u0438\u0432 \u0434\u043b\u044f \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 j \u0432 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e\u0435 \u043f\u043e\u043b\u043e\u0436\u0435\u043d\u0438\u0435. \u0421\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e, \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0443 \u043d\u0435 \u043d\u0443\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u043d\u0430\u0447\u0438\u043d\u0430\u0442\u044c \u0441\u043e\u043f\u043e\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0441\u043b\u043e\u0432\u0430 W \u0441 \u0441\u0430\u043c\u043e\u0433\u043e \u043d\u0430\u0447\u0430\u043b\u0430, \u043a\u043e\u0433\u0434\u0430 \u0431\u044b\u043b\u043e \u043e\u0431\u043d\u0430\u0440\u0443\u0436\u0435\u043d\u043e \u043d\u0435\u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0438\u0435. \u042d\u0442\u043e \u043f\u043e\u043c\u043e\u0433\u043b\u043e \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0443 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e \u043d\u0430\u0439\u0442\u0438 \u0432\u0445\u043e\u0436\u0434\u0435\u043d\u0438\u0435 \u0441\u043b\u043e\u0432\u0430 W \u043d\u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u043c \u0448\u0430\u0433\u0435.<\/p>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/c19\/39b\/56c\/c1939b56cb30911e75fb1a1df85f60f7.gif\" width=\"1\" height=\"1\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/c19\/39b\/56c\/c1939b56cb30911e75fb1a1df85f60f7.gif\"\/><\/figure>\n<p>\u041e\u043a, \u0441 \u043f\u0440\u0438\u043d\u0446\u0438\u043f\u0430\u043c\u0438 \u0440\u0430\u0431\u043e\u0442\u044b \u041a\u041c\u041f \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u043b\u0438\u0441\u044c, \u0430 \u0432\u043e\u0442 \u0442\u0430\u043a \u043c\u043e\u0436\u0435\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u0435\u0433\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043d\u0430 Python:<\/p>\n<pre><code class=\"python\"># \u041f\u0440\u0435\u0444\u0438\u043a\u0441-\u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0434\u043b\u044f \u041a\u041c\u041f def pref_func(x):     # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043c\u0430\u0441\u0441\u0438\u0432\u0430-\u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430 \u0434\u043b\u0438\u043d\u043e\u0439 X     res = [0] * len(x)     i = 0     j = -1     res[0] = -1     # \u0412\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0435 \u043f\u0440\u0435\u0444\u0438\u043a\u0441-\u0444\u0443\u043d\u043a\u0446\u0438\u0438     while i &lt; len(x) - 1:         while j >= 0 and x[j] != x[i]:             j = res[j]         i += 1         j += 1         if x[i] == x[j]:             res[i] = res[j]         else:             res[i] = j     return res  # \u0412\u043e\u0437\u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u043f\u0440\u0435\u0444\u0438\u043a\u0441-\u0444\u0443\u043d\u043a\u0446\u0438\u0438  # \u0424\u0443\u043d\u043a\u0446\u0438\u044f \u043f\u043e\u0438\u0441\u043a\u0430 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u043e\u043c \u041a\u041c\u041f def kmp(x, s):     nom = \"\"  # \u041e\u0431\u044a\u044f\u0432\u043b\u0435\u043d\u0438\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \u0441 \u043d\u043e\u043c\u0435\u0440\u0430\u043c\u0438 \u043f\u043e\u0437\u0438\u0446\u0438\u0439     if len(x) > len(s):         return nom  # \u0412\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 0 \u043f\u043e\u0438\u0441\u043a \u0435\u0441\u043b\u0438 \u043e\u0431\u0440\u0430\u0437\u0435\u0446 \u0431\u043e\u043b\u044c\u0448\u0435 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0438     # \u0412\u044b\u0437\u043e\u0432 \u043f\u0440\u0435\u0444\u0438\u043a\u0441-\u0444\u0443\u043d\u043a\u0446\u0438\u0438     d = pref_func(x)     i = 0     while i &lt; len(s):         j = 0         while i &lt; len(s) and j &lt; len(x):             if x[j] != s[i]:                 j = d[j]             i += 1             j += 1         if j == len(x):             nom += str(i - j) + \", \"     if nom != \"\":         nom = nom[:-2]  # \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0439 \u0437\u0430\u043f\u044f\u0442\u043e\u0439 \u0438 \u043f\u0440\u043e\u0431\u0435\u043b\u0430     return nom  # \u0412\u043e\u0437\u0432\u0440\u0430\u0449\u0435\u043d\u0438\u0435 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430 \u043f\u043e\u0438\u0441\u043a\u0430 <\/code><\/pre>\n<p>\u0412 \u0442\u0435\u043c\u0443, \u0432\u043e\u0442 <a href=\"https:\/\/users.math-cs.spbu.ru\/~okhotin\/teaching\/algorithms1_2022\/okhotin_algorithms1_2022_l6.pdf\" rel=\"noopener noreferrer nofollow\">\u043f\u043e\u0434\u0440\u043e\u0431\u043d\u0430\u044f \u043b\u0435\u043a\u0446\u0438\u044f \u043f\u043e \u041a\u041c\u041f<\/a> \u043e\u0442 \u0410\u043b\u0435\u043a\u0441\u0430\u043d\u0434\u0440\u0430 \u041e\u0445\u043e\u0442\u0438\u043d\u0430. \u0418 \u0432\u043e\u0442, <a href=\"https:\/\/www.youtube.com\/watch?v=Cbt67mfrF6o\" rel=\"noopener noreferrer nofollow\">\u0433\u043e\u0434\u043d\u043e\u0435 \u0432\u0438\u0434\u0435\u043e<\/a>, \u043e\u0447\u0435\u043d\u044c \u043f\u043e\u043d\u044f\u0442\u043d\u043e \u0432\u0441\u0451 \u043e\u0431\u044a\u044f\u0441\u043d\u044f\u0435\u0442\u0441\u044f.<\/p>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/4fb\/a1b\/a8b\/4fba1ba8bb82b2c4a781316e8a959150.gif\" width=\"1\" height=\"1\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/4fb\/a1b\/a8b\/4fba1ba8bb82b2c4a781316e8a959150.gif\"\/><\/figure>\n<h3>\u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0410\u0445\u043e-\u041a\u043e\u0440\u0430\u0441\u0438\u043a<\/h3>\n<p><strong>\u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0410\u0445\u043e-\u041a\u043e\u0440\u0430\u0441\u0438\u043a<\/strong> \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442 \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a \u0432\u0441\u0435\u0445 \u0432\u0445\u043e\u0436\u0434\u0435\u043d\u0438\u0439 \u0432\u0441\u0435\u0445 \u0441\u0442\u0440\u043e\u043a-\u043e\u0431\u0440\u0430\u0437\u0446\u043e\u0432 \u0432 \u0437\u0430\u0434\u0430\u043d\u043d\u0443\u044e \u0441\u0442\u0440\u043e\u043a\u0443. \u042d\u0442\u043e\u0442 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0432 \u0440\u0430\u0437\u043d\u044b\u0445 \u0443\u0442\u0438\u043b\u0438\u0442\u0430\u0445 \u0438 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f\u0445, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0432 <code>grep<\/code>.<\/p>\n<p>\u0410\u0445\u043e-\u041a\u043e\u0440\u0430\u0441\u0438\u043a \u0441\u0442\u0440\u043e\u0438\u0442 \u043a\u043e\u043d\u0435\u0447\u043d\u044b\u0439 \u0430\u0432\u0442\u043e\u043c\u0430\u0442, \u043a\u043e\u0442\u043e\u0440\u043e\u043c\u0443 \u0437\u0430\u0442\u0435\u043c \u043f\u0435\u0440\u0435\u0434\u0430\u0451\u0442 \u0441\u0442\u0440\u043e\u043a\u0443 \u043f\u043e\u0438\u0441\u043a\u0430. \u0410\u0432\u0442\u043e\u043c\u0430\u0442 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 \u043f\u043e \u043e\u0447\u0435\u0440\u0435\u0434\u0438 \u0432\u0441\u0435 \u0441\u0438\u043c\u0432\u043e\u043b\u044b \u0441\u0442\u0440\u043e\u043a\u0438 \u0438 \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u0442 \u043f\u043e \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u043c \u0440\u0451\u0431\u0440\u0430\u043c. \u0415\u0441\u043b\u0438 \u0430\u0432\u0442\u043e\u043c\u0430\u0442 \u043f\u0440\u0438\u0448\u0451\u043b \u0432 \u043a\u043e\u043d\u0435\u0447\u043d\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435, \u0442\u043e \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430 \u0441\u043b\u043e\u0432\u0430\u0440\u044f \u043f\u0440\u0438\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \u0432 \u0441\u0442\u0440\u043e\u043a\u0435 \u043f\u043e\u0438\u0441\u043a\u0430.<\/p>\n<p>\u0422\u0443\u0442 \u043d\u0438\u0436\u0435 \u043d\u0435\u0434\u0435\u0442\u0435\u0440\u043c\u0438\u043d\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0430\u0432\u0442\u043e\u043c\u0430\u0442 \u0434\u043b\u044f \u0441\u043b\u043e\u0432\u0430\u0440\u044f <code>{a, ab, bc, bca, c, caa}<\/code>. \u0421\u0435\u0440\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u043f\u0440\u043e\u043c\u0435\u0436\u0443\u0442\u043e\u0447\u043d\u044b\u0435, \u0431\u0435\u043b\u044b\u0435 \u043a\u043e\u043d\u0435\u0447\u043d\u044b\u0435. \u0421\u0438\u043d\u0438\u0435 \u0441\u0442\u0440\u0435\u043b\u043a\u0438 \u2014 \u0441\u0443\u0444\u0444\u0438\u043a\u0441\u043d\u044b\u0435 \u0441\u0441\u044b\u043b\u043a\u0438, \u0437\u0435\u043b\u0451\u043d\u044b\u0435 \u2014 \u043a\u043e\u043d\u0435\u0447\u043d\u044b\u0435.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/7bc\/c03\/988\/7bcc03988fc25faa00f8ee9b1ac2271b.png\" width=\"5024\" height=\"3112\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/7bc\/c03\/988\/7bcc03988fc25faa00f8ee9b1ac2271b.png\"\/><\/figure>\n<p>\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0410\u0445\u043e-\u041a\u043e\u0440\u0430\u0441\u0438\u043a \u043d\u0430 Python \u043c\u043e\u0436\u0435\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u0442\u0430\u043a, \u0432 \u043f\u0440\u0438\u043d\u0446\u0438\u043f\u0435, \u0442\u0443\u0442 \u0438\u0437 \u043a\u043e\u0434\u0430 \u043a\u0440\u0438\u0441\u0442\u0430\u043b\u044c\u043d\u043e \u044f\u0441\u043d\u043e, \u043a\u0430\u043a \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c:<\/p>\n<pre><code class=\"python\">class AhoNode:     ''' \u0412\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0441 \u0434\u043b\u044f \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u044f \u0434\u0435\u0440\u0435\u0432\u0430'''     def __init__(self):         self.goto = {}         self.out = []         self.fail = None  def aho_create_forest(patterns):     '''\u0421\u043e\u0437\u0434\u0430\u0442\u044c \u0431\u043e\u0440 - \u0434\u0435\u0440\u0435\u0432\u043e \u043f\u0430\u0442\u0442\u0435\u0440\u043d\u043e\u0432'''     root = AhoNode()      for path in patterns:         node = root         for symbol in path:             node = node.goto.setdefault(symbol, AhoNode())         node.out.append(path)     return root  def aho_create_statemachine(patterns):     '''\u0421\u043e\u0437\u0434\u0430\u0442\u044c \u0430\u0432\u0442\u043e\u043c\u0430\u0442 \u0410\u0445\u043e-\u041a\u043e\u0440\u0430\u0441\u0438\u043a\u0430.     \u0424\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0441\u043e\u0437\u0434\u0430\u0435\u0442 \u0431\u043e\u0440 \u0438 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0435\u0442 fail-\u0444\u0443\u043d\u043a\u0446\u0438\u0438     \u0432\u0441\u0435\u0445 \u0443\u0437\u043b\u043e\u0432, \u043e\u0431\u0445\u043e\u0434\u044f \u0434\u0435\u0440\u0435\u0432\u043e \u0432 \u0448\u0438\u0440\u0438\u043d\u0443.'''     # \u0421\u043e\u0437\u0434\u0430\u0435\u043c \u0431\u043e\u0440, \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0435\u043c \u043d\u0435\u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0445 \u043f\u043e\u0442\u043e\u043c\u043a\u043e\u0432 \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0433\u043e \u0443\u0437\u043b\u0430     root = aho_create_forest(patterns)     queue = []     for node in root.goto.itervalues():         queue.append(node)         node.fail = root      # \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0435\u043c \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u0443\u0437\u043b\u044b:     # 1. \u0411\u0435\u0440\u0435\u043c \u043e\u0447\u0435\u0440\u0435\u0434\u043d\u043e\u0439 \u0443\u0437\u0435\u043b (\u0432\u0430\u0436\u043d\u043e, \u0447\u0442\u043e \u043f\u0440\u043e\u0445\u043e\u0434 \u0432 \u0448\u0438\u0440\u0438\u043d\u0443)     # 2. \u041d\u0430\u0445\u043e\u0434\u0438\u043c \u0441\u0430\u043c\u0443\u044e \u0434\u043b\u0438\u043d\u043d\u0443\u044e \u0441\u0443\u0444\u0444\u0438\u043a\u0441\u043d\u0443\u044e \u0441\u0441\u044b\u043b\u043a\u0443 \u0434\u043b\u044f \u044d\u0442\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b - \u044d\u0442\u043e \u0438 \u0431\u0443\u0434\u0435\u0442 fail-\u0444\u0443\u043d\u043a\u0446\u0438\u044f     # 3. \u0415\u0441\u043b\u0438 \u0442\u0430\u043a\u043e\u0432\u043e\u0439 \u043d\u0435 \u043d\u0430\u0448\u043b\u043e\u0441\u044c - \u0443\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c fail-\u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0432 \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0439 \u0443\u0437\u0435\u043b     while len(queue) > 0:         rnode = queue.pop(0)          for key, unode in rnode.goto.iteritems():             queue.append(unode)             fnode = rnode.fail             while fnode is not None and key not in fnode.goto:                 fnode = fnode.fail             unode.fail = fnode.goto[key] if fnode else root             unode.out += unode.fail.out      return root   def aho_find_all(s, root, callback):     '''\u041d\u0430\u0445\u043e\u0434\u0438\u0442 \u0432\u0441\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u0435 \u043f\u043e\u0434\u0441\u0442\u0440\u043e\u043a\u0438 \u0438\u0437 \u043d\u0430\u0431\u043e\u0440\u0430 \u043f\u0430\u0442\u0442\u0435\u0440\u043d\u043e\u0432 \u0432 \u0441\u0442\u0440\u043e\u043a\u0435.'''     node = root      for i in xrange(len(s)):         while node is not None and s[i] not in node.goto:             node = node.fail         if node is None:             node = root             continue         node = node.goto[s[i]]         for pattern in node.out:             callback(i - len(pattern) + 1, pattern)   # \u0418 \u0432\u043e\u0442 \u0442\u0430\u043a \u044d\u0442\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442  def on_occurence(pos, patterns):     print \"At pos %s found pattern: %s\" % (pos, patterns)  patterns = ['a', 'ab', 'abc', 'bc', 'c', 'cba'] s = \"abcba\" root = aho_create_statemachine(patterns) aho_find_all(s, root, on_occurence)  # At pos 0 found pattern: a # At pos 0 found pattern: ab # At pos 0 found pattern: abc # At pos 1 found pattern: bc # At pos 2 found pattern: c # At pos 2 found pattern: cba # At pos 4 found pattern: a <\/code><\/pre>\n<p>By the way, <a href=\"https:\/\/cdkrot.me\/teaching\/2021f-algo-masters\/notes\/2021-11-29_Aho-Corasick_slides.pdf\" rel=\"noopener noreferrer nofollow\">\u0432\u043e\u0442 \u043d\u0435\u043f\u043b\u043e\u0445\u0430\u044f \u043b\u0435\u043a\u0446\u0438\u044f \u043e\u0442 \u0412\u0428\u042d<\/a> \u043f\u043e \u044d\u0442\u043e\u043c\u0443 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0443.<\/p>\n<p>\u0410 <a href=\"https:\/\/compiler.club\/aho-corasick\/\" rel=\"noopener noreferrer nofollow\">\u0432\u043e\u0442 \u0437\u0434\u0435\u0441\u044c<\/a> \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u043a\u043e\u043d\u0435\u0447\u043d\u044b\u0439 \u0430\u0432\u0442\u043e\u043c\u0430\u0442 \u0410\u0445\u043e-\u041a\u043e\u0440\u0430\u0441\u0438\u043a \u0434\u043b\u044f \u043a\u0430\u043a\u0438\u0445-\u0442\u043e \u043f\u043e\u0434\u0441\u0442\u0440\u043e\u043a, \u0437\u0430\u0446\u0435\u043d\u0438\u0442\u0435:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/14e\/fd2\/8c5\/14efd28c5b6079b08c75377a62b1b536.png\" width=\"1169\" height=\"676\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/14e\/fd2\/8c5\/14efd28c5b6079b08c75377a62b1b536.png\"\/><\/figure>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/9b9\/8a8\/c4d\/9b98a8c4dad986d61f906932fee015a6.gif\" width=\"1\" height=\"1\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/9b9\/8a8\/c4d\/9b98a8c4dad986d61f906932fee015a6.gif\"\/><\/figure>\n<h3>\u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0425\u0430\u0444\u0444\u043c\u0430\u043d\u0430<\/h3>\n<p>\u041a\u0441\u0442\u0430\u0442\u0438, \u043f\u0430\u0440\u0443 \u043c\u0435\u0441\u044f\u0446\u0435\u0432 \u043d\u0430\u0437\u0430\u0434 \u0432\u044b\u0448\u043b\u0430 \u0445\u0430\u0431\u0440\u043e\u0441\u0442\u0430\u0442\u044c\u044f \u00ab<a href=\"https:\/\/habr.com\/ru\/companies\/samsung\/articles\/771572\/\" rel=\"noopener noreferrer nofollow\">\u0415\u0449\u0451 \u0440\u0430\u0437 \u043f\u0440\u043e \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0441\u0436\u0430\u0442\u0438\u044f \u0425\u0430\u0444\u0444\u043c\u0430\u043d\u0430<\/a>\u00bb \u2014 \u0442\u0430\u043c \u0434\u0435\u0442\u0430\u043b\u044c\u043d\u043e \u043f\u043e\u044f\u0441\u043d\u044f\u0435\u0442\u0441\u044f, \u0447\u0442\u043e \u0435\u0441\u0442\u044c \u0447\u0442\u043e.<\/p>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/b18\/1c4\/cd8\/b181c4cd8e8e447a250aa558b5c685e3.gif\" width=\"300\" height=\"329\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/b18\/1c4\/cd8\/b181c4cd8e8e447a250aa558b5c685e3.gif\"\/><\/figure>\n<p><strong>\u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0425\u0430\u0444\u0444\u043c\u0430\u043d\u0430<\/strong> \u2014 \u0436\u0430\u0434\u043d\u044b\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u043f\u0440\u0435\u0444\u0438\u043a\u0441\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0430\u043b\u0444\u0430\u0432\u0438\u0442\u0430 \u0441 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0439 \u0438\u0437\u0431\u044b\u0442\u043e\u0447\u043d\u043e\u0441\u0442\u044c\u044e.<\/p>\n<p>\u0418\u0434\u0435\u044f, \u043f\u043e\u043b\u043e\u0436\u0435\u043d\u043d\u0430\u044f \u0432 \u043e\u0441\u043d\u043e\u0432\u0443 \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438 \u0425\u0430\u0444\u0444\u043c\u0430\u043d\u0430, \u043e\u0441\u043d\u043e\u0432\u0430\u043d\u0430 \u043d\u0430 \u0447\u0430\u0441\u0442\u043e\u0442\u0435 \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u044f \u0441\u0438\u043c\u0432\u043e\u043b\u0430 \u0432 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438. \u0421\u0438\u043c\u0432\u043e\u043b, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u0441\u0442\u0440\u0435\u0447\u0430\u0435\u0442\u0441\u044f \u0432 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0447\u0430\u0449\u0435 \u0432\u0441\u0435\u0433\u043e, \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 \u043d\u043e\u0432\u044b\u0439 \u043e\u0447\u0435\u043d\u044c \u043c\u0430\u043b\u0435\u043d\u044c\u043a\u0438\u0439 \u043a\u043e\u0434, \u0430 \u0441\u0438\u043c\u0432\u043e\u043b, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u0441\u0442\u0440\u0435\u0447\u0430\u0435\u0442\u0441\u044f \u0440\u0435\u0436\u0435 \u0432\u0441\u0435\u0433\u043e, \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442, \u043d\u0430\u043e\u0431\u043e\u0440\u043e\u0442, \u043e\u0447\u0435\u043d\u044c \u0434\u043b\u0438\u043d\u043d\u044b\u0439 \u043a\u043e\u0434.<\/p>\n<p>\u041a\u043e\u0434\u044b \u0425\u0430\u0444\u0444\u043c\u0430\u043d\u0430 \u043e\u0431\u043b\u0430\u0434\u0430\u044e\u0442 \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u043e\u043c \u043f\u0440\u0435\u0444\u0438\u043a\u0441\u043d\u043e\u0441\u0442\u0438 (\u0442. \u0435. \u043d\u0438 \u043e\u0434\u043d\u043e \u043a\u043e\u0434\u043e\u0432\u043e\u0435 \u0441\u043b\u043e\u0432\u043e \u043d\u0435 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043f\u0440\u0435\u0444\u0438\u043a\u0441\u043e\u043c \u0434\u0440\u0443\u0433\u043e\u0433\u043e), \u0447\u0442\u043e \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043e\u0434\u043d\u043e\u0437\u043d\u0430\u0447\u043d\u043e \u0438\u0445 \u0434\u0435\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u0442\u044c.<br \/> \u041a\u043b\u0430\u0441\u0441\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0425\u0430\u0444\u0444\u043c\u0430\u043d\u0430 \u043d\u0430 \u0432\u0445\u043e\u0434\u0435 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 \u0442\u0430\u0431\u043b\u0438\u0446\u0443 \u0447\u0430\u0441\u0442\u043e\u0442 \u0432\u0441\u0442\u0440\u0435\u0447\u0430\u0435\u043c\u043e\u0441\u0442\u0438 \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432 \u0432 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0438. \u0414\u0430\u043b\u0435\u0435 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0430\u043d\u0438\u0438 \u044d\u0442\u043e\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0441\u0442\u0440\u043e\u0438\u0442\u0441\u044f \u0434\u0435\u0440\u0435\u0432\u043e \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0425\u0430\u0444\u0444\u043c\u0430\u043d\u0430 (\u041d-\u0434\u0435\u0440\u0435\u0432\u043e).<\/p>\n<ol>\n<li>\n<p>\u0421\u0438\u043c\u0432\u043e\u043b\u044b \u0432\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u0430\u043b\u0444\u0430\u0432\u0438\u0442\u0430 \u043e\u0431\u0440\u0430\u0437\u0443\u044e\u0442 \u0441\u043f\u0438\u0441\u043e\u043a \u0441\u0432\u043e\u0431\u043e\u0434\u043d\u044b\u0445 \u0443\u0437\u043b\u043e\u0432. \u041a\u0430\u0436\u0434\u044b\u0439 \u043b\u0438\u0441\u0442 \u0438\u043c\u0435\u0435\u0442 \u0432\u0435\u0441, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0440\u0430\u0432\u0435\u043d \u043b\u0438\u0431\u043e \u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e\u0441\u0442\u0438, \u043b\u0438\u0431\u043e \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0443 \u0432\u0445\u043e\u0436\u0434\u0435\u043d\u0438\u0439 \u0441\u0438\u043c\u0432\u043e\u043b\u0430 \u0432 \u0441\u0436\u0438\u043c\u0430\u0435\u043c\u043e\u0435 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435.<\/p>\n<\/li>\n<li>\n<p>\u0412\u044b\u0431\u0438\u0440\u0430\u044e\u0442\u0441\u044f \u0434\u0432\u0430 \u0441\u0432\u043e\u0431\u043e\u0434\u043d\u044b\u0445 \u0443\u0437\u043b\u0430 \u0434\u0435\u0440\u0435\u0432\u0430 \u0441 \u043d\u0430\u0438\u043c\u0435\u043d\u044c\u0448\u0438\u043c\u0438 \u0432\u0435\u0441\u0430\u043c\u0438.<\/p>\n<\/li>\n<li>\n<p>\u0421\u043e\u0437\u0434\u0430\u0435\u0442\u0441\u044f \u0438\u0445 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044c \u0441 \u0432\u0435\u0441\u043e\u043c, \u0440\u0430\u0432\u043d\u044b\u043c \u0438\u0445 \u0441\u0443\u043c\u043c\u0430\u0440\u043d\u043e\u043c\u0443 \u0432\u0435\u0441\u0443.<\/p>\n<\/li>\n<li>\n<p>\u0420\u043e\u0434\u0438\u0442\u0435\u043b\u044c \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0432 \u0441\u043f\u0438\u0441\u043e\u043a \u0441\u0432\u043e\u0431\u043e\u0434\u043d\u044b\u0445 \u0443\u0437\u043b\u043e\u0432, \u0430 \u0434\u0432\u0430 \u0435\u0433\u043e \u043f\u043e\u0442\u043e\u043c\u043a\u0430 \u0443\u0434\u0430\u043b\u044f\u044e\u0442\u0441\u044f \u0438\u0437 \u044d\u0442\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430.<\/p>\n<\/li>\n<li>\n<p>\u041e\u0434\u043d\u043e\u0439 \u0434\u0443\u0433\u0435, \u0432\u044b\u0445\u043e\u0434\u044f\u0449\u0435\u0439 \u0438\u0437 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044f, \u0441\u0442\u0430\u0432\u0438\u0442\u0441\u044f \u0432 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0438\u0435 \u0431\u0438\u0442 1, \u0434\u0440\u0443\u0433\u043e\u0439 \u2014 \u0431\u0438\u0442 0.<\/p>\n<\/li>\n<li>\n<p>\u0428\u0430\u0433\u0438, \u043d\u0430\u0447\u0438\u043d\u0430\u044f \u0441\u043e \u0432\u0442\u043e\u0440\u043e\u0433\u043e, \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044e\u0442\u0441\u044f \u0434\u043e \u0442\u0435\u0445 \u043f\u043e\u0440, \u043f\u043e\u043a\u0430 \u0432 \u0441\u043f\u0438\u0441\u043a\u0435 \u0441\u0432\u043e\u0431\u043e\u0434\u043d\u044b\u0445 \u0443\u0437\u043b\u043e\u0432 \u043d\u0435 \u043e\u0441\u0442\u0430\u043d\u0435\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d \u0441\u0432\u043e\u0431\u043e\u0434\u043d\u044b\u0439 \u0443\u0437\u0435\u043b. \u041e\u043d \u0438 \u0431\u0443\u0434\u0435\u0442 \u0441\u0447\u0438\u0442\u0430\u0442\u044c\u0441\u044f \u043a\u043e\u0440\u043d\u0435\u043c \u0434\u0435\u0440\u0435\u0432\u0430.<\/p>\n<\/li>\n<\/ol>\n<p>\u042d\u0442\u043e\u0442 \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u043a\u0430\u043a \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435 \u0434\u0435\u0440\u0435\u0432\u0430, \u043a\u043e\u0440\u0435\u043d\u044c \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u2014 \u0441\u0438\u043c\u0432\u043e\u043b \u0441 \u0441\u0443\u043c\u043c\u043e\u0439 \u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e\u0441\u0442\u0435\u0439 \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u0435\u043d\u043d\u044b\u0445 \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432, \u043f\u043e\u043b\u0443\u0447\u0438\u0432\u0448\u0438\u0439\u0441\u044f \u043f\u0440\u0438 \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0438 \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432 \u0438\u0437 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0433\u043e \u0448\u0430\u0433\u0430, \u0435\u0433\u043e n0 \u043f\u043e\u0442\u043e\u043c\u043a\u043e\u0432 \u2014 \u0441\u0438\u043c\u0432\u043e\u043b\u044b \u0438\u0437 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0433\u043e \u0448\u0430\u0433\u0430 \u0438 \u0442. \u0434.<\/p>\n<p>\u0427\u0442\u043e\u0431\u044b \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u043a\u043e\u0434 \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0438\u0437 \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432, \u0432\u0445\u043e\u0434\u044f\u0449\u0438\u0445 \u0432 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435, \u043c\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u043f\u0440\u043e\u0439\u0442\u0438 \u043f\u0443\u0442\u044c \u043e\u0442 \u043a\u043e\u0440\u043d\u044f \u0434\u043e \u043b\u0438\u0441\u0442\u0430 \u0434\u0435\u0440\u0435\u0432\u0430, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u0433\u043e \u0442\u0435\u043a\u0443\u0449\u0435\u043c\u0443 \u0441\u0438\u043c\u0432\u043e\u043b\u0443, \u043d\u0430\u043a\u0430\u043f\u043b\u0438\u0432\u0430\u044f \u0431\u0438\u0442\u044b \u043f\u0440\u0438 \u043f\u0435\u0440\u0435\u043c\u0435\u0449\u0435\u043d\u0438\u0438 \u043f\u043e \u0432\u0435\u0442\u0432\u044f\u043c \u0434\u0435\u0440\u0435\u0432\u0430 (\u043f\u0435\u0440\u0432\u0430\u044f \u0432\u0435\u0442\u0432\u044c \u0432 \u043f\u0443\u0442\u0438 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \u043c\u043b\u0430\u0434\u0448\u0435\u043c\u0443 \u0431\u0438\u0442\u0443). \u041f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u0430\u044f \u0442\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0431\u0438\u0442\u043e\u0432 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043a\u043e\u0434\u043e\u043c \u0434\u0430\u043d\u043d\u043e\u0433\u043e \u0441\u0438\u043c\u0432\u043e\u043b\u0430, \u0437\u0430\u043f\u0438\u0441\u0430\u043d\u043d\u044b\u043c \u0432 \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435.<\/p>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/cde\/81f\/c74\/cde81fc74237b8e74089ffdee2037fca.gif\" width=\"1\" height=\"1\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/cde\/81f\/c74\/cde81fc74237b8e74089ffdee2037fca.gif\"\/><\/figure>\n<p>\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 \u0425\u0430\u0444\u0444\u043c\u0430\u043d\u0430 \u043d\u0430 Python \u043c\u043e\u0436\u0435\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u0442\u0430\u043a:<\/p>\n<pre><code class=\"python\">import heapq                        # \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u043c\u0438\u043d. \u043a\u0443\u0447\u0435\u0439 \u0438\u0437 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u043e\u0439 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438  from collections import Counter     # \u0441\u043b\u043e\u0432\u0430\u0440\u044c, \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u0441\u0447\u0435\u0442\u0447\u0438\u043a from collections import namedtuple  # \u043a\u043b\u0430\u0441\u0441\u044b \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438 \u043e \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0435 \u0434\u0435\u0440\u0435\u0432\u0430  class Node(namedtuple(\"Node\", [\"left\", \"right\"])): # \u043a\u043b\u0430\u0441\u0441 \u0434\u043b\u044f \u0432\u0435\u0442\u0432\u0435\u0439 - \u0432\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0445 \u0443\u0437\u043b\u043e\u0432     def walk(self, code, acc):                                                  # \u0447\u0442\u043e\u0431\u044b \u043e\u0431\u043e\u0439\u0442\u0438 \u0434\u0435\u0440\u0435\u0432\u043e \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e:         self.left.walk(code, acc + \"0\")          # \u043f\u043e\u0439\u0442\u0438 \u0432 \u043b\u0435\u0432\u043e\u0433\u043e \u043f\u043e\u0442\u043e\u043c\u043a\u0430, \u043a \u043f\u0440\u0435\u0444\u0438\u043a\u0441\u0443 +\"0\"         self.right.walk(code, acc + \"1\")         # \u043f\u043e\u0439\u0442\u0438 \u0432 \u043f\u0440\u0430\u0432\u043e\u0433\u043e \u043f., \u043a \u043f\u0440\u0435\u0444\u0438\u043a\u0441\u0443 +\"1\"  class Leaf(namedtuple(\"Leaf\", [\"char\"])):        # \u043a\u043b\u0430\u0441\u0441 \u0434\u043b\u044f \u043b\u0438\u0441\u0442\u044c\u0435\u0432 \u0434\u0435\u0440\u0435\u0432\u0430 \u0431\u0435\u0437 \u043f\u043e\u0442\u043e\u043c\u043a\u043e\u0432     def walk(self, code, acc):         code[self.char] = acc or \"0\"               def huffman_encode(s):                           # \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0432 \u043a\u043e\u0434\u044b \u0425\u0430\u0444\u0444\u043c\u0430\u043d\u0430     h = []                                       # \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0441 \u043f\u0440\u0438\u043e\u0440\u0438\u0442\u0435\u0442\u0430\u043c\u0438     for ch, freq in Counter(s).items():          # \u0441\u0447\u0435\u0442\u0447\u0438\u043a, \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u044b\u0439 \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u043b\u0438\u0441\u0442\u044c\u0435\u0432         h.append((freq, len(h), Leaf(ch)))       # \u0447\u0430\u0441\u0442\u043e\u0442\u0430 \u0441\u0438\u043c\u0432\u043e\u043b\u0430, \u0441\u0447\u0435\u0442\u0447\u0438\u043a, \u0441\u0438\u043c\u0432\u043e\u043b     heapq.heapify(h)                             # \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0441 \u043f\u0440\u0438\u043e\u0440\u0438\u0442\u0435\u0442\u0430\u043c\u0438     count = len(h)                               # \u0441\u0447\u0435\u0442\u0447\u0438\u043a \u0434\u043b\u0438\u043d\u043e\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438     while len(h) > 1:                            # \u043f\u043e\u043a\u0430 \u0435\u0441\u0442\u044c \u0445\u043e\u0442\u044f \u0431\u044b 2 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430         freq1, _count1, left = heapq.heappop(h)  # \u0441 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0439 \u0447\u0430\u0441\u0442\u043e\u0442\u043e\u0439 - \u043b\u0435\u0432\u044b\u0439 \u0443\u0437\u0435\u043b         freq2, _count2, right = heapq.heappop(h) # \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0441 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0439 \u0447. - \u043f\u0440\u0430\u0432\u044b\u0439          # \u043f\u043e\u043c\u0435\u0441\u0442\u0438\u043c \u043d\u043e\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442, \u0443 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0447\u0430\u0441\u0442\u043e\u0442\u0430 = \u0441\u0443\u043c\u043c\u0435 \u0447\u0430\u0441\u0442\u043e\u0442 \u0432\u044b\u0442\u0430\u0449\u0435\u043d\u043d\u044b\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432         heapq.heappush(h, (freq1 + freq2, count,          Node(left, right)))                      # \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u043d\u043e\u0432\u044b\u0439 \u0432\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0439 \u0443\u0437\u0435\u043b         count += 1                                    code = {}                                    # \u0441\u043b\u043e\u0432\u0430\u0440\u044c \u043a\u043e\u0434\u043e\u0432 \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432     if h:                                        # \u0435\u0441\u043b\u0438 \u0441\u0442\u0440\u043e\u043a\u0430 \u043f\u0443\u0441\u0442\u0430\u044f, \u0442\u043e \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0442\u043e\u0436\u0435         [(_freq, _count, root)] = h                       root.walk(code, \"\")                      # \u043e\u0431\u043e\u0439\u0434\u0435\u043c \u0434\u0435\u0440\u0435\u0432\u0430 \u043e\u0442 \u043a\u043e\u0440\u043d\u044f \u0438 \u0437\u0430\u043f\u043e\u043b\u043d\u0438\u043c \u0441\u043b\u043e\u0432\u0430\u0440\u044c     return code                                  # \u0441\u043b\u043e\u0432\u0430\u0440\u044c \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432 \u0438 \u043a\u043e\u0434\u043e\u0432  def main():     s = input()                              # \u0447\u0438\u0442\u0430\u0435\u043c \u0441\u0442\u0440\u043e\u043a\u0443 \u0434\u043b\u0438\u043d\u043e\u0439  \u0434\u043e 10**4     code = huffman_encode(s)                 # \u043a\u043e\u0434\u0438\u0440\u0443\u0435\u043c \u0441\u0442\u0440\u043e\u043a\u0443     encoded = \"\".join(code[ch] for ch in s)  # \u0437\u0430\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u0430\u044f \u0432\u0435\u0440\u0441\u0438\u044f                                              # \u043a\u043e\u043d\u043a\u0430\u0442\u0435\u043d\u0438\u0440\u0443\u0435\u043c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442     print(len(code), len(encoded))           # \u0447\u0438\u0441\u043b\u043e \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432 \u0438 \u0434\u043b\u0438\u043d\u0430 \u043a\u043e\u0434\u0430     for ch in sorted(code):                           print(\"{}: {}\".format(ch, code[ch])) # \u0441\u0438\u043c\u0432\u043e\u043b \u0438 \u0435\u0433\u043e \u043a\u043e\u0434     print(encoded)                            if __name__ == \"__main__\":     main() <\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043c\u043e\u0436\u0435\u043c \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0443 \u043d\u0430\u0448\u0435\u0433\u043e \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430, \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043d\u0430\u043f\u0438\u0448\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0434\u0435\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0441\u0442\u0440\u043e\u043a\u0438 \u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f. \u0412 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0431\u0443\u0434\u0435\u043c \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u0442\u0440\u043e\u043a\u0443 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u043b\u044c\u043d\u044b\u0445 \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u043b\u044c\u043d\u043e\u0439 \u0434\u043b\u0438\u043d\u044b.<\/p>\n<pre><code class=\"python\">def huffman_decode(encoded, code):          # \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0434\u0435\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043f\u043e \u043a\u043e\u0434\u0430\u043c \u0425\u0430\u0444\u0444\u043c\u0430\u043d\u0430     sx =[]                                  # \u043c\u0430\u0441\u0441\u0438\u0432 \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432 \u0440\u0430\u0441\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0438     enc_ch = \"\"                             # \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0437\u0430\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0441\u0438\u043c\u0432\u043e\u043b\u0430     for ch in encoded:                      # \u043e\u0431\u0445\u043e\u0434 \u0437\u0430\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0438 \u043f\u043e \u0441\u0438\u043c\u0432\u043e\u043b\u0430\u043c         enc_ch += ch                        # \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u0441\u0438\u043c\u0432\u043e\u043b \u043a \u0441\u0442\u0440\u043e\u043a\u0435 \u043a\u043e\u0434\u0430         for dec_ch in code:                 # \u0438\u0449\u0435\u043c \u0437\u0430\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0441\u0438\u043c\u0432\u043e\u043b \u0432 \u0441\u043b\u043e\u0432\u0430\u0440\u0435 \u043a\u043e\u0434\u043e\u0432             if code.get(dec_ch) == enc_ch:  # \u0435\u0441\u043b\u0438 \u0437\u0430\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0441\u0438\u043c\u0432\u043e\u043b \u043d\u0430\u0439\u0434\u0435\u043d,                 sx.append(dec_ch)           # \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0440\u0430\u0441\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e + \u043a \u043c\u0430\u0441\u0441\u0438\u0432\u0443 \u0441\u0442\u0440\u043e\u043a\u0438                 enc_ch = \"\"                 # \u043e\u0431\u043d\u0443\u043b\u0438\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0437\u0430\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0441\u0438\u043c\u0432\u043e\u043b\u0430                 break     return \"\".join(sx)                      # \u0432\u0435\u0440\u043d\u0435\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0440\u0430\u0441\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0438   def test(n_iter=100):   # \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u0442\u0435\u0441\u0442 \u0434\u043b\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430     import random       # \u043d\u0430\u043c \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u0441\u044f \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440 \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0445 \u0447\u0438\u0441\u0435\u043b     import string       # string - \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432 \u043f\u043e \u0438\u0445 \u043a\u043e\u0434\u0443                                                             # \u0441\u0442\u0440\u043e\u043a\u0430 \u0438\u0437 ascii-\u0441\u0438\u043c\u0432\u043e\u043b\u043e\u0432     for i in range(n_iter):                                # \u043a\u0440\u0430\u0435\u0432\u044b\u0435 \u0441\u043b\u0443\u0447\u0430\u0438          length = random.randint(0, 32)                     # \u043a\u043e\u0434 \u0441\u0438\u043c\u0432\u043e\u043b\u0430         s = \"\".join(random.choice(string.ascii_letters) \\               for _ in range(length))                        # \u0441\u0438\u043c\u0432\u043e\u043b \u043f\u043e \u043a\u043e\u0434\u0443, + \u043a \u0441\u0442\u0440\u043e\u043a\u0435         code = huffman_encode(s)                           # \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441\u0442\u0440\u043e\u043a\u0438         encoded = \"\".join(code[ch] for ch in s)            # \u0437\u0430\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430         assert huffman_decode(encoded, code) == s          # \u0440\u0430\u0441\u043a\u043e\u0434\u0438\u0440\u0443\u0435\u043c \u0441\u0442\u0440\u043e\u043a\u0443 \u0438 \u0441\u0440\u0430\u0432\u043d\u0438\u043c <\/code><\/pre>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/c37\/a6e\/043\/c37a6e043ea5c35551433781eaaef1cc.gif\" width=\"1\" height=\"1\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/c37\/a6e\/043\/c37a6e043ea5c35551433781eaaef1cc.gif\"\/><\/figure>\n<p><code>\u0445\u0430\u0431\u0440_\u0442\u043e\u0440\u0442_\u0438\u043b\u0438_\u043d\u0435_\u0442\u043e\u0440\u0442<\/code>:<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/a34\/e03\/697\/a34e03697986e4e79b47153bc8a6f5f7.gif\" width=\"1119\" height=\"734\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/a34\/e03\/697\/a34e03697986e4e79b47153bc8a6f5f7.gif\"\/><\/figure>\n<h2>The end<\/h2>\n<p>\u0427\u0442\u043e \u0436, \u044d\u0442\u043e \u0431\u044b\u043b\u0438 \u0433\u0440\u0430\u0444\u043e\u0432\u044b\u0435 \u0438 \u0441\u0442\u0440\u043e\u043a\u043e\u0432\u044b\u0435 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u044b, \u043f\u0443\u0441\u0442\u044c \u044d\u0442\u0430 \u0448\u043f\u0430\u0440\u0433\u0430\u043b\u043a\u0430 \u043f\u043e\u043c\u043e\u0436\u0435\u0442 \u0432\u0430\u043c \u0432 \u043f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u043a\u0435 \u043a \u0441\u043e\u0431\u0435\u0441\u0443.<\/p>\n<p>\u0414\u0435\u0442\u0430\u043b\u044c\u043d\u043e \u043e \u0442\u043e\u043c, \u043a\u0430\u043a \u0432\u043e\u043e\u0431\u0449\u0435 \u043f\u0440\u043e\u0445\u043e\u0434\u0438\u0442 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0441\u0435\u043a\u0446\u0438\u044f, \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0447\u0438\u0442\u0430\u0442\u044c \u0432 \u044d\u0442\u043e\u0439 \u0445\u0430\u0431\u0440\u043e\u0441\u0442\u0430\u0442\u044c\u0435 \u2014 <a href=\"https:\/\/habr.com\/ru\/articles\/713404\/\" rel=\"noopener noreferrer nofollow\">\u00ab\u041f\u043b\u0430\u043d \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u0441\u043e\u0431\u0435\u0441\u0435\u0434\u043e\u0432\u0430\u043d\u0438\u044f&#8230;\u00bb<\/a><\/p>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/69c\/cfa\/f6d\/69ccfaf6dc485b6d8bacd5a1fd1834cd.gif\" width=\"1\" height=\"1\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/69c\/cfa\/f6d\/69ccfaf6dc485b6d8bacd5a1fd1834cd.gif\"\/><\/figure>\n<p>\u0410 \u0432\u043e\u0442 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u043e\u043b\u0435\u0437\u043d\u044b\u0435 \u0440\u0435\u0441\u0443\u0440\u0441\u044b \u0434\u043b\u044f \u0438\u0437\u0443\u0447\u0435\u043d\u0438\u044f \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u043e\u0432.<\/p>\n<p>\u0412\u043e-\u043f\u0435\u0440\u0432\u044b\u0445, \u044d\u0442\u043e \u043b\u0435\u043a\u0446\u0438\u0438 \u043e\u0442 \u0410\u043b\u0435\u043a\u0441\u0430\u043d\u0434\u0440\u0430 \u041e\u0445\u043e\u0442\u0438\u043d\u0430 \u2014 \u043d\u0435\u0440\u0435\u0430\u043b\u044c\u043d\u043e \u043f\u043e\u043b\u0435\u0437\u043d\u043e (\u0445\u043e\u0442\u044f \u043c\u0435\u0441\u0442\u0430\u043c\u0438 \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u0442\u0435\u043e\u0440\u0435\u0442\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e):<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/f73\/f62\/e6c\/f73f62e6c9898533aec86f5c776ce831.png\" width=\"4308\" height=\"3676\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/f73\/f62\/e6c\/f73f62e6c9898533aec86f5c776ce831.png\"\/><\/figure>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/282\/37d\/6a2\/28237d6a261f88cb7e43ffd188ee8220.gif\" width=\"1\" height=\"1\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/282\/37d\/6a2\/28237d6a261f88cb7e43ffd188ee8220.gif\"\/><\/figure>\n<p>\u0422\u0430\u043a\u0436\u0435 \u043c\u043d\u043e\u0433\u043e \u0432\u0441\u0435\u0433\u043e \u0433\u043e\u0434\u043d\u043e\u0433\u043e \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u0439\u0442\u0438 \u0442\u0443\u0442 \u2014 <a href=\"https:\/\/ru.algorithmica.org\/cs\/string-structures\/trie\/\" rel=\"noopener noreferrer nofollow\">ru.algorithmica.org\/cs\/<\/a><\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/4a6\/330\/94c\/4a633094c5ef857116c7b964a19cd75a.png\" width=\"1282\" height=\"673\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/4a6\/330\/94c\/4a633094c5ef857116c7b964a19cd75a.png\"\/><\/figure>\n<p>&#8230;\u0438 \u0432\u043e\u0442 \u0437\u0434\u0435\u0441\u044c \u2014 <a href=\"https:\/\/eecs376.github.io\/notes\/algorithms.html\" rel=\"noopener noreferrer nofollow\">eecs376.github.io\/notes\/algorithms.html<\/a><\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/b59\/e61\/40f\/b59e6140fb107020c41974ef097b7868.png\" alt=\"https:\/\/eecs376.github.io\/notes\/algorithms.html#dynamic-programming\" width=\"1244\" height=\"674\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/b59\/e61\/40f\/b59e6140fb107020c41974ef097b7868.png\"\/><\/p>\n<div><figcaption>https:\/\/eecs376.github.io\/notes\/algorithms.html#dynamic-programming<\/figcaption><\/div>\n<\/figure>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/323\/f5b\/1c7\/323f5b1c75a92f7f1d6ff14bea2a70a9.gif\" width=\"1\" height=\"1\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/323\/f5b\/1c7\/323f5b1c75a92f7f1d6ff14bea2a70a9.gif\"\/><\/figure>\n<p>\u041e\u0442\u043b\u0438\u0447\u043d\u043e\u0433\u043e \u0432\u0441\u0435\u043c \u0434\u043d\u044f, \u043d\u043e\u0432\u044b\u0445 \u0433\u043e\u0440\u0438\u0437\u043e\u043d\u0442\u043e\u0432, \u0438 \u0434\u0430 \u043d\u0435 \u0437\u0430\u043c\u0435\u043d\u0438\u0442 \u0432\u0430\u0441 \u0418\u0418)<\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/797837\/\"> https:\/\/habr.com\/ru\/articles\/797837\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041f\u0440\u0438\u0432\u0435\u0442, \u0425\u0430\u0431\u0440!<\/p>\n<p>\u0412 \u043d\u0430\u0448\u0435 \u043d\u0435\u0441\u043f\u043e\u043a\u043e\u0439\u043d\u043e\u0435 \u0432\u0440\u0435\u043c\u044f, \u043a\u043e\u0433\u0434\u0430 \u0441\u0444\u0435\u0440\u0430 AI \u0441\u0442\u0440\u0435\u043c\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0434\u0432\u0438\u0436\u0435\u0442\u0441\u044f \u0432\u043f\u0435\u0440\u0451\u0434, \u0445\u043e\u0447\u0435\u0442\u0441\u044f \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0441\u0442\u0430\u0431\u0438\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0438 \u0443\u0432\u0435\u0440\u0435\u043d\u043d\u043e\u0441\u0442\u0438 \u0432 \u0437\u0430\u0432\u0442\u0440\u0430\u0448\u043d\u0435\u043c \u0434\u043d\u0435. \u041a\u0430\u043a \u044d\u0442\u043e \u0441\u0432\u044f\u0437\u0430\u043d\u043e \u0441 \u0442\u0435\u043c\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0438?<\/p>\n<p>\u0421\u0430\u043c\u044b\u043c \u043f\u0440\u044f\u043c\u044b\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u2014 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u044b \u043f\u0440\u043e\u043a\u0430\u0447\u0438\u0432\u0430\u044e\u0442 \u0443\u043c\u0435\u043d\u0438\u0435 \u0441\u0438\u0441\u0442\u0435\u043c\u043d\u043e \u043c\u044b\u0441\u043b\u0438\u0442\u044c, \u0438\u0441\u043a\u0430\u0442\u044c \u043d\u0435\u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0435 \u043f\u0443\u0442\u0438 \u0440\u0435\u0448\u0435\u043d\u0438\u044f; \u0447\u0435\u043b\u043e\u0432\u0435\u043a\u0430, \u0443 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u044d\u0442\u0438 \u0441\u043a\u0438\u043b\u043b\u044b \u043d\u0430 \u0432\u044b\u0441\u043e\u0442\u0435, \u0432\u0440\u044f\u0434 \u043b\u0438 \u0437\u0430\u043c\u0435\u043d\u0438\u0442 \u0418\u0418. \u0422\u0430\u043a \u0447\u0442\u043e, \u0442\u0440\u0430\u0442\u044f \u0431\u0435\u0441\u0446\u0435\u043d\u043d\u043e\u0435 \u0432\u0440\u0435\u043c\u044f \u043d\u0430 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u044b, \u0432\u044b \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442\u0435\u0441\u044c \u043e\u0447\u0435\u043d\u044c \u043f\u043e\u043b\u0435\u0437\u043d\u044b\u043c \u0434\u0435\u043b\u043e\u043c, \u0440\u0430\u0441\u0441\u043b\u0430\u0431\u044c\u0442\u0435\u0441\u044c \u0438 \u043f\u043e\u043b\u0443\u0447\u0430\u0439\u0442\u0435 \u0443\u0434\u043e\u0432\u043e\u043b\u044c\u0441\u0442\u0432\u0438\u0435) \u0412 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0431\u043e\u043d\u0443\u0441\u0430 \u043a \u043f\u0440\u043e\u043a\u0430\u0447\u0438\u0432\u0430\u043d\u0438\u044e \u0441\u0435\u0440\u043e\u0433\u043e \u0432\u0435\u0449\u0435\u0441\u0442\u0432\u0430 \u0432\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u0435 \u0441\u0432\u0435\u0440\u0445\u0441\u043f\u043e\u0441\u043e\u0431\u043d\u043e\u0441\u0442\u044c \u043f\u0440\u043e\u0439\u0442\u0438 \u043b\u044e\u0431\u043e\u0439 \u0430\u043b\u0433\u043e\u0441\u043e\u0431\u0435\u0441 \u0432 FAANG \u0438 \u0443\u0434\u043e\u0432\u043b\u0435\u0442\u0432\u043e\u0440\u0438\u0442\u044c \u043b\u044e\u0431\u044b\u0435 \u043f\u043e\u0442\u0440\u0435\u0431\u043d\u043e\u0441\u0442\u0438 \u043f\u0438\u0440\u0430\u043c\u0438\u0434\u044b \u041c\u0430\u0441\u043b\u043e\u0443 \u2014 \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u043f\u0440\u0438\u044f\u0442\u043d\u043e)<\/p>\n<p>\u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u043c\u044b \u0440\u0430\u0437\u0431\u0435\u0440\u0451\u043c \u0433\u0440\u0430\u0444\u043e\u0432\u044b\u0435 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u044b \u0442\u0438\u043f\u043e DFS, \u0424\u043b\u043e\u0439\u0434\u0430\u2013\u0423\u043e\u0440\u0448\u0435\u043b\u043b\u0430 \u0438 \u0441\u0442\u0440\u043e\u043a\u043e\u0432\u044b\u0435 \u043d\u0430\u043f\u043e\u0434\u043e\u0431\u0438\u0435 \u0410\u0445\u043e-\u041a\u043e\u0440\u0430\u0441\u0438\u043a.<\/p>\n<figure class=\"\"><\/figure>\n<p>\u042d\u0442\u043e 2 \u0447\u0430\u0441\u0442\u044c \u0440\u0430\u0437\u0431\u043e\u0440\u0430 \u0441\u0430\u043c\u044b\u0445 \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0445 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u043e\u0432, \u0430 \u0432\u043e\u0442 1 \u0447\u0430\u0441\u0442\u044c: <a href=\"https:\/\/habr.com\/ru\/articles\/794556\/\" rel=\"noopener noreferrer nofollow\">\u00ab\u0428\u043f\u0430\u0440\u0433\u0430\u043b\u043a\u0430 \u0434\u043b\u044f \u0430\u043b\u0433\u043e\u0441\u043e\u0431\u0435\u0441\u0430 \u2014 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c, \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0434\u0430\u043d\u043d\u044b\u0445, \u043c\u0435\u0442\u043e\u0434\u044b \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 \u0438 \u0414\u0435\u0439\u043a\u0441\u0442\u0440\u0430\u00bb<\/a><\/p>\n<p>\u041f\u0440\u0438\u0432\u0435\u0434\u0443 \u0435\u0449\u0451 \u0440\u0430\u0437 \u0442\u0443 \u0441\u0445\u0435\u043c\u0443 \u0441\u043e \u0432\u0441\u0435\u043c\u0438 \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u043c\u0438 \u043f\u043e\u043d\u044f\u0442\u0438\u044f\u043c\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u043e\u0433\u0443\u0442 \u0432\u0441\u043f\u043b\u044b\u0442\u044c \u043d\u0430 \u0441\u0435\u043a\u0446\u0438\u0438 &#171;\u0410\u043b\u0433\u043e\u0440\u0442\u0438\u043c\u044b&#187;:<\/p>\n<figure class=\"full-width\"><\/figure>\n<p>\u042d\u0442\u0430 \u0441\u0445\u0435\u043c\u0430 \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u043c\u043e\u0447\u044c \u0432\u0430\u043c \u0441\u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0432 \u0442\u0435\u043c\u0435 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u043e\u0432.<\/p>\n<p><em>\u0420\u0430\u0437\u0431\u043e\u0440 \u043e\u0433\u0440\u043e\u043c\u043d\u043e\u0433\u043e \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0430 \u0432\u043e\u043f\u0440\u043e\u0441\u043e\u0432 \u0441 \u0441\u043e\u0431\u0435\u0441\u043e\u0432\u00a0\u0438 \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e <\/em><a href=\"https:\/\/t.me\/+8DmvLCXF5FZkNGQy\" rel=\"noopener noreferrer nofollow\">\u0432 \u043c\u043e\u0435\u043c \u043a\u0430\u043d\u0430\u043b\u0435<\/a>, \u0432\u043e\u043f\u0440\u043e\u0441\u044b \u043f\u043e <a href=\"https:\/\/t.me\/+5eGt0MFT8opkZjcy\" rel=\"noopener noreferrer nofollow\">\u043c\u0430\u0448\u0438\u043d\u043d\u043e\u043c\u0443 \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044e, <\/a>\u0430 \u0437\u0434\u0435\u0441\u044c \u0441\u043e\u0431\u0440\u0430\u043d\u0430<em> <\/em><a href=\"https:\/\/t.me\/addlist\/8vDUwYRGujRmZjFi\" rel=\"noopener noreferrer nofollow\"><em>\u0446\u0435\u043b\u0430\u044f \u043f\u0430\u043f\u043a\u0430\u00a0<\/em><\/a><em>\u0441 \u043a\u0430\u043d\u0430\u043b\u0430\u043c\u0438 \u0434\u043b\u044f \u043f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u043a\u0438 \u043a \u0441\u043e\u0431\u0435\u0441\u0435\u0434\u043e\u0432\u0430\u043d\u0438\u044f\u043c.<\/em><\/p>\n<p>\u041a\u0441\u0442\u0430\u0442\u0438, \u0442\u0443\u0442 \u0431\u0443\u043a\u0432\u0430\u043b\u044c\u043d\u043e \u043d\u0430 \u0434\u043d\u044f\u0445 \u0432\u044b\u0448\u0435\u043b \u0434\u0438\u0432\u043d\u044b\u0439 \u043f\u043e\u0438\u0441\u043a\u043e\u0432\u0438\u043a <a href=\"https:\/\/explorer.globe.engineer\/?q=%D0%B0%D0%BB%D0%B3%D0%BE%D1%80%D0%B8%D1%82%D0%BC%D1%8B+computer+science\" rel=\"noopener noreferrer nofollow\">explorer.globe.engineer<\/a>, \u043e\u043d \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u043d\u0430 \u043f\u043e\u0438\u0441\u043a\u0435 \u043f\u043e \u0433\u0440\u0430\u0444\u0438\u043a\u0430\u043c \u0438 \u0441\u0445\u0435\u043c\u0430\u043c, \u0430 \u0437\u0430\u043e\u0434\u043d\u043e \u043f\u044b\u0442\u0430\u0435\u0442\u0441\u044f \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432\u044b\u0434\u0430\u0447\u0443 \u2014 \u0442\u0430\u043a \u0447\u0442\u043e \u043c\u043e\u0436\u043d\u043e \u0431\u0435\u0437 \u043f\u0440\u043e\u0431\u043b\u0435\u043c \u043d\u0430\u0440\u0443\u043b\u0438\u0442\u044c \u0441\u0435\u0431\u0435 \u043d\u0435\u043f\u043b\u043e\u0445\u0438\u0435 \u0432\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043a\u0430\u043a\u043e\u0433\u043e-\u043d\u0438\u0431\u0443\u0434\u044c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430. \u0417\u0430\u0446\u0435\u043d\u0438\u0442\u0435, \u043a\u0430\u043a \u043a\u0440\u0443\u0442\u043e:<\/p>\n<figure class=\"full-width\"><\/figure>\n<p>\u041b\u0430\u0434\u043d\u043e, \u043f\u043e\u0435\u0445\u0430\u043b\u0438 \u0443\u0436\u0435 \u043a \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430\u043c!<\/p>\n<figure class=\"\"><\/figure>\n<h2>\u0413\u0440\u0430\u0444\u043e\u0432\u044b\u0435 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u044b<\/h2>\n<p>\u041d\u0438\u0436\u0435 \u044d\u0442\u043e \u0443 \u043d\u0430\u0441 \u043f\u0440\u043e\u0441\u0442\u044b\u0435 (\u0431\u0435\u0437 \u043f\u0435\u0442\u0435\u043b\u044c \u0438 \u043a\u0440\u0430\u0442\u043d\u044b\u0445 \u0440\u0451\u0431\u0435\u0440) \u043d\u0435\u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u0433\u0440\u0430\u0444\u044b:<\/p>\n<figure class=\"\"><\/figure>\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u043c\u0441\u044f \u0441 \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u043c\u0438 \u043f\u043e\u043d\u044f\u0442\u0438\u044f\u043c\u0438, \u043c\u0430\u043b\u043e \u043b\u0438, \u043c\u043e\u0436\u0435\u0442 \u043a\u0442\u043e \u043d\u0435 \u0432 \u0442\u0435\u043c\u0435:<\/p>\n<ul>\n<li>\n<p><strong>\u041f\u0443\u0442\u044c<\/strong> \u2013 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0432\u0435\u0440\u0448\u0438\u043d, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043a\u0430\u0436\u0434\u0430\u044f \u0432\u0435\u0440\u0448\u0438\u043d\u0430 \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0430 \u0441\u043e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u043e\u0439 \u0440\u0435\u0431\u0440\u043e\u043c. \u041d\u0430 \u0420\u0438\u0441. 1 \u043a\u0440\u0430\u0441\u043d\u044b\u043c\u0438 \u0441\u0442\u0440\u0435\u043b\u043a\u0430\u043c\u0438 \u043f\u043e\u043a\u0430\u0437\u0430\u043d \u043f\u0443\u0442\u044c \u043e\u0442 \u0432\u0435\u0440\u0448\u0438\u043d\u044b 1 \u043a \u0432\u0435\u0440\u0435\u0448\u0438\u043d\u0435 3 \u0447\u0435\u0440\u0435\u0437 \u0432\u0435\u0440\u0448\u0438\u043d\u0443 4.<\/p>\n<\/li>\n<li>\n<p><strong>\u041f\u043e\u0440\u044f\u0434\u043e\u043a<\/strong> \u2013 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0432\u0435\u0440\u0448\u0438\u043d \u0433\u0440\u0430\u0444\u0430.<\/p>\n<\/li>\n<li>\n<p><strong>\u0420\u0430\u0437\u043c\u0435\u0440<\/strong> \u2013 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0440\u0435\u0431\u0435\u0440 \u0433\u0440\u0430\u0444\u0430.<\/p>\n<\/li>\n<li>\n<p><strong>\u0421\u0432\u044f\u0437\u043d\u043e\u0441\u0442\u044c (\u0441\u0442\u0435\u043f\u0435\u043d\u044c \u0441\u0432\u044f\u0437\u043d\u043e\u0441\u0442\u0438) \u0432\u0435\u0440\u0448\u0438\u043d\u044b<\/strong> \u2013 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0440\u0435\u0431\u0435\u0440, \u0438\u0441\u0445\u043e\u0434\u044f\u0449\u0438\u0445 \u0438\u0437 \u044d\u0442\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b.<\/p>\n<\/li>\n<li>\n<p><strong>\u0418\u0437\u043e\u043b\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u0430\u044f \u0432\u0435\u0440\u0448\u0438\u043d\u0430<\/strong> \u2013 \u0432\u0435\u0440\u0448\u0438\u043d\u0430, \u043d\u0435 \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u0430\u044f \u0441 \u0434\u0440\u0443\u0433\u0438\u043c\u0438 \u0432\u0435\u0440\u0448\u0438\u043d\u0430\u043c\u0438 \u0433\u0440\u0430\u0444\u0430.<\/p>\n<\/li>\n<li>\n<p><strong>\u0426\u0438\u043a\u043b (\u043f\u0435\u0442\u043b\u044f)<\/strong> \u2013 \u0440\u0435\u0431\u0440\u043e \u043e\u0442 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u043a \u044d\u0442\u043e\u0439 \u0436\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u0435.<\/p>\n<\/li>\n<li>\n<p><strong>\u041e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0433\u0440\u0430\u0444<\/strong> \u2013 \u0433\u0440\u0430\u0444, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0432\u0441\u0435 \u0440\u0435\u0431\u0440\u0430 \u0438\u043c\u0435\u044e\u0442 \u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0435, \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0449\u0435\u0435, \u043a\u0430\u043a\u0430\u044f \u0438\u0437 \u0432\u0435\u0440\u0448\u0438\u043d \u0440\u0435\u0431\u0440\u0430 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0439, \u0430 \u043a\u0430\u043a\u0430\u044f \u2013 \u043a\u043e\u043d\u0435\u0447\u043d\u043e\u0439.<\/p>\n<\/li>\n<li>\n<p><strong>\u041d\u0435\u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0433\u0440\u0430\u0444<\/strong> \u2013 \u0433\u0440\u0430\u0444, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0443 \u0440\u0435\u0431\u0435\u0440 \u043d\u0435\u0442 \u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u0439.<\/p>\n<\/li>\n<li>\n<p><strong>\u0412\u0437\u0432\u0435\u0448\u0435\u043d\u043d\u044b\u0439 \u0433\u0440\u0430\u0444<\/strong> \u2013 \u0432\u0441\u0435 \u0440\u0435\u0431\u0440\u0430 \u0433\u0440\u0430\u0444\u0430 \u0438\u043c\u0435\u044e\u0442 \u0432\u0435\u0441\u0430.<\/p>\n<\/li>\n<li>\n<p><strong>\u041d\u0435\u0432\u0437\u0432\u0435\u0448\u0435\u043d\u043d\u044b\u0439 \u0433\u0440\u0430\u0444<\/strong> \u2013 \u0440\u0435\u0431\u0440\u0430 \u0433\u0440\u0430\u0444\u0430 \u043d\u0435 \u0438\u043c\u0435\u044e\u0442 \u0432\u0435\u0441\u043e\u0432.<\/p>\n<\/li>\n<\/ul>\n<figure class=\"full-width\"><\/figure>\n<p>\u0413\u0440\u0430\u0444 \u043d\u0430\u0437\u044b\u0432\u0430\u044e\u0442 <em>\u0430\u0446\u0438\u043a\u043b\u0438\u0447\u043d\u044b\u043c<\/em>, \u0435\u0441\u043b\u0438 \u0432 \u043d\u0435\u043c \u043d\u0435\u0442 \u043f\u0440\u043e\u0441\u0442\u044b\u0445 \u0446\u0438\u043a\u043b\u043e\u0432.<\/p>\n<p>\u0410\u0446\u0438\u043a\u043b\u0438\u0447\u043d\u044b\u0439 \u043d\u0435\u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0433\u0440\u0430\u0444 \u043d\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f <em>\u043b\u0435\u0441\u043e\u043c<\/em>. \u0421\u0432\u044f\u0437\u043d\u044b\u0439 \u043b\u0435\u0441 \u043d\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f <em>\u0434\u0435\u0440\u0435\u0432\u043e\u043c<\/em>. \u0423 \u0434\u0435\u0440\u0435\u0432\u044c\u0435\u0432 \u0435\u0441\u0442\u044c \u0434\u0440\u0443\u0433\u0438\u0435 \u044d\u043a\u0432\u0438\u0432\u0430\u043b\u0435\u043d\u0442\u043d\u044b\u0435 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f:<\/p>\n<ul>\n<li>\n<p>\u0413\u0440\u0430\u0444 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0434\u0435\u0440\u0435\u0432\u043e\u043c, \u0435\u0441\u043b\u0438 \u0432 \u043d\u0435\u043c <em>n<\/em> \u0432\u0435\u0440\u0448\u0438\u043d \u0438 (<em>n<\/em>\u22121) \u0440\u0435\u0431\u0435\u0440 \u0438 \u043d\u0435\u0442 \u0446\u0438\u043a\u043b\u043e\u0432.<\/p>\n<\/li>\n<li>\n<p>\u0413\u0440\u0430\u0444 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0434\u0435\u0440\u0435\u0432\u043e\u043c, \u0435\u0441\u043b\u0438 \u0438\u0437 \u043b\u044e\u0431\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u043c\u043e\u0436\u043d\u043e \u0434\u043e\u0439\u0442\u0438 \u0432 \u043b\u044e\u0431\u0443\u044e \u0434\u0440\u0443\u0433\u0443\u044e \u0435\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c.<\/p>\n<\/li>\n<\/ul>\n<p>\u0414\u0435\u0440\u0435\u0432\u043e \u043d\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043a\u043e\u0440\u043d\u0435\u0432\u044b\u043c, \u0435\u0441\u043b\u0438 \u0443 \u043d\u0435\u0433\u043e \u0435\u0441\u0442\u044c \u0440\u043e\u0432\u043d\u043e 1 \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u0430\u044f \u0432\u0435\u0440\u0448\u0438\u043d\u0430, \u043d\u0430\u0437\u044b\u0432\u0430\u0435\u043c\u0430\u044f \u043a\u043e\u0440\u043d\u0435\u043c. \u0427\u0430\u0441\u0442\u043e \u043f\u043e\u0434\u0440\u0430\u0437\u0443\u043c\u0435\u0432\u0430\u0435\u0442\u0441\u044f, \u0447\u0442\u043e \u0440\u0435\u0431\u0440\u0430 \u043a\u043e\u0440\u043d\u0435\u0432\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 \u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u044b \u0442\u0430\u043a, \u0447\u0442\u043e \u043e\u0442 \u043a\u043e\u0440\u043d\u044f \u043c\u043e\u0436\u043d\u043e \u0434\u043e\u0439\u0442\u0438 \u043e\u0442 \u0432\u0441\u0435\u0445 \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0445 \u0432\u0435\u0440\u0448\u0438\u043d.<\/p>\n<p>\u0413\u0440\u0430\u0444 \u043d\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043f\u043b\u0430\u043d\u0430\u0440\u043d\u044b\u043c, \u0435\u0441\u043b\u0438 \u0435\u0433\u043e \u043c\u043e\u0436\u043d\u043e \u0440\u0430\u0437\u043b\u043e\u0436\u0438\u0442\u044c \u043d\u0430 \u043f\u043b\u043e\u0441\u043a\u043e\u0441\u0442\u0438 \u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b \u0435\u0433\u043e \u0440\u0435\u0431\u0440\u0430 \u043d\u0435 \u043f\u0435\u0440\u0435\u0441\u0435\u043a\u0430\u043b\u0438\u0441\u044c \u0432 \u0442\u043e\u0447\u043a\u0430\u0445, \u043e\u0442\u043b\u0438\u0447\u043d\u044b\u0445 \u043e\u0442 \u0432\u0435\u0440\u0448\u0438\u043d. \u0413\u0440\u0430\u0444 \u043d\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f <em>\u0434\u0432\u0443\u0434\u043e\u043b\u044c\u043d\u044b\u043c<\/em>, \u0435\u0441\u043b\u0438 \u0435\u0433\u043e \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u043c\u043e\u0436\u043d\u043e \u0440\u0430\u0437\u0434\u0435\u043b\u0438\u0442\u044c \u043d\u0430 \u0434\u0432\u0430 \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430 \u0442\u0430\u043a\u0438\u0445, \u0447\u0442\u043e \u0440\u0435\u0431\u0440\u0430 \u0441\u043e\u0435\u0434\u0438\u043d\u044f\u044e\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0438\u0437 \u0440\u0430\u0437\u043d\u044b\u0445 \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432.<\/p>\n<p>\u042d\u0442\u0438\u0445 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0439 \u043d\u0430\u043c \u043f\u043e\u043a\u0430 \u0445\u0432\u0430\u0442\u0438\u0442. \u041f\u0435\u0440\u0435\u0439\u0434\u0451\u043c \u043a \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u043c \u0433\u0440\u0430\u0444\u043e\u0432\u044b\u043c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430\u043c.<\/p>\n<figure class=\"\"><\/figure>\n<h3>\u041f\u043e\u0438\u0441\u043a \u0432 \u0448\u0438\u0440\u0438\u043d\u0443 (Breadth First Searching)<\/h3>\n<p>\u0412\u0438\u0437\u0443\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f BFS:<\/p>\n<figure class=\"full-width\"><\/figure>\n<p><strong>\u041f\u043e\u0438\u0441\u043a \u0432 \u0448\u0438\u0440\u0438\u043d\u0443<\/strong> \u2014 \u043e\u0434\u0438\u043d \u0438\u0437 \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u0445 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u043e\u0432 \u043d\u0430 \u0433\u0440\u0430\u0444\u0430\u0445, \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0449\u0438\u0439 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u044c \u0432\u0441\u0435 \u043a\u0440\u0430\u0442\u0447\u0430\u0439\u0448\u0438\u0435 \u043f\u0443\u0442\u0438 \u043e\u0442 \u0437\u0430\u0434\u0430\u043d\u043d\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0438 \u0440\u0435\u0448\u0430\u0442\u044c \u043c\u043d\u043e\u0433\u0438\u0435 \u0434\u0440\u0443\u0433\u0438\u0435 \u0437\u0430\u0434\u0430\u0447\u0438.<\/p>\n<p>\u041f\u043e\u0438\u0441\u043a \u0432 \u0448\u0438\u0440\u0438\u043d\u0443 \u0442\u0430\u043a\u0436\u0435 \u043d\u0430\u0437\u044b\u0432\u0430\u044e\u0442 <strong>\u043e\u0431\u0445\u043e\u0434\u043e\u043c<\/strong> \u2014 \u0442\u0430\u043a \u0436\u0435, \u043a\u0430\u043a \u043f\u043e\u0438\u0441\u043a \u0432 \u0433\u043b\u0443\u0431\u0438\u043d\u0443 \u0438 \u0432\u0441\u0435 \u0434\u0440\u0443\u0433\u0438\u0435 \u043e\u0431\u0445\u043e\u0434\u044b, \u043e\u043d \u043f\u043e\u0441\u0435\u0449\u0430\u0435\u0442 \u0432\u0441\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0433\u0440\u0430\u0444\u0430 \u043f\u043e \u043e\u0434\u043d\u043e\u043c\u0443 \u0440\u0430\u0437\u0443, \u043d\u043e \u0432 \u0434\u0440\u0443\u0433\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435: \u043f\u043e \u0443\u0432\u0435\u043b\u0438\u0447\u0435\u043d\u0438\u044e \u0440\u0430\u0441\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u0434\u043e \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b.<\/p>\n<figure class=\"\"><\/figure>\n<p><strong>\u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c.<\/strong> \u041d\u0430 \u0432\u0445\u043e\u0434 \u043f\u043e\u0434\u0430\u0451\u0442\u0441\u044f <em>\u043d\u0435\u0432\u0437\u0432\u0435\u0448\u0435\u043d\u043d\u044b\u0439<\/em> \u0433\u0440\u0430\u0444 \u0438 \u043d\u043e\u043c\u0435\u0440 \u0441\u0442\u0430\u0440\u0442\u043e\u0432\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b <code>s<\/code>. \u0413\u0440\u0430\u0444 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043a\u0430\u043a \u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u043c, \u0442\u0430\u043a \u0438 \u043d\u0435\u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u043c \u2014 \u0434\u043b\u044f \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 \u044d\u0442\u043e \u043d\u0435 \u0432\u0430\u0436\u043d\u043e.<\/p>\n<p>\u041e\u0441\u043d\u043e\u0432\u043d\u0443\u044e \u0438\u0434\u0435\u044e \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043d\u0438\u043c\u0430\u0442\u044c \u043a\u0430\u043a \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u00ab\u043f\u043e\u0434\u0436\u0438\u0433\u0430\u043d\u0438\u044f\u00bb \u0433\u0440\u0430\u0444\u0430: \u043d\u0430 \u043d\u0443\u043b\u0435\u0432\u043e\u043c \u0448\u0430\u0433\u0435 \u043c\u044b \u043f\u043e\u0434\u0436\u0438\u0433\u0430\u0435\u043c \u0432\u0435\u0440\u0448\u0438\u043d\u0443 <code>s<\/code>, \u0430 \u043d\u0430 \u043a\u0430\u0436\u0434\u043e\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u043c \u0448\u0430\u0433\u0435 \u043e\u0433\u043e\u043d\u044c \u0441 \u043a\u0430\u0436\u0434\u043e\u0439 \u0443\u0436\u0435 \u0433\u043e\u0440\u044f\u0449\u0435\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u043f\u0435\u0440\u0435\u043a\u0438\u0434\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u0432\u0441\u0435\u0445 \u0435\u0451 \u0441\u043e\u0441\u0435\u0434\u0435\u0439, \u0432 \u043a\u043e\u043d\u0435\u0447\u043d\u043e\u043c \u0441\u0447\u0435\u0442\u0435 \u043f\u043e\u0434\u0436\u0438\u0433\u0430\u044f \u0432\u0435\u0441\u044c \u0433\u0440\u0430\u0444.<\/p>\n<p>\u0415\u0441\u043b\u0438 \u043c\u043e\u0434\u0435\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u044d\u0442\u043e\u0442 \u043f\u0440\u043e\u0446\u0435\u0441\u0441, \u0442\u043e \u0437\u0430 \u043a\u0430\u0436\u0434\u0443\u044e \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u044e \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442\u044c \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435 \u00ab\u043a\u043e\u043b\u044c\u0446\u0430 \u043e\u0433\u043d\u044f\u00bb \u0432 \u0448\u0438\u0440\u0438\u043d\u0443 \u043d\u0430 \u0435\u0434\u0438\u043d\u0438\u0446\u0443. \u041d\u043e\u043c\u0435\u0440 \u0448\u0430\u0433\u0430, \u043d\u0430 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0432\u0435\u0440\u0448\u0438\u043d\u0430 <code>v<\/code> \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u0442 \u0433\u043e\u0440\u0435\u0442\u044c, \u0432 \u0442\u043e\u0447\u043d\u043e\u0441\u0442\u0438 \u0440\u0430\u0432\u0435\u043d \u0434\u043b\u0438\u043d\u0435 \u0435\u0451 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u043f\u0443\u0442\u0438 \u0438\u0437 \u0432\u0435\u0440\u0448\u0438\u043d\u044b <code>s<\/code>.<\/p>\n<figure class=\"full-width\"><\/figure>\n<p>\u041c\u043e\u0434\u0435\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u044d\u0442\u043e \u043c\u043e\u0436\u043d\u043e \u0442\u0430\u043a. \u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043e\u0447\u0435\u0440\u0435\u0434\u044c, \u0432 \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u0431\u0443\u0434\u0443\u0442 \u043f\u043e\u043c\u0435\u0449\u0430\u0442\u044c\u0441\u044f \u0433\u043e\u0440\u044f\u0449\u0438\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b, \u0430 \u0442\u0430\u043a\u0436\u0435 \u0437\u0430\u0432\u0435\u0434\u0451\u043c \u0431\u0443\u043b\u0435\u0432\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0431\u0443\u0434\u0435\u043c \u043e\u0442\u043c\u0435\u0447\u0430\u0442\u044c, \u0433\u043e\u0440\u0438\u0442 \u043e\u043d\u0430 \u0438\u043b\u0438 \u043d\u0435\u0442 \u2014 \u0438\u043b\u0438 \u0438\u043d\u044b\u043c\u0438 \u0441\u043b\u043e\u0432\u0430\u043c\u0438, \u0431\u044b\u043b\u0430 \u043b\u0438 \u043e\u043d\u0430 \u0443\u0436\u0435 \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u0430. \u0418\u0437\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043f\u043e\u043c\u0435\u0449\u0430\u0435\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u0432\u0435\u0440\u0448\u0438\u043d\u0430 <code>s<\/code>, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0441\u0440\u0430\u0437\u0443 \u043f\u043e\u043c\u0435\u0447\u0430\u0435\u0442\u0441\u044f \u0433\u043e\u0440\u044f\u0449\u0435\u0439.<\/p>\n<p>\u0417\u0430\u0442\u0435\u043c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0441\u043e\u0431\u043e\u0439 \u0442\u0430\u043a\u043e\u0439 \u0446\u0438\u043a\u043b: \u043f\u043e\u043a\u0430 \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043d\u0435 \u043f\u0443\u0441\u0442\u0430, \u0434\u043e\u0441\u0442\u0430\u0442\u044c \u0438\u0437 \u0435\u0451 \u0433\u043e\u043b\u043e\u0432\u044b \u043e\u0434\u043d\u0443 \u0432\u0435\u0440\u0448\u0438\u043d\u0443 <code>v<\/code>, \u043f\u0440\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0432\u0441\u0435 \u0440\u0451\u0431\u0440\u0430, \u0438\u0441\u0445\u043e\u0434\u044f\u0449\u0438\u0435 \u0438\u0437 \u044d\u0442\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b, \u0438 \u0435\u0441\u043b\u0438 \u043a\u0430\u043a\u0438\u0435-\u0442\u043e \u0438\u0437 \u0441\u043c\u0435\u0436\u043d\u044b\u0445 \u0432\u0435\u0440\u0448\u0438\u043d <code>u<\/code> \u0435\u0449\u0451 \u043d\u0435 \u0433\u043e\u0440\u044f\u0442, \u043f\u043e\u0434\u0436\u0435\u0447\u044c \u0438\u0445 \u0438 \u043f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0432 \u043a\u043e\u043d\u0435\u0446 \u043e\u0447\u0435\u0440\u0435\u0434\u0438.<\/p>\n<p>\u0412 \u0438\u0442\u043e\u0433\u0435, \u043a\u043e\u0433\u0434\u0430 \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043e\u043f\u0443\u0441\u0442\u0435\u0435\u0442, \u043c\u044b \u043f\u043e \u043e\u0434\u043d\u043e\u043c\u0443 \u0440\u0430\u0437\u0443 \u043e\u0431\u043e\u0439\u0434\u0451\u043c \u0432\u0441\u0435 \u0434\u043e\u0441\u0442\u0438\u0436\u0438\u043c\u044b\u0435 \u0438\u0437 <code>s<\/code> \u0432\u0435\u0440\u0448\u0438\u043d\u044b, \u043f\u0440\u0438\u0447\u0451\u043c \u0434\u043e \u043a\u0430\u0436\u0434\u043e\u0439 \u0434\u043e\u0439\u0434\u0451\u043c \u043a\u0440\u0430\u0442\u0447\u0430\u0439\u0448\u0438\u043c \u043f\u0443\u0442\u0451\u043c. \u0414\u043b\u0438\u043d\u044b \u043a\u0440\u0430\u0442\u0447\u0430\u0439\u0448\u0438\u0445 \u043f\u0443\u0442\u0435\u0439 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0441\u0447\u0438\u0442\u0430\u0442\u044c, \u0435\u0441\u043b\u0438 \u0437\u0430\u0432\u0435\u0441\u0442\u0438 \u0434\u043b\u044f \u043d\u0438\u0445 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 <code>d<\/code> \u0438 \u043f\u0440\u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0438 \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043f\u0435\u0440\u0435\u0441\u0447\u0438\u0442\u044b\u0432\u0430\u0442\u044c \u043f\u043e \u043f\u0440\u0430\u0432\u0438\u043b\u0443 \u200b. \u0422\u0430\u043a\u0436\u0435 \u043c\u043e\u0436\u043d\u043e \u043a\u043e\u043c\u043f\u0430\u043a\u0442\u043d\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u0443\u044e \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u0434\u043b\u044f \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f \u0441\u0430\u043c\u0438\u0445 \u043f\u0443\u0442\u0435\u0439, \u0437\u0430\u0432\u0435\u0434\u044f \u043c\u0430\u0441\u0441\u0438\u0432 \u00ab\u043f\u0440\u0435\u0434\u043a\u043e\u0432\u00bb, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0445\u0440\u0430\u043d\u0438\u0442\u0441\u044f \u043d\u043e\u043c\u0435\u0440 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0438\u0437 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043c\u044b \u0432 \u043d\u0435\u0451 \u043f\u043e\u043f\u0430\u043b\u0438.<\/p>\n<figure class=\"\"><\/figure>\n<p>\u0420\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u043f\u043e\u0438\u0441\u043a\u0430 \u0432 \u0448\u0438\u0440\u0438\u043d\u0443 \u043d\u0430 Python \u043c\u043e\u0436\u043d\u043e \u0442\u0430\u043a:<\/p>\n<pre><code class=\"python\"># \u0421\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u044c \u0432\u0435\u0440\u0448\u0438\u043d inc = {     1: [2, 8],     2: [1, 3, 8],     3: [2, 4, 8],     4: [3, 7, 9],     5: [6, 7],     6: [5],     7: [4, 5, 8],     8: [1, 2, 3, 7],     9: [4], }  visited = set()  # \u041f\u043e\u0441\u0435\u0449\u0435\u043d\u0430 \u043b\u0438 \u0432\u0435\u0440\u0448\u0438\u043d\u0430? Q = []           # \u041e\u0447\u0435\u0440\u0435\u0434\u044c BFS = []  # \u041f\u043e\u0438\u0441\u043a \u0432 \u0448\u0438\u0440\u0438\u043d\u0443 BFS def bfs(v):     if v in visited:  # \u0415\u0441\u043b\u0438 \u0432\u0435\u0440\u0448\u0438\u043d\u0430 \u0443\u0436\u0435 \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u0430, \u0432\u044b\u0445\u043e\u0434\u0438\u043c         return     visited.add(v)    # \u041f\u043e\u0441\u0435\u0442\u0438\u043b\u0438 \u0432\u0435\u0440\u0448\u0438\u043d\u0443 v     BFS.append(v)     # \u0417\u0430\u043f\u043e\u043c\u0438\u043d\u0430\u0435\u043c \u043f\u043e\u0440\u044f\u0434\u043e\u043a \u043e\u0431\u0445\u043e\u0434\u0430      for i in inc[v]:  # \u0412\u0441\u0435 \u0441\u043c\u0435\u0436\u043d\u044b\u0435 \u0441 v \u0432\u0435\u0440\u0448\u0438\u043d\u044b         if not i in visited:             Q.append(i)     while Q:         bfs(Q.pop(0))  start = 1 bfs(start)  # start - \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u0430\u044f \u0432\u0435\u0440\u0448\u0438\u043d\u0430 \u043e\u0431\u0445\u043e\u0434\u0430 print(BFS)  # [1, 2, 8, 3, 7, 4, 5, 9, 6] <\/code><\/pre>\n<figure class=\"\"><\/figure>\n<p>\u0412\u043e\u043e\u0431\u0449\u0435, \u043e\u0434\u043d\u043e \u0438\u0437 \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u0439 BFS \u2014 \u043f\u043e\u0438\u0441\u043a \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0440\u0430\u0441\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u043c\u0435\u0436\u0434\u0443 \u0432\u0435\u0440\u0448\u0438\u043d\u0430\u043c\u0438; \u043c\u043e\u0436\u043d\u043e \u0441\u0440\u0430\u0432\u043d\u0438\u0442\u044c, \u043d\u0430\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0445\u043e\u0440\u043e\u0448\u043e \u043e\u043d \u043f\u043e\u0434\u0445\u043e\u0434\u0438\u0442 \u0434\u043b\u044f \u044d\u0442\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438 \u043f\u043e \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044e \u0441 \u0434\u0440\u0443\u0433\u0438\u043c\u0438 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430\u043c\u0438:<\/p>\n<figure class=\"full-width\"><\/figure>\n<figure class=\"\"><\/figure>\n<h3>\u041f\u043e\u0438\u0441\u043a \u0432 \u0433\u043b\u0443\u0431\u0438\u043d\u0443 (Depth First Searching)<\/h3>\n<p><strong>\u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u043f\u043e\u0438\u0441\u043a\u0430 \u0432 \u0433\u043b\u0443\u0431\u0438\u043d\u0443<\/strong> (DFS) \u043e\u0442\u043b\u0438\u0447\u0430\u0435\u0442\u0441\u044f \u043e\u0442 \u043f\u043e\u0438\u0441\u043a\u0430 \u0432 \u0448\u0438\u0440\u0438\u043d\u0443 \u0431\u043e\u043b\u0435\u0435 \u0430\u0433\u0440\u0435\u0441\u0441\u0438\u0432\u043d\u044b\u043c \u043f\u0440\u043e\u0434\u0432\u0438\u0436\u0435\u043d\u0438\u0435\u043c \u043f\u043e \u0433\u0440\u0430\u0444\u0443. \u041e\u043d \u0432\u0441\u0435\u0433\u0434\u0430 \u0441\u0440\u0430\u0437\u0443 \u043f\u0440\u043e\u0434\u0432\u0438\u0433\u0430\u0435\u0442\u0441\u044f \u043a \u0441\u0430\u043c\u043e\u0439 \u043e\u0442\u0434\u0430\u043b\u0435\u043d\u043d\u043e\u0439 \u043e\u0442 \u0441\u0442\u0430\u0440\u0442\u043e\u0432\u043e\u0439 \u043d\u043e\u0434\u044b \u0432\u0435\u0440\u0448\u0438\u043d\u0435 \u0438 \u0437\u0430\u0442\u0435\u043c, \u0435\u0441\u043b\u0438 \u043d\u0435 \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u043e\u0434\u0432\u0438\u043d\u0443\u0442\u044c\u0441\u044f \u0434\u0430\u043b\u044c\u0448\u0435, \u043e\u0442\u0441\u0442\u0443\u043f\u0430\u0435\u0442 \u043d\u0430\u0437\u0430\u0434.<\/p>\n<p>\u041a\u0430\u043a \u0438 BFS, DFS \u043f\u043e\u043c\u0435\u0447\u0430\u0435\u0442 \u043d\u043e\u0434\u0443 \u043a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437, \u043a\u0430\u043a \u0435\u0435 \u043e\u0431\u043d\u0430\u0440\u0443\u0436\u0438\u0432\u0430\u0435\u0442. \u041d\u0430 \u043a\u0430\u0436\u0434\u043e\u0439 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u043e\u0431\u0445\u043e\u0434\u0438\u0442 \u0432 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u043b\u044c\u043d\u043e\u043c \u043f\u043e\u0440\u044f\u0434\u043a\u0435 \u043d\u043e\u0434\u044b, \u0431\u043b\u0438\u0436\u0430\u0439\u0448\u0438\u0435 \u043a \u0442\u0435\u043a\u0443\u0449\u0435\u0439. \u041d\u0430 \u043f\u0435\u0440\u0432\u043e\u0439 \u0436\u0435 \u043d\u0430\u0439\u0434\u0435\u043d\u043d\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u0435 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0431\u0443\u0434\u0435\u0442 \u043f\u044b\u0442\u0430\u0442\u044c\u0441\u044f \u043d\u0430\u0439\u0442\u0438 \u0431\u043b\u0438\u0436\u0430\u0439\u0448\u0438\u0435 \u043d\u043e\u0434\u044b \u043a \u0443\u0436\u0435 \u0440\u0430\u0437\u0432\u0435\u0434\u0430\u043d\u043d\u043e\u0439 (\u0432 \u044d\u0442\u043e\u043c \u043e\u043d \u043e\u0442\u043b\u0438\u0447\u0430\u0435\u0442\u0441\u044f \u043e\u0442 BFS, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0438\u0441\u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u043d\u043e\u0434\u044b, \u0431\u043b\u0438\u0436\u0430\u0439\u0448\u0438\u0435 \u043a \u0441\u0442\u0430\u0440\u0442\u043e\u0432\u043e\u0439) \u0438 \u0431\u0443\u0434\u0435\u0442 \u0434\u0435\u043b\u0430\u0442\u044c \u044d\u0442\u043e \u043d\u0430 \u043a\u0430\u0436\u0434\u043e\u0439 \u043f\u043e\u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0439 \u0438\u0442\u0435\u0440\u0430\u0446\u0438\u0438 \u0434\u043e \u0442\u0435\u0445 \u043f\u043e\u0440, \u043f\u043e\u043a\u0430 \u043d\u0435 \u043e\u043a\u0430\u0436\u0435\u0442\u0441\u044f \u0432 \u043d\u043e\u0434\u0435, \u0438\u0437 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0435\u043c\u0443 \u043d\u0435\u043a\u0443\u0434\u0430 \u0443\u0439\u0442\u0438. \u0422\u043e\u0433\u0434\u0430 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u043e\u0442\u0441\u0442\u0443\u043f\u0430\u0435\u0442 \u043d\u0430\u0437\u0430\u0434 \u0438 \u043f\u044b\u0442\u0430\u0435\u0442\u0441\u044f \u043f\u0440\u043e\u0434\u0432\u0438\u043d\u0443\u0442\u044c\u0441\u044f \u0434\u0430\u043b\u044c\u0448\u0435 \u043f\u043e \u0434\u0440\u0443\u0433\u043e\u043c\u0443 \u043f\u0443\u0442\u0438. \u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0442\u0430\u043a \u0436\u0435 \u043e\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u0442\u0441\u044f, \u043a\u043e\u0433\u0434\u0430 \u0432\u0441\u0435 \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u044b\u0435 \u043d\u043e\u0434\u044b \u0431\u0443\u0434\u0443\u0442 \u0440\u0430\u0437\u0432\u0435\u0434\u0430\u043d\u044b.<\/p>\n<p>DFS \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442\u0441\u044f \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u0441\u0442\u0435\u043a\u0430 LIFO (last in, first out, \u00ab\u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u043c \u043f\u0440\u0438\u0448\u0451\u043b \u2014 \u043f\u0435\u0440\u0432\u044b\u043c \u0443\u0448\u0451\u043b\u00bb).<\/p>\n<figure class=\"full-width\"><\/figure>\n<p><strong>DFS \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c.<\/strong> <strong>\u0412\u0445\u043e\u0434<\/strong>: \u0433\u0440\u0430\u0444 , \u0433\u0434\u0435  \u044d\u0442\u043e \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u043d\u043e\u0434, \u0430  \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u0440\u0435\u0431\u0435\u0440. \u0421\u0442\u0430\u0440\u0442\u043e\u0432\u0430\u044f \u0432\u0435\u0440\u0448\u0438\u043d\u0430 .<\/p>\n<p><strong>\u0412\u044b\u0445\u043e\u0434<\/strong>: \u0433\u0440\u0430\u0444 \u200b, \u043f\u0440\u0438 \u0443\u0441\u043b\u043e\u0432\u0438\u0438, \u0447\u0442\u043e \u043a\u0430\u0436\u0434\u0430\u044f \u0432\u0435\u0440\u0448\u0438\u043d\u0430 \u0442\u0430\u043a\u043e\u0433\u043e \u0433\u0440\u0430\u0444\u0430 \u0434\u043e\u0441\u0442\u0438\u0436\u0438\u043c\u0430 \u0438\u0437 s \u0442\u043e\u0433\u0434\u0430 \u0438 \u0442\u043e\u043b\u044c\u043a\u043e \u0442\u043e\u0433\u0434\u0430, \u043a\u043e\u0433\u0434\u0430 \u043e\u043d\u0430 \u0440\u0430\u0437\u043c\u0435\u0447\u0435\u043d\u0430 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u043e\u043c \u043a\u0430\u043a \u00ab\u0440\u0430\u0437\u0432\u0435\u0434\u0430\u043d\u043d\u0430\u044f\u00bb.<\/p>\n<p><strong>\u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c DFS<\/strong><\/p>\n<ol>\n<li>\n<p>\u043f\u043e\u043c\u0435\u0442\u0438\u0442\u044c s \u043a\u0430\u043a \u0440\u0430\u0437\u0432\u0435\u0434\u0430\u043d\u043d\u0443\u044e \u0432\u0435\u0440\u0448\u0438\u043d\u0443, \u0432\u0441\u0435 \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0435 \u043a\u0430\u043a \u043d\u0435\u0440\u0430\u0437\u0432\u0435\u0434\u0430\u043d\u043d\u044b\u0435<\/p>\n<\/li>\n<li>\n<p>\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0441\u0442\u0435\u043a S, \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u043e\u0439 s<\/p>\n<\/li>\n<li>\n<p>\u0434\u043e \u0442\u0435\u0445 \u043f\u043e\u0440, \u043f\u043e\u043a\u0430 \u0441\u0442\u0435\u043a S \u043d\u0435\u043f\u0443\u0441\u0442\u043e\u0439:<\/p>\n<\/li>\n<li>\n<p>\u0443\u0434\u0430\u043b\u0438\u0442\u044c \u0432\u0435\u0440\u0448\u0438\u043d\u0443 \u0441\u0432\u0435\u0440\u0445\u0443 \u0441\u0442\u0435\u043a\u0430 S, \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0438\u0432 \u0435\u0435 \u043a\u0430\u043a v<\/p>\n<\/li>\n<li>\n<p>\u0435\u0441\u043b\u0438 v \u043d\u0435 \u0440\u0430\u0437\u0432\u0435\u0434\u0430\u043d\u0430:<\/p>\n<\/li>\n<li>\n<p>\u043f\u043e\u043c\u0435\u0442\u0438\u0442\u044c v \u043a\u0430\u043a \u0440\u0430\u0437\u0432\u0435\u0434\u0430\u043d\u043d\u0443\u044e<\/p>\n<\/li>\n<li>\n<p>\u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0440\u0435\u0431\u0440\u0430 (v,w) \u0432 \u0441\u043f\u0438\u0441\u043a\u0435 \u0441\u043c\u0435\u0436\u043d\u043e\u0441\u0442\u0438 v:<\/p>\n<\/li>\n<li>\n<p>\u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c w \u043d\u0430\u0432\u0435\u0440\u0445 \u0441\u0442\u0435\u043a\u0430 S<\/p>\n<\/li>\n<\/ol>\n<p>\u041a\u0440\u043e\u043c\u0435 \u0442\u043e\u0433\u043e, \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e.<\/p>\n<figure class=\"\"><\/figure>\n<p>\u0420\u0430\u0437\u0431\u0435\u0440\u0451\u043c DFS \u043d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0435:<\/p>\n<figure class=\"full-width\"><\/figure>\n<p>\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f DFS \u043d\u0430 Python \u043c\u043e\u0436\u0435\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u043a\u0430\u043a-\u0442\u043e \u0442\u0430\u043a:<\/p>\n<pre><code class=\"python\">def dfs(graph, start, visited=None):     if visited is None:         visited = set()     visited.add(start)     for next in graph[start] - visited:         dfs(graph, next, visited)     return visited   graph = {'0': {'1', '2'},          '1': {'0', '3', '4'},          '2': {'0'},          '3': {'1'},          '4': {'2', '3'}}   visited = dfs(graph, '0') print(visited)  # {'0', '1', '4', '2', '3'} <\/code><\/pre>\n<figure class=\"\"><\/figure>\n<h3>\u0417\u0430\u0434\u0430\u0447\u0430 \u0442\u043e\u043f\u043e\u043b\u043e\u0433\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 (Topological Sorting)<\/h3>\n<figure class=\"\"><\/figure>\n<p><strong>\u0417\u0430\u0434\u0430\u0447\u0430 \u0442\u043e\u043f\u043e\u043b\u043e\u0433\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438<\/strong> \u2014 \u043a\u0430\u043a \u043f\u0440\u043e\u043d\u0443\u043c\u0435\u0440\u043e\u0432\u0430\u0442\u044c \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u0433\u0440\u0430\u0444\u0430, \u0447\u0442\u043e\u0431\u044b \u043a\u0430\u0436\u0434\u043e\u0435 \u0440\u0435\u0431\u0440\u043e \u0432\u0435\u043b\u043e \u0438\u0437 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0441 \u043c\u0435\u043d\u044c\u0448\u0438\u043c \u043d\u043e\u043c\u0435\u0440\u043e\u043c \u0432 \u0432\u0435\u0440\u0448\u0438\u043d\u0443 \u0441 \u0431\u043e\u043b\u044c\u0448\u0438\u043c \u043d\u043e\u043c\u0435\u0440\u043e\u043c. \u0414\u043b\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u044d\u0442\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438 \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0440\u0430\u0437\u043d\u044b\u0435 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u044b, \u0432 \u0447\u0430\u0441\u0442\u043d\u043e\u0441\u0442\u0438 DFS \u0438 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u041a\u0430\u043d\u0430.<\/p>\n<p>\u0421\u0440\u0430\u0437\u0443 \u0437\u0430\u043c\u0435\u0442\u0438\u043c, \u0447\u0442\u043e \u0433\u0440\u0430\u0444 \u0441 \u0446\u0438\u043a\u043b\u043e\u043c \u0442\u043e\u043f\u043e\u043b\u043e\u0433\u0438\u0447\u0435\u0441\u043a\u0438 \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043d\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u0441\u044f \u2014 \u043a\u0430\u043a \u043d\u0438 \u0440\u0430\u0441\u043f\u043e\u043b\u0430\u0433\u0430\u0439 \u0446\u0438\u043a\u043b \u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0435, \u0432\u0441\u0435 \u0432\u0440\u0435\u043c\u044f \u0438\u0434\u0442\u0438 \u0432\u043f\u0440\u0430\u0432\u043e \u043f\u043e \u0440\u0435\u0431\u0440\u0430\u043c \u0446\u0438\u043a\u043b\u0430 \u043d\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u0441\u044f. \u0412\u0435\u0440\u043d\u043e \u043e\u0431\u0440\u0430\u0442\u043d\u043e\u0435 \u2014 \u0435\u0441\u043b\u0438 \u0446\u0438\u043a\u043b\u0430 \u043d\u0435\u0442, \u0442\u043e \u0435\u0433\u043e \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u043c\u043e\u0436\u043d\u043e \u0442\u043e\u043f\u043e\u043b\u043e\u0433\u0438\u0447\u0435\u0441\u043a\u0438 \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c.<\/p>\n<p>\u0414\u043b\u044f \u043e\u0434\u043d\u043e\u0433\u043e \u0433\u0440\u0430\u0444\u0430 \u043c\u043e\u0436\u0435\u0442 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u043e\u0432\u0430\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u0432 \u0442\u043e\u043f\u043e\u043b\u043e\u0433\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438 (\u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u043a\u043e\u0433\u0434\u0430 \u043e\u043d \u043d\u0435\u0441\u0432\u044f\u0437\u043d\u044b\u0439).<\/p>\n<figure class=\"\"><\/figure>\n<p><strong>\u0421\u043f\u043e\u0441\u043e\u0431\u044b \u0442\u043e\u043f\u043e\u043b\u043e\u0433\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u043a\u0438.<\/strong> \u0417\u0430\u043c\u0435\u0442\u0438\u043c, \u0447\u0442\u043e \u0432\u0435\u0440\u0448\u0438\u043d\u0443, \u0438\u0437 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043d\u0435 \u0432\u0435\u0434\u0435\u0442 \u043d\u0438 \u043e\u0434\u043d\u043e \u0440\u0435\u0431\u0440\u043e, \u043c\u043e\u0436\u043d\u043e \u0432\u0441\u0435\u0433\u0434\u0430 \u043f\u043e\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0439, \u0430 \u0442\u0430\u043a\u0430\u044f \u0432\u0435\u0440\u0448\u0438\u043d\u0430 \u0432 \u0430\u0446\u0438\u043a\u043b\u0438\u0447\u0435\u0441\u043a\u043e\u043c \u0433\u0440\u0430\u0444\u0435 \u0432\u0441\u0435\u0433\u0434\u0430 \u0435\u0441\u0442\u044c (\u0438\u043d\u0430\u0447\u0435 \u043c\u043e\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u0431\u044b \u0438\u0434\u0442\u0438 \u043f\u043e \u043e\u0431\u0440\u0430\u0442\u043d\u044b\u043c \u0440\u0451\u0431\u0440\u0430\u043c \u0431\u0435\u0441\u043a\u043e\u043d\u0435\u0447\u043d\u043e). \u0418\u0437 \u044d\u0442\u043e\u0433\u043e \u0441\u0440\u0430\u0437\u0443 \u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u0438\u0432\u043d\u043e\u0435 \u0434\u043e\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044c\u0441\u0442\u0432\u043e: \u0431\u0443\u0434\u0435\u043c \u0438\u0442\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u043e \u043a\u043b\u0430\u0441\u0442\u044c \u0432 \u043c\u0430\u0441\u0441\u0438\u0432 \u0432\u0435\u0440\u0448\u0438\u043d\u0443, \u0438\u0437 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u0432\u0435\u0434\u0435\u0442, \u0438 \u0443\u0431\u0438\u0440\u0430\u0442\u044c \u0435\u0435 \u0438\u0437 \u0433\u0440\u0430\u0444\u0430. \u041f\u043e\u0441\u043b\u0435 \u044d\u0442\u043e\u0433\u043e \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0430 \u043c\u0430\u0441\u0441\u0438\u0432 \u043d\u0430\u0434\u043e \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0437\u0432\u0435\u0440\u043d\u0443\u0442\u044c.<\/p>\n<p>\u042d\u0442\u043e\u0442 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u043f\u0440\u043e\u0449\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c, \u043e\u0431\u0440\u0430\u0442\u0438\u0432 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043d\u0430 \u0432\u0440\u0435\u043c\u0435\u043d\u0430 \u0432\u044b\u0445\u043e\u0434\u0430 \u0432\u0435\u0440\u0448\u0438\u043d \u0432 DFS. \u0412\u0435\u0440\u0448\u0438\u043d\u0430, \u0438\u0437 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043c\u044b \u0432\u044b\u0439\u0434\u0435\u043c \u043f\u0435\u0440\u0432\u043e\u0439 \u2014 \u0442\u0430, \u0443 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043d\u0435\u0442 \u043d\u043e\u0432\u044b\u0445 \u0438\u0441\u0445\u043e\u0434\u044f\u0449\u0438\u0445 \u0440\u0435\u0431\u0435\u0440. \u0414\u0430\u043b\u044c\u0448\u0435 \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0432\u044b\u0445\u043e\u0434\u0438\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u0438\u0437 \u0442\u0435\u0445 \u0432\u0435\u0440\u0448\u0438\u043d, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0435\u0441\u043b\u0438 \u0438 \u0438\u043c\u0435\u044e\u0442 \u0438\u0441\u0445\u043e\u0434\u044f\u0449\u0438\u0435<\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-369857","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/369857","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=369857"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/369857\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=369857"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=369857"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=369857"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}