{"id":256561,"date":"2015-05-05T00:07:02","date_gmt":"2015-05-04T20:07:02","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=256561"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=256561","title":{"rendered":"\u041e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f \u043a\u043e\u0434\u0430 \u043d\u0430 C++ (C++11) \u043d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438"},"content":{"rendered":"<p>     \t\u0425\u043e\u0447\u0435\u0442\u0441\u044f \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u0430\u0442\u044c \u043f\u0440\u043e \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044e \u0421\/C++ \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c, \u0438\u0431\u043e \u0432 \u0438\u043d\u0442\u0435\u0440\u043d\u0435\u0442\u0435 \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u043c\u0430\u043b\u043e \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438. \u0412 \u043f\u043e\u0441\u0442\u0435 \u0431\u0443\u0434\u0443\u0442 \u043e\u0431\u044a\u044f\u0441\u043d\u0435\u043d\u0438\u044f \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0439 \u043d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u0437\u0430\u0434\u0430\u0447\u0438, \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u043c\u043d\u0435 \u0434\u0430\u043b\u0438 \u0440\u0435\u0448\u0438\u0442\u044c \u0432 \u0438\u043d\u0441\u0442\u0438\u0442\u0443\u0442\u0435 (\u0437\u0430\u0434\u0430\u0447\u0430 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u0430 \u043d\u0430 \u0421++11, \u043e\u0431\u044a\u044f\u0441\u043d\u0435\u043d\u0438\u0435 \u0434\u0430\u0435\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u043b\u044f 64 \u0431\u0438\u0442\u043d\u044b\u0445 \u0441\u0438\u0441\u0442\u0435\u043c).<br \/>  \u0412 \u043f\u0435\u0440\u0432\u0443\u044e \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0441\u0442\u0430\u0442\u044c\u044f \u043f\u0438\u0441\u0430\u043b\u0430\u0441\u044c, \u0447\u0442\u043e\u0431\u044b \u0431\u043e\u043b\u044c\u0448\u0435 \u0443\u0437\u043d\u0430\u0442\u044c \u043f\u0440\u043e \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044e, \u0441\u043e\u0432\u0435\u0442\u044b \u043f\u043e \u043a\u043e\u0442\u043e\u0440\u043e\u0439, \u044f \u043d\u0430\u0434\u0435\u044e\u0441\u044c, \u0432\u044b \u043e\u0441\u0442\u0430\u0432\u0438\u0442\u0435 \u0432 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u044f\u0445. <br \/>  <a name=\"habracut\"><\/a><\/p>\n<h5>\u0423\u0441\u043b\u043e\u0432\u0438\u0435 \u0437\u0430\u0434\u0430\u0447\u0438 \u0438 \u0446\u0435\u043b\u044c<\/h5>\n<p>  \u0417\u0430\u0434\u0430\u0447\u0430 \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u0442\u0440\u0438\u0432\u0438\u0430\u043b\u044c\u043d\u0430:  <\/p>\n<blockquote><p>\u0412 \u0433\u043e\u0440\u043e\u0434\u0435 M \u0441\u0442\u0440\u043e\u044f\u0442\u0441\u044f N \u043d\u043e\u0432\u044b\u0445 \u043c\u0438\u043a\u0440\u043e\u0440\u0430\u0439\u043e\u043d\u043e\u0432, \u043c\u0435\u0436\u0434\u0443 \u043a\u043e\u0442\u043e\u0440\u044b\u043c\u0438 \u0437\u0430\u043f\u043b\u0430\u043d\u0438\u0440\u043e\u0432\u0430\u043d\u043e M \u043c\u0430\u0433\u0438\u0441\u0442\u0440\u0430\u043b\u044c\u043d\u044b\u0445 \u0434\u043e\u0440\u043e\u0433. \u041a\u0430\u0436\u0434\u0430\u044f \u0434\u043e\u0440\u043e\u0433\u0430 \u0438\u043c\u0435\u0435\u0442 \u0441\u0432\u043e\u044e \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u0441\u0442\u0440\u043e\u0438\u0442\u0435\u043b\u044c\u0441\u0442\u0432\u0430 Pij. \u0412 \u043f\u0440\u043e\u0448\u043b\u043e\u043c \u0433\u043e\u0434\u0443 \u0438\u0437 \u044d\u0442\u0438\u0445 M \u0434\u043e\u0440\u043e\u0433 \u043c\u0443\u043d\u0438\u0446\u0438\u043f\u0430\u043b\u0438\u0442\u0435\u0442 \u0443\u0441\u043f\u0435\u043b \u043f\u043e\u0441\u0442\u0440\u043e\u0438\u0442\u044c K \u0448\u0442\u0443\u043a. \u041a \u0441\u043e\u0436\u0430\u043b\u0435\u043d\u0438\u044e, \u0432 \u044d\u0442\u043e\u043c \u0433\u043e\u0434\u0443 \u0444\u0438\u043d\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441\u0442\u0440\u043e\u0438\u0442\u0435\u043b\u044c\u0441\u0442\u0432\u0430 \u0440\u0435\u0448\u0435\u043d\u043e \u0431\u044b\u043b\u043e \u0441\u043e\u043a\u0440\u0430\u0442\u0438\u0442\u044c, \u0438 \u0442\u0435\u043f\u0435\u0440\u044c \u0438\u0437 \u0437\u0430\u043f\u043b\u0430\u043d\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0445, \u043d\u043e \u043d\u0435\u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u044b\u0445 M-K \u0434\u043e\u0440\u043e\u0433 \u043d\u0443\u0436\u043d\u043e \u043e\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0442\u0430\u043a\u0438\u0435, \u0447\u0442\u043e\u0431\u044b \u0438\u0437 \u043b\u044e\u0431\u043e\u0433\u043e \u043c\u0438\u043a\u0440\u043e\u0440\u0430\u0439\u043e\u043d\u0430 \u0432 \u043b\u044e\u0431\u043e\u0439 \u0434\u0440\u0443\u0433\u043e\u0439 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u043e\u0432\u0430\u043b \u0445\u043e\u0442\u044f \u0431\u044b \u043e\u0434\u0438\u043d \u043f\u0443\u0442\u044c, \u043d\u043e \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u0438\u0445 \u0441\u0442\u0440\u043e\u0438\u0442\u0435\u043b\u044c\u0441\u0442\u0432\u0430 \u0431\u044b\u043b\u0430 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0439 (\u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u0443\u0435\u0442\u0441\u044f, \u0447\u0442\u043e \u0442\u0430\u043a\u043e\u0439 \u043d\u0430\u0431\u043e\u0440 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442).<br \/>  \u041a\u0430\u043a\u043e\u0432\u0430 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0430\u044f \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c P \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f \u0441\u0442\u0440\u043e\u0438\u0442\u0435\u043b\u044c\u0441\u0442\u0432\u0430 \u0434\u043e\u0440\u043e\u0436\u043d\u043e\u0439 \u0441\u0435\u0442\u0438 \u043f\u043e \u043d\u043e\u0432\u043e\u043c\u0443 \u043f\u043b\u0430\u043d\u0443, \u0438 \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043d\u043e\u0432\u044b\u0445 \u0434\u043e\u0440\u043e\u0433 \u043f\u043e \u043d\u0435\u043c\u0443 \u043f\u0440\u0435\u0434\u0441\u0442\u043e\u0438\u0442 \u043f\u043e\u0441\u0442\u0440\u043e\u0438\u0442\u044c?<\/p><\/blockquote>\n<p>  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0432\u0432\u043e\u0434\/\u0432\u044b\u0432\u043e\u0434<\/b><\/p>\n<div class=\"spoiler_text\">\u0424\u043e\u0440\u043c\u0430\u0442 \u0432\u0432\u043e\u0434\u0430:<br \/>  \u041f\u0435\u0440\u0432\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0447\u0435\u0440\u0435\u0437 \u043f\u0440\u043e\u0431\u0435\u043b \u0434\u0432\u0430 \u0447\u0438\u0441\u043b\u0430:<br \/>  N (2 \u2264 N \u2264 10^5) \u2014 \u0447\u0438\u0441\u043b\u043e \u0441\u0442\u0440\u043e\u044f\u0449\u0438\u0445\u0441\u044f \u043c\u0438\u043a\u0440\u043e\u0440\u0430\u0439\u043e\u043d\u043e\u0432,<br \/>  M (1 \u2264 M \u2264 min(N(N \u2014 1), 2 * 10^6)) \u2014 \u0447\u0438\u0441\u043b\u043e \u0437\u0430\u043f\u043b\u0430\u043d\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0445 \u0434\u043e\u0440\u043e\u0433.<br \/>  \u0421\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 M \u0441\u0442\u0440\u043e\u043a \u043e\u043f\u0438\u0441\u044b\u0432\u0430\u044e\u0442 \u0437\u0430\u043f\u043b\u0430\u043d\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u0434\u043e\u0440\u043e\u0433\u0438, \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u043f\u043e \u043f\u0430\u0440\u0435 \u043d\u043e\u043c\u0435\u0440\u043e\u0432 \u0441\u0432\u043e\u0438\u0445 \u043a\u043e\u043d\u0446\u043e\u0432, \u0438 \u043a\u0430\u0436\u0434\u0430\u044f \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0447\u0435\u0440\u0435\u0437 \u043f\u0440\u043e\u0431\u0435\u043b \u0442\u0440\u0438 \u0446\u0435\u043b\u044b\u0445 \u0447\u0438\u0441\u043b\u0430:<br \/>  i (1 \u2264 i &lt; N) \u2014 \u043d\u043e\u043c\u0435\u0440 \u043c\u0438\u043a\u0440\u043e\u0440\u0430\u0439\u043e\u043d\u0430, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0434\u043e\u0440\u043e\u0433\u0430 \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u0442\u0441\u044f,<br \/>  j (i &lt; j \u2264 N) \u2014 \u043d\u043e\u043c\u0435\u0440 \u043c\u0438\u043a\u0440\u043e\u0440\u0430\u0439\u043e\u043d\u0430, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0434\u043e\u0440\u043e\u0433\u0430 \u0437\u0430\u043a\u0430\u043d\u0447\u0438\u0432\u0430\u0435\u0442\u0441\u044f,<br \/>  Pij (0 \u2264 Pij \u2264 103) \u2014 \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u0441\u0442\u0440\u043e\u0438\u0442\u0435\u043b\u044c\u0441\u0442\u0432\u0430 \u0434\u043e\u0440\u043e\u0433\u0438. 0, \u0435\u0441\u043b\u0438 \u0434\u043e\u0440\u043e\u0433\u0430 \u0443\u0436\u0435 \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0430.<\/p>\n<p>  \u0424\u043e\u0440\u043c\u0430\u0442 \u0432\u044b\u0432\u043e\u0434\u0430:<br \/>  \u0412\u044b\u0432\u043e\u0434 \u0434\u043e\u043b\u0436\u0435\u043d \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u0434\u0432\u0430 \u0447\u0438\u0441\u043b\u0430 \u0447\u0435\u0440\u0435\u0437 \u043f\u0440\u043e\u0431\u0435\u043b: \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c P \u0437\u0430\u0432\u0435\u0440\u0448\u0435\u043d\u0438\u044f \u0441\u0442\u0440\u043e\u0438\u0442\u0435\u043b\u044c\u0441\u0442\u0432\u0430 \u0434\u043e\u0440\u043e\u0436\u043d\u043e\u0439 \u0441\u0435\u0442\u0438 \u043f\u043e \u043d\u043e\u0432\u043e\u043c\u0443 \u043f\u043b\u0430\u043d\u0443 \u0438 L \u2014 \u0447\u0438\u0441\u043b\u043e \u0434\u043e\u0440\u043e\u0433, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u043e\u0438\u0442 \u043f\u043e \u043d\u0435\u043c\u0443 \u043f\u043e\u0441\u0442\u0440\u043e\u0438\u0442\u044c.  <\/div>\n<\/div>\n<p>  \u0438 \u0440\u0435\u0448\u0430\u0435\u0442\u0441\u044f <a href=\"https:\/\/ru.wikipedia.org\/wiki\/\u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c_\u041a\u0440\u0443\u0441\u043a\u0430\u043b\u0430\">\u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u043e\u043c \u041a\u0440\u0443\u0441\u043a\u0430\u043b\u0430<\/a> (\u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435 \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). \u0418\u0437\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e \u0432 \u043a\u043e\u043d\u0442\u0435\u0441\u0442\u0435 \u0431\u044b\u043b\u0438 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f \u0432 2 \u0441\u0435\u043a\u0443\u043d\u0434\u044b \u043d\u0430 \u0438\u0441\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u0438 24 \u043c\u0431 \u043f\u0430\u043c\u044f\u0442\u0438. \u041d\u0435 \u0441\u0435\u0440\u044c\u0435\u0437\u043d\u043e. \u041f\u0435\u0440\u0435\u0434 \u0441\u043e\u0431\u043e\u0439 \u044f \u043f\u043e\u0441\u0442\u0430\u0432\u0438\u043b \u0446\u0435\u043b\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0443 \u0438 \u0443\u043b\u043e\u0436\u0438\u0442\u044c\u0441\u044f \u0432 100\u043c\u0441 \u0434\u043b\u044f \u0441\u0430\u043c\u043e\u0433\u043e \u0441\u043b\u043e\u0436\u043d\u043e\u0433\u043e \u0442\u0435\u0441\u0442\u0430, \u0445\u043e\u0447\u0443 \u0437\u0430\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043c\u043e\u0436\u043d\u043e \u0442\u043e\u043b\u044c\u043a\u043e STL \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0443 \u0438 \u0444\u0438\u043a\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u0430 (\u043a\u0430\u043a \u043d\u0438\u043a\u0430\u043a \u044d\u0442\u043e \u0437\u0430\u0434\u0430\u0447\u0430 \u0438\u0437 \u043a\u043e\u043d\u0442\u0435\u0441\u0442\u0430). \u041f\u043e\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u043d\u0430\u044f \u0437\u0430\u0434\u0430\u0447\u0430 \u0431\u044b\u043b\u0430 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0430 \u043d\u0435 \u0434\u043e \u043a\u043e\u043d\u0446\u0430 \u2014 \u044f \u0443\u043b\u043e\u0436\u0438\u043b\u0441\u044f \u0432 122 \u043c\u0441 \u0438 11\u043c\u0431 \u043f\u0430\u043c\u044f\u0442\u0438. \u0414\u043b\u044f \u0441\u0430\u043c\u044b\u0445 \u043d\u0435\u0442\u0435\u0440\u043f\u0435\u043b\u0438\u0432\u044b\u0445 \u043f\u0440\u0438\u0432\u043e\u0436\u0443 \u043a\u043e\u0434 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b:  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u041a\u043e\u0434 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"cpp\">#include &lt;iostream&gt; #include &lt;vector&gt; #include &lt;algorithm&gt;  \/*  * \u0414\u0430\u043d\u043d\u0430\u044f \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u043e\u0431\u0443\u0441\u043b\u043e\u0432\u043b\u0435\u043d\u0430 \u0444\u043e\u0440\u043c\u0430\u0442\u043e\u043c \u0432\u0445\u043e\u0434\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445  *\/ template &lt;class T&gt; struct Triplet {     T first;   \/\/ \u0413\u043e\u0440\u043e\u0434, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0434\u043e\u0440\u043e\u0433\u0430 \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u0442\u0441\u044f     T second;  \/\/ \u0413\u043e\u0440\u043e\u0434, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0434\u043e\u0440\u043e\u0433\u0430 \u0437\u0430\u043a\u0430\u043d\u0447\u0438\u0432\u0430\u0435\u0442\u0441\u044f     T third;   \/\/ \u0426\u0435\u043d\u0430 \u0441\u0442\u0440\u043e\u0438\u0442\u0435\u043b\u044c\u0441\u0442\u0432\u0430 \u0434\u043e\u0440\u043e\u0433\u0438 };  \/*  * \u041a\u0430\u0436\u0434\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 UnionFind \u044d\u0442\u043e \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e  *\/ template &lt;class T&gt; class UF_element { public:     T parent;   \/\/ \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u0438\u0442\u0435\u043b\u044c \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430     T size;     \/\/ \u0440\u0430\u0437\u043c\u0435\u0440 \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430      UF_element() {}      UF_element(T id)             : size(1)             , parent(id)     {     } };  template &lt;class T&gt; class UnionFind { private:     std::vector&lt;UF_element&lt;T&gt;&gt; Elements; public: \/*  * \u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u0433\u043e\u0440\u043e\u0434 \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0438\u0437 \u043c\u0438\u043a\u0440\u043e\u0440\u0430\u0439\u043e\u043d\u043e\u0432 \u043d\u0435 \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u043d\u044b\u0445 \u0434\u043e\u0440\u043e\u0433\u0430\u043c\u0438  *\/     UnionFind(T n)             : Elements(n + 1)     {         for (register T i = 1; i &lt; n + 1; ++i) {             Elements[i] = UF_element&lt;T&gt;(i);         }     }  \/*  * \u041f\u0440\u0438 \u043f\u0435\u0440\u0432\u043e\u043c \u0432\u044b\u0437\u043e\u0432\u0435 \u043f\u043e\u0438\u0441\u043a\u0430 \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044f, \u0441\u043e\u043a\u0440\u0430\u0449\u0430\u0435\u043c \u043f\u0443\u0442\u044c \u0434\u043e \u043d\u0435\u0433\u043e.   *     \u043f\u0435\u0440\u0432\u044b\u0439 \u043f\u043e\u0438\u0441\u043a \u0437\u0430 \u041e(log n),  *     \u043f\u043e\u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u0432\u044b\u0437\u043e\u0432\u044b \u0437\u0430 O(1)  *\/     T find(T id){         UF_element&lt;T&gt;& current = Elements[id];          while (current.parent != Elements[current.parent].parent){             current.parent = Elements[current.parent].parent;         }         return current.parent;     }      void merge(T a, T b) {         UF_element&lt;T&gt;& UF_elem_A = Elements[find(a)];         UF_element&lt;T&gt;& UF_elem_B = Elements[find(b)];          if(UF_elem_A.parent == UF_elem_B.parent)             return;          if(UF_elem_A.size &lt; UF_elem_B.size) {             UF_elem_A.parent = b;             UF_elem_B.size += UF_elem_A.size;         } else {             UF_elem_B.parent = a;             UF_elem_A.size += UF_elem_B.size;         }     } };  template &lt;typename T&gt; bool comp(const Triplet&lt;T&gt;& a, const Triplet&lt;T&gt;& b) {     return (a.third &lt; b.third); }  template &lt;typename T&gt; void kruskal(std::vector&lt;Triplet&lt;T&gt;&gt;& data, size_t& cost, size_t& count, const size_t& n) {     UnionFind&lt;T&gt; N(n);      for (size_t i = 0; i &lt; data.size(); ++i) {         if (N.find(data[i].first) != N.find(data[i].second)) {             if (data[i].third) {         \/\/ \u0435\u0441\u043b\u0438 \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u0434\u043e\u0440\u043e\u0433\u0438 \u043d\u0435 0 (\u0442.\u0435 \u0434\u043e\u0440\u043e\u0433\u0430 \u0435\u0449\u0435 \u043d\u0435 \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0430)                 cost += data[i].third;   \/\/ \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0432\u0430\u0435\u043c \u0441\u0442\u043e\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u043e\u0441\u0442\u0440\u043e\u0439\u043a\u0438 \u0434\u043e\u0440\u043e\u0433                 ++count;                 \/\/ \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0432\u0430\u0435\u043c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0434\u043e\u0440\u043e\u0433, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0430\u0434\u043e \u043f\u043e\u0441\u0442\u0440\u043e\u0438\u0442\u044c             }              N.merge(data[i].first, data[i].second);         }     } }  \/*  * \u041f\u0430\u0440\u0441\u0435\u0440 \u0434\u043b\u044f \u043b\u044e\u0431\u044b\u0445 \u043d\u0435\u043e\u0442\u0440\u0438\u0446\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u0446\u0435\u043b\u043e\u0447\u0438\u0441\u043b\u0435\u043d\u043d\u044b\u0445 \u0442\u0438\u043f\u043e\u0432  *\/ template &lt;typename T&gt; void read_unlocked(T &out) {     T c = getchar_unlocked();      for (; '0' &lt;= c && c &lt;= '9' ; c = getchar_unlocked()) {         if (c == ' ' || c == '\\n')             return;         out = out * 10 + c - '0';     } }  int main() {     std::ios_base::sync_with_stdio(false);      size_t count = 0, cost = 0;     unsigned int n, m;      read_unlocked(n);     read_unlocked(m);      std::vector&lt;Triplet&lt;unsigned int&gt;&gt; input(m);      for (size_t i = 0; i &lt; m; ++i) {         read_unlocked(input[i].first);         read_unlocked(input[i].second);         read_unlocked(input[i].third);     }      std::sort(input.begin(), input.end(), comp&lt;unsigned int&gt;);     kruskal(input, cost, count, n);      std::cout &lt;&lt; cost &lt;&lt; &quot; &quot; &lt;&lt; count &lt;&lt; '\\n'; } <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  <\/p>\n<h5>\u0412\u044b\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u043d\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0445<\/h5>\n<p>  \u041f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435 \u0443\u0437\u043d\u0430\u0442\u044c, \u0447\u0442\u043e \u0442\u0430\u043a\u043e\u0435 \u0432\u044b\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u043d\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 \u043c\u043e\u0436\u043d\u043e <a href=\"https:\/\/ru.wikipedia.org\/wiki\/\u0412\u044b\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u043d\u0438\u0435_\u0434\u0430\u043d\u043d\u044b\u0445\">\u0442\u0443\u0442<\/a>, \u044f \u043b\u0438\u0448\u044c \u043f\u043e\u044f\u0441\u043d\u044e \u043d\u0430 \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u044b\u0445 \u043f\u0440\u0438\u043c\u0435\u0440\u0430\u0445. \u041a \u0441\u043e\u0436\u0430\u043b\u0435\u043d\u0438\u044e, \u0432 \u043c\u043e\u0435\u043c \u043a\u043e\u0434\u0435 \u044d\u0442\u043e \u043d\u0438\u0433\u0434\u0435 \u044f\u0432\u043d\u043e \u043d\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f, \u043d\u043e \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0430 \u0442\u0430\u043a\u0443\u044e \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443:  <\/p>\n<pre><code class=\"cpp\">struct A {     char a;     long int b;     int c; }; <\/code><\/pre>\n<p>  \u042d\u0442\u0430 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 24 \u0431\u0430\u0439\u0442\u0430 (\u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0432\u044b\u0432\u0435\u0434\u0435\u043c \u043d\u0430 \u044d\u043a\u0440\u0430\u043d \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 sizeof(A)). <br \/>  \u0422\u0435\u043f\u0435\u0440\u044c \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0442\u0430\u043a\u0443\u044e \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443:  <\/p>\n<pre><code class=\"cpp\">struct B {     char a;     int c;     long int b; }; <\/code><\/pre>\n<p>  \u041e\u043d\u0430 \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 \u0432\u0441\u0435\u0433\u043e 16 \u0431\u0430\u0439\u0442. \u041f\u043e\u0447\u0435\u043c\u0443 \u0442\u0430\u043a? \u0414\u0435\u043b\u043e \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u043d\u0430 64 \u0431\u0438\u0442\u043d\u043e\u0439 \u041e\u0421 \u043f\u0430\u043c\u044f\u0442\u044c \u0441\u0447\u0438\u0442\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043a\u0443\u0441\u043a\u0430\u043c\u0438 \u043f\u043e 8 \u0431\u0430\u0439\u0442 \u0438 \u0434\u043b\u044f \u0443\u0441\u043a\u043e\u0440\u0435\u043d\u0438\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0440\u043e\u043c \u0438\u0434\u0435\u0442 \u0432\u044b\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u043d\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0445, \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e \u0434\u043b\u044f \u043a\u043b\u0430\u0441\u0441\u0430 \u0410 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0440\u0430\u0441\u043f\u043e\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0432 \u043f\u0430\u043c\u044f\u0442\u0438 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0442\u0430\u043a:<br \/>   (1 \u0431\u0430\u0439\u0442 \u043f\u043e\u0434 a + 7 \u043f\u0443\u0441\u0442\u044b\u0445 \u0431\u0430\u0439\u0442) + (8 \u0431\u0430\u0439\u0442 \u043f\u043e\u0434 b) + (4 \u0431\u0430\u0439\u0442\u0430 \u043f\u043e\u0434 c + 4 \u043f\u0443\u0441\u0442\u044b\u0445 \u0431\u0430\u0439\u0442\u0430) = 24 \u0431\u0430\u0439\u0442\u0430.<br \/>   \u041d\u0435 \u043e\u0447\u0435\u043d\u044c \u044d\u043a\u043e\u043d\u043e\u043c\u043d\u043e, \u043f\u0440\u0430\u0432\u0434\u0430? \u0414\u043b\u044f \u043a\u043b\u0430\u0441\u0441\u0430 B \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u043c\u0430\u044f \u043f\u0430\u043c\u044f\u0442\u044c \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0442\u0430\u043a:<br \/>   (1 \u0431\u0430\u0439\u0442 \u043f\u043e\u0434 a + 3 \u043f\u0443\u0441\u0442\u044b\u0445 \u0431\u0430\u0439\u0442\u0430 + 4 \u0431\u0430\u0439\u0442\u0430 \u043f\u043e\u0434 c) + (8 \u0431\u0430\u0439\u0442 \u043f\u043e\u0434 b) = 16 \u0431\u0430\u0439\u0442. <br \/>  \u041d\u0430 \u0442\u0430\u043a\u0443\u044e \u043c\u0435\u043b\u043e\u0447\u044c \u0441\u0442\u043e\u0438\u0442 \u043e\u0431\u0440\u0430\u0449\u0430\u0442\u044c \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u043f\u0435\u0440\u0435\u043c\u0435\u0449\u0435\u043d\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 \u00ab\u0442\u0443\u0434\u0430-\u0441\u044e\u0434\u0430\u00bb \u0441\u0442\u043e\u0438\u0442 \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u0438 \u0447\u0435\u043c \u043c\u0435\u043d\u044c\u0448\u0435 \u0434\u0430\u043d\u043d\u044b\u0445 \u043f\u0435\u0440\u0435\u043c\u0435\u0449\u0430\u0435\u0442\u0441\u044f, \u0442\u0435\u043c \u0441\u0438\u043b\u044c\u043d\u0435\u0435 \u0443\u0441\u043a\u043e\u0440\u044f\u0435\u0442\u0441\u044f \u0432\u0430\u0448\u0430 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0430, \u0434\u0430 \u0438 \u043f\u043e\u0442\u043e\u043c, \u043f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u043c\u0435\u0441\u0442\u0430\u043c\u0438 \u043f\u0430\u0440\u0443 \u0441\u0442\u0440\u043e\u0447\u0435\u043a \u043d\u0435 \u0441\u0430\u043c\u0430\u044f \u0441\u043b\u043e\u0436\u043d\u0430\u044f \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044f.<\/p>\n<h5>\u0421\u043f\u0438\u0441\u043a\u0438 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438<\/h5>\n<p>  \u041f\u043e\u0447\u0435\u043c\u0443 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 \u0434\u043e\u043b\u0436\u0435\u043d \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u0442\u0430\u043a:  <\/p>\n<pre><code class=\"cpp\">UF_element(T id)         : size(1)         , parent(id) { } <\/code><\/pre>\n<p>  \u0410 \u043d\u0435 \u0442\u0430\u043a:  <\/p>\n<pre><code class=\"cpp\">UF_element(T id) {     size = 1;     parent = id; } <\/code><\/pre>\n<p>  \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0447\u0438\u0442\u0430\u0442\u044c <a href=\"http:\/\/habrahabr.ru\/post\/58818\/\">\u0442\u0443\u0442<\/a>. \u041f\u0440\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 \u0441\u043f\u0438\u0441\u043a\u043e\u0432 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0432 \u0432\u044b\u0437\u043e\u0432 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u0430 \u0441 \u043f\u0435\u0440\u0435\u0434\u0430\u043d\u043d\u044b\u043c\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f\u043c\u0438, \u0432 \u043e\u0442\u043b\u0438\u0447\u0438\u0438 \u043e\u0442 \u0432\u0442\u043e\u0440\u043e\u0433\u043e \u0441\u043b\u0443\u0447\u0430\u044f, \u043a\u043e\u0433\u0434\u0430 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440 \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e \u0434\u043b\u044f \u043f\u043e\u043b\u0435\u0439 \u043a\u043b\u0430\u0441\u0441\u0430 \u0438 \u0432 \u0442\u0435\u043b\u0435 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u0435 \u043a\u043b\u0430\u0441\u0441\u0430 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0441\u044f \u043f\u0440\u0438\u0441\u0432\u0430\u0438\u0432\u0430\u043d\u0438\u0435.<\/p>\n<h5>\u0421\u043f\u0435\u0446\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440 register<\/h5>\n<p>  \u041d\u0435\u043c\u043d\u043e\u0436\u043a\u043e \u0442\u0435\u043e\u0440\u0438\u0438:  <\/p>\n<blockquote><p>register \u2014 \u0441\u043f\u0435\u0446\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430 \u043f\u0430\u043c\u044f\u0442\u0438. \u041f\u0440\u0438\u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f \u043a \u043e\u0431\u044a\u0435\u043a\u0442\u0430\u043c, \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e \u0440\u0430\u0441\u043f\u043e\u043b\u0430\u0433\u0430\u0435\u043c\u044b\u043c \u0432 \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u0438. \u041f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0438\u0437 \u0441\u0435\u0431\u044f <b><u>\u00ab\u043f\u0440\u043e\u0441\u044c\u0431\u0443\u00bb<\/u><\/b>(\u043d\u0435\u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u0435\u043d \u043a \u0438\u0441\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044e) \u043a \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u0443 \u043e \u0440\u0430\u0437\u043c\u0435\u0449\u0435\u043d\u0438\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432, \u043e\u0431\u044a\u044f\u0432\u043b\u0435\u043d\u043d\u044b\u0445 \u0441\u043e \u0441\u043f\u0435\u0446\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u043e\u043c register \u0432 \u043e\u0434\u043d\u043e\u043c \u0438\u0437 \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u044b\u0445 \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u043e\u0432, \u0430 \u043d\u0435 \u0432 \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u0438.<\/p><\/blockquote>\n<p>  \u0412 \u0441\u0432\u043e\u0435\u043c \u043a\u043e\u0434\u0435 \u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b \u0442\u0430\u043a\u0443\u044e \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u044e:  <\/p>\n<pre><code class=\"cpp\">for (register T i = 1; i &lt; n + 1; ++i) {     idElements[i] = UF_element&lt;T&gt;(i); } <\/code><\/pre>\n<p>  \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u044d\u0442\u043e\u0442 \u0441\u043f\u0435\u0446\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440 \u043d\u0430\u0434\u043e \u0433\u0440\u0430\u043c\u043e\u0442\u043d\u043e.<br \/>  \u0415\u0441\u043b\u0438 \u043d\u0430\u0434\u043e \u0437\u0430\u0431\u0438\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 \u0447\u0438\u0441\u043b\u0430\u043c\u0438 \u043e\u0442 1 \u0434\u043e n (\u043f\u043e\u0447\u0442\u0438 \u043a\u0430\u043a \u0443 \u043c\u0435\u043d\u044f \u0432 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u0432\u044b\u0448\u0435), \u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0441\u043f\u0435\u0446\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u0430 \u0434\u0430\u0441\u0442 \u043f\u0440\u0438\u0431\u0430\u0432\u043a\u0443 \u0432 \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u0438, \u0435\u0441\u043b\u0438 \u0442\u0435\u043b\u043e \u0446\u0438\u043a\u043b\u0430 \u043d\u0435 \u0442\u0430\u043a\u043e\u0435 \u0442\u0440\u0438\u0432\u0438\u0430\u043b\u044c\u043d\u043e\u0435, \u0442\u043e \u0441\u043a\u043e\u0440\u0435\u0435 \u0432\u0441\u0435\u0433\u043e register \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0435 \u0438\u0437\u043c\u0435\u043d\u0438\u0442 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c, \u043d\u043e \u043c\u043e\u0436\u0435\u0442 \u0438 \u0443\u043c\u0435\u043d\u044c\u0448\u0438\u0442\u044c \u0435\u0433\u043e, \u0432\u044b\u0434\u0435\u043b\u0438\u0432 \u043f\u043e\u0434 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0443\u044e \u0440\u0435\u0433\u0438\u0441\u0442\u0440.<\/p>\n<h5>\u0420\u0430\u0431\u043e\u0442\u0430 \u0441 \u043f\u0430\u043c\u044f\u0442\u044c\u044e<\/h5>\n<p>  \u041e\u0447\u0435\u043d\u044c \u043a\u043e\u0440\u043e\u0442\u043a\u043e \u0438 \u0445\u043e\u0440\u043e\u0448\u043e \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u043e <a href=\"http:\/\/habrahabr.ru\/post\/148657\/\">\u0442\u0443\u0442<\/a>. \u0412\u043e\u043e\u0431\u0449\u0435, \u043a\u043e\u0433\u0434\u0430 \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u0448\u044c \u0440\u0430\u0437\u0431\u0438\u0440\u0430\u0442\u044c\u0441\u044f \u043a\u0430\u043a \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u043f\u0430\u043c\u044f\u0442\u044c \u0438, \u0432 \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u0438, \u0438\u0449\u0435\u0448\u044c \u0443\u0442\u0435\u0447\u043a\u0438, \u0432\u044b\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u044e\u0442\u0441\u044f \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u0440\u0430\u0432\u0438\u043b\u0430. \u0414\u043b\u044f \u0441\u0435\u0431\u044f \u044f \u0443\u044f\u0441\u043d\u0438\u043b \u043e\u0434\u043d\u043e: \u0434\u043e \u0442\u0435\u0445 \u043f\u043e\u0440, \u043f\u043e\u043a\u0430 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0441\u0442 \u043d\u0435 \u0432 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0438 \u0441 \u0445\u043e\u0434\u0443 \u0441\u043a\u0430\u0437\u0430\u0442\u044c \u043a\u043e\u0433\u0434\u0430 \u0438 \u0433\u0434\u0435 \u043e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0430\u0435\u0442\u0441\u044f \u043f\u0430\u043c\u044f\u0442\u044c, \u0432\u044b\u0434\u0435\u043b\u0435\u043d\u043d\u0430\u044f \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u0430 new \u0438 \u043d\u0430\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0443\u0434\u043e\u0431\u043d\u043e \u0431\u0443\u0434\u0435\u0442 \u043c\u0435\u043d\u0435\u0434\u0436\u0435\u0440\u0443 \u043f\u0430\u043c\u044f\u0442\u0438 \u043e\u0431\u0440\u0430\u0449\u0430\u0442\u044c\u0441\u044f \u043a \u043e\u0441\u0432\u043e\u0431\u043e\u0434\u0438\u0432\u0448\u0435\u0439\u0441\u044f \u043f\u0430\u043c\u044f\u0442\u0438 \u043f\u043e\u0441\u043b\u0435 \u0432\u044b\u0437\u043e\u0432\u0430 delete, \u0435\u0433\u043e \u043d\u0430\u0434\u043e \u0431\u0438\u0442\u044c \u0441\u0441\u0430\u043d\u044b\u043c\u0438 \u0442\u0440\u044f\u043f\u043a\u0430\u043c\u0438 \u0437\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 new\/delete. \u041f\u043e\u044d\u0442\u043e\u043c\u0443, \u0447\u0442\u043e\u0431\u044b \u043d\u0435 \u0431\u044b\u0442\u044c \u043f\u043e\u0431\u0438\u0442\u044b\u043c, \u044f \u0438\u0437\u0431\u0430\u0432\u0438\u043b\u0441\u044f \u043e\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u044d\u0442\u0438\u0445 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u043e\u0432 \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u043d\u043e:   <\/p>\n<pre><code class=\"cpp\"> UnionFind(T n)             : Elements(n + 1) \/\/ \u0442\u0443\u0442 \u044f \u0432\u044b\u0434\u0435\u043b\u044f\u044e \u043d\u0443\u0436\u043d\u044b\u0439 \u043c\u043d\u0435 \u043a\u0443\u0441\u043e\u043a \u043f\u0430\u043c\u044f\u0442\u0438     {         for (register T i = 1; i &lt; n + 1; ++i) {             Elements[i] = UF_element&lt;T&gt;(i); \/\/ \u0442\u0443\u0442 \u0435\u0433\u043e \u0437\u0430\u043f\u043e\u043b\u043d\u044f\u044e         }     } <\/code><\/pre>\n<h5>\u0412\u0432\u043e\u0434 \u0438 \u0432\u044b\u0432\u043e\u0434 \u0434\u0430\u043d\u043d\u044b\u0445<\/h5>\n<p>  \u0420\u0443\u043a\u043e\u0432\u043e\u0434\u0441\u0442\u0432\u0443\u044f\u0441\u044c <a href=\"http:\/\/habrahabr.ru\/post\/246257\/\">\u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435\u0439<\/a> \u0431\u044b\u043b \u0437\u043d\u0430\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0443\u0441\u043a\u043e\u0440\u0435\u043d \u0432\u0432\u043e\u0434. \u0422\u0430\u043a \u043a\u0430\u043a \u0432 \u0434\u0430\u043d\u043d\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0435 \u0432\u044b\u0432\u043e\u0434\u044f\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e 2 \u0447\u0438\u0441\u043b\u0430, \u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 putchar (putchar_unlocked) \u0431\u043e\u043b\u044c\u0448\u043e\u0433\u043e \u043f\u0440\u043e\u0444\u0438\u0442\u0430 \u043d\u0435 \u043f\u0440\u0438\u043d\u043e\u0441\u0438\u0442, \u043d\u043e \u0434\u043b\u044f \u0442\u0435\u0445 \u043a\u0442\u043e \u043d\u0435 \u043f\u043e\u0432\u0435\u0440\u0438\u0442, \u043f\u043e\u043a\u0430 \u043d\u0435 \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442, \u043f\u0440\u0438\u0432\u043e\u0436\u0443 \u043d\u0438\u0436\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u0432\u044b\u0432\u043e\u0434\u0430 \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c putchar_unlocked.  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0424\u0443\u043d\u043a\u0446\u0438\u044f \u0432\u044b\u0432\u043e\u0434\u0430<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"cpp\">\/*  * \u041f\u043e\u0434\u0445\u043e\u0434\u0438\u0442 \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u043d\u0435\u043e\u0442\u0440\u0438\u0446\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u0446\u0435\u043b\u043e\u0447\u0438\u0441\u043b\u0435\u043d\u043d\u044b\u0445 \u0442\u0438\u043f\u043e\u0432:  *     \u043f\u0435\u0440\u0432\u044b\u0439 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 - \u044d\u0442\u043e \u0447\u0438\u0441\u043b\u043e, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0432\u044b\u0432\u0435\u0441\u0442\u0438  *     \u0432\u0442\u043e\u0440\u043e\u0439 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 - \u044d\u0442\u043e \u0441\u0438\u043c\u0432\u043e\u043b, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043d\u0430\u0434\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043f\u043e\u0441\u043b\u0435 \u0447\u0438\u0441\u043b\u0430  *\/ template &lt;typename T&gt; void write_unlocked(T inp, char last=' ') {     if (!inp) {         putchar_unlocked('0');         putchar_unlocked(last);         return;     }      T out = 0;     unsigned char sz_inp = 0;     \/\/ \u043f\u0435\u0440\u0435\u0432\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u0435\u043c \u0447\u0438\u0441\u043b\u043e     for (; inp; inp \/= 10, ++sz_inp) {         out = (out &lt;&lt; 3) + (out &lt;&lt; 1) + inp % 10;     }     \/\/ \u043f\u0435\u0447\u0430\u0442\u0430\u0435\u043c \u0447\u0438\u0441\u043b\u043e     for (; out; out \/= 10, --sz_inp) {         putchar_unlocked(out % 10 + '0');     }     \/\/ \u0432\u044b\u0432\u043e\u0434\u0438\u043c \u043d\u0443\u043b\u0438, \u0435\u0441\u043b\u0438 \u043e\u043d\u0438 \u0431\u044b\u043b\u0438 \u0432 \u0447\u0438\u0441\u043b\u0435     for (;sz_inp; --sz_inp) {         putchar_unlocked('0');     }      putchar_unlocked(last); } <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<div class=\"clear\"><\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"http:\/\/habrahabr.ru\/post\/256929\/\"> http:\/\/habrahabr.ru\/post\/256929\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>     \t\u0425\u043e\u0447\u0435\u0442\u0441\u044f \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u0430\u0442\u044c \u043f\u0440\u043e \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044e \u0421\/C++ \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c, \u0438\u0431\u043e \u0432 \u0438\u043d\u0442\u0435\u0440\u043d\u0435\u0442\u0435 \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u043c\u0430\u043b\u043e \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438. \u0412 \u043f\u043e\u0441\u0442\u0435 \u0431\u0443\u0434\u0443\u0442 \u043e\u0431\u044a\u044f\u0441\u043d\u0435\u043d\u0438\u044f \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u0439 \u043d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u0437\u0430\u0434\u0430\u0447\u0438, \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u043c\u043d\u0435 \u0434\u0430\u043b\u0438 \u0440\u0435\u0448\u0438\u0442\u044c \u0432 \u0438\u043d\u0441\u0442\u0438\u0442\u0443\u0442\u0435 (\u0437\u0430\u0434\u0430\u0447\u0430 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u0430 \u043d\u0430 \u0421++11, \u043e\u0431\u044a\u044f\u0441\u043d\u0435\u043d\u0438\u0435 \u0434\u0430\u0435\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u043b\u044f 64 \u0431\u0438\u0442\u043d\u044b\u0445 \u0441\u0438\u0441\u0442\u0435\u043c).<br \/>  \u0412 \u043f\u0435\u0440\u0432\u0443\u044e \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0441\u0442\u0430\u0442\u044c\u044f \u043f\u0438\u0441\u0430\u043b\u0430\u0441\u044c, \u0447\u0442\u043e\u0431\u044b \u0431\u043e\u043b\u044c\u0448\u0435 \u0443\u0437\u043d\u0430\u0442\u044c \u043f\u0440\u043e \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0430\u0446\u0438\u044e, \u0441\u043e\u0432\u0435\u0442\u044b \u043f\u043e \u043a\u043e\u0442\u043e\u0440\u043e\u0439, \u044f \u043d\u0430\u0434\u0435\u044e\u0441\u044c, \u0432\u044b \u043e\u0441\u0442\u0430\u0432\u0438\u0442\u0435 \u0432 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u044f\u0445.   <\/p>\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-256561","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/256561","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=256561"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/256561\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=256561"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=256561"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=256561"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}