{"id":544,"date":"2018-05-25T17:12:13","date_gmt":"2018-05-25T09:12:13","guid":{"rendered":"http:\/\/kylelv.com\/?p=544"},"modified":"2018-05-27T19:35:55","modified_gmt":"2018-05-27T11:35:55","slug":"bzoj-4316-%e5%b0%8fc%e7%9a%84%e7%8b%ac%e7%ab%8b%e9%9b%86-%e5%9c%86%e6%96%b9%e6%a0%91%ef%bc%8cdp","status":"publish","type":"post","link":"https:\/\/blog.kylelv.com\/?p=544","title":{"rendered":"bzoj 4316: \u5c0fC\u7684\u72ec\u7acb\u96c6  &#8212; \u5706\u65b9\u6811\uff0cdp"},"content":{"rendered":"<p>&nbsp;<\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_73 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-69fdefb2948ff\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-69fdefb2948ff\"  aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/blog.kylelv.com\/?p=544\/#4316_%E5%B0%8FC%E7%9A%84%E7%8B%AC%E7%AB%8B%E9%9B%86\" title=\"4316: \u5c0fC\u7684\u72ec\u7acb\u96c6\">4316: \u5c0fC\u7684\u72ec\u7acb\u96c6<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/blog.kylelv.com\/?p=544\/#Description\" title=\"Description\">Description<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/blog.kylelv.com\/?p=544\/#Input\" title=\"Input\">Input<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/blog.kylelv.com\/?p=544\/#Output\" title=\"Output\">Output<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/blog.kylelv.com\/?p=544\/#Sample_Input\" title=\"Sample Input\">Sample Input<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/blog.kylelv.com\/?p=544\/#Sample_Output\" title=\"Sample Output\">Sample Output<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/blog.kylelv.com\/?p=544\/#HINT\" title=\"HINT\">HINT<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/blog.kylelv.com\/?p=544\/#Source\" title=\"Source\">Source<\/a><\/li><\/ul><\/nav><\/div>\n<h2 style=\"text-align: center;\"><span class=\"ez-toc-section\" id=\"4316_%E5%B0%8FC%E7%9A%84%E7%8B%AC%E7%AB%8B%E9%9B%86\"><\/span>4316: \u5c0fC\u7684\u72ec\u7acb\u96c6<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: center;\"><span class=\"green\">Time Limit:\u00a0<\/span>10 Sec\u00a0\u00a0<span class=\"green\">Memory Limit:\u00a0<\/span>128 MB<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Description\"><\/span>Description<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<div class=\"content\">\n<div>\u56fe\u8bba\u5c0f\u738b\u5b50\u5c0fC\u7ecf\u5e38\u8650\u83dc\uff0c\u7279\u522b\u662f\u5728\u56fe\u8bba\u65b9\u9762\uff0c\u7ecf\u5e38\u628a\u5c0fD\u8650\u5f97\u5f88\u60e8\u5f88\u60e8\u3002<\/div>\n<div>\u8fd9\u4e0d\uff0c\u5c0fC\u8ba9\u5c0fD\u53bb\u6c42\u4e00\u4e2a\u65e0\u5411\u56fe\u7684\u6700\u5927\u72ec\u7acb\u96c6\uff0c\u901a\u4fd7\u5730\u8bb2\u5c31\u662f\uff1a\u5728\u65e0\u5411\u56fe\u4e2d\u9009\u51fa\u82e5\u5e72\u4e2a\u70b9\uff0c\u8fd9\u4e9b\u70b9\u4e92\u76f8\u6ca1\u6709\u8fb9\u8fde\u63a5\uff0c\u5e76\u4f7f\u53d6\u51fa\u7684\u70b9\u5c3d\u91cf\u591a\u3002<\/div>\n<div>\u5c0fD\u867d\u7136\u56fe\u8bba\u5f88\u5f31\uff0c\u4f46\u662f\u4e5f\u77e5\u9053\u65e0\u5411\u56fe\u6700\u5927\u72ec\u7acb\u96c6\u662fnpc\uff0c\u4f46\u662f\u5c0fC\u5f88\u4ec1\u6148\u7684\u7ed9\u4e86\u4e00\u4e2a\u5f88\u6709\u7279\u70b9\u7684\u56fe\uff1a \u56fe\u4e2d\u4efb\u4f55\u4e00\u6761\u8fb9\u5c5e\u4e8e\u4e14\u4ec5\u5c5e\u4e8e\u4e00\u4e2a\u7b80\u5355\u73af\uff0c\u56fe\u4e2d\u6ca1\u6709\u91cd\u8fb9\u548c\u81ea\u73af\u3002\u5c0fC\u8bf4\u8fd9\u6837\u5c31\u4f1a\u6bd4\u8f83\u6c34\u4e86\u3002<\/div>\n<div>\u5c0fD\u89c9\u5f97\u8fd9\u4e2a\u9898\u76ee\u5f88\u6709\u8da3\uff0c\u5c31\u4ea4\u7ed9\u4f60\u4e86\uff0c\u76f8\u4fe1\u4f60\u4e00\u5b9a\u53ef\u4ee5\u89e3\u51fa\u6765\u7684\u3002<\/div>\n<\/div>\n<h2><span class=\"ez-toc-section\" id=\"Input\"><\/span>Input<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<div class=\"content\">\n<div>\u7b2c\u4e00\u884c\uff0c\u4e24\u4e2a\u6570n\uff0c m\uff0c\u8868\u793a\u56fe\u7684\u70b9\u6570\u548c\u8fb9\u6570\u3002<\/div>\n<div>\u7b2c\u4e8c~m+1\u884c\uff0c\u6bcf\u884c\u4e24\u4e2a\u6570x\uff0cy\uff0c\u8868\u793ax\u4e0ey\u4e4b\u95f4\u6709\u4e00\u6761\u65e0\u5411\u8fb9\u3002<\/div>\n<\/div>\n<h2><span class=\"ez-toc-section\" id=\"Output\"><\/span>Output<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<div class=\"content\">\n<div>\u8f93\u51fa\u8fd9\u4e2a\u56fe\u7684\u6700\u5927\u72ec\u7acb\u96c6\u3002<\/div>\n<\/div>\n<h2><span class=\"ez-toc-section\" id=\"Sample_Input\"><\/span>Sample Input<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<div class=\"content\"><span class=\"sampledata\">5 6<br \/>\n1 2<br \/>\n2 3<br \/>\n3 1<br \/>\n3 4<br \/>\n4 5<br \/>\n3 5<\/span><\/div>\n<h2><span class=\"ez-toc-section\" id=\"Sample_Output\"><\/span>Sample Output<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<div class=\"content\"><span class=\"sampledata\">2<br \/>\n<\/span><\/div>\n<h2><span class=\"ez-toc-section\" id=\"HINT\"><\/span>HINT<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<div class=\"content\">\n<div>100% n &lt;=50000, m&lt;=60000<\/div>\n<div><\/div>\n<\/div>\n<h2><span class=\"ez-toc-section\" id=\"Source\"><\/span>Source<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<div class=\"content\">emmm\u76f4\u63a5\u641e\u51fa\u5706\u65b9\u6811\u7136\u540edp<\/div>\n<div>\u6211\u4eec\u53ea\u9700\u8981\u5bf9\u4e8e\u65b9\u70b9\u7279\u6b8a\u5904\u7406<\/div>\n<div>\u56e0\u4e3a\u6211\u4eec\u53d1\u73b0\u5bf9\u4e8e\u65b9\u70b9\u5411\u4e0a\u7684\u66f4\u65b0\uff0c\u5982\u679cfa[x]\u53d6\u7684\u8bdd\u4ed6\u4f1a\u7531f[x][0]\u66f4\u65b0\uff0c\u6240\u4ee5f[x][0]\u5b9e\u9645\u8868\u793a\u7684\u662fx\u513f\u5b50\u4e2d\u5934\u5c3e\u4e24\u4e2a\u70b9\u4e0d\u53d6\u7684\u6700\u4f18\u4ee3\u4ef7\uff0c\u6211\u4eec\u53ef\u4ee5\u518ddp\u4e00\u4e0b\u5f97\u5230<\/div>\n<div>\u540c\u7406\uff0c\u5bf9\u4e8ef[x][1]\u8868\u793a\u513f\u5b50\u4e2d\u4e0d\u76f8\u90bb\u53d6\u6cd5\u7684\u6700\u4f18\u4ee3\u4ef7\uff0c\u4e00\u6837\u53ef\u4ee5dp\u6c42\u5f97<\/div>\n<div><\/div>\n<div>\n<pre class=\"lang:c++ decode:true \">#include&lt;map&gt;\r\n#include&lt;cmath&gt;\r\n#include&lt;queue&gt;\r\n#include&lt;cstdio&gt;\r\n#include&lt;string&gt;\r\n#include&lt;cstring&gt;\r\n#include&lt;iostream&gt;\r\n#include&lt;algorithm&gt;\r\nusing namespace std;\r\n#define inf 1000000007\r\n#define ll long long\r\n#define N 120010\r\ninline int rd()\r\n{\r\n    int x=0,f=1;char ch=getchar();\r\n    while(ch&lt;'0'||ch&gt;'9'){if(ch=='-')f=-1;ch=getchar();}\r\n    while(ch&gt;='0'&amp;&amp;ch&lt;='9'){x=x*10+ch-'0';ch=getchar();}\r\n    return x*f;\r\n}\r\nint n,m,nn;\r\nint dfn[N],tim,fa[N],len[N];\r\nbool vs[N];\r\nstruct cst\r\n{\r\n    int lj[N],fro[N],to[N],cnt=0;\r\n    void add(int a,int b){fro[++cnt]=lj[a];to[cnt]=b;lj[a]=cnt;}\r\n    int f[N][2],s[N],sz;\r\n    int g[N][2];\/\/\u8868\u793a\u7b2ci\u4e2a\u513f\u5b50\u9009\u6216\u8005\u4e0d\u9009\u7684\u6700\u5927\u4e2a\u6570\r\n    void sol(int x)\r\n    {\r\n        sz=0;\r\n        for(int i=lj[x];i;i=fro[i]) s[++sz]=to[i];\r\n        if(sz==2)\r\n        {\r\n            f[x][0]=f[s[1]][0]+f[s[2]][0];\r\n            f[x][1]=max(f[s[1]][0]+f[s[2]][0],max(f[s[1]][0]+f[s[2]][1],f[s[1]][1]+f[s[2]][0]));\r\n            return;\r\n        }\r\n        g[1][0]=f[s[1]][0];g[1][1]=-inf;\r\n        for(int i=2;i&lt;=sz;i++)\r\n        {\r\n            g[i][1]=g[i-1][0]+f[s[i]][1];\r\n            g[i][0]=max(g[i-1][1],g[i-1][0])+f[s[i]][0];\r\n        }\r\n        f[x][0]=g[sz][0];\r\n        g[1][0]=f[s[1]][0];g[1][1]=f[s[1]][1];\r\n        for(int i=2;i&lt;=sz;i++)\r\n        {\r\n            g[i][1]=g[i-1][0]+f[s[i]][1];\r\n            g[i][0]=max(g[i-1][1],g[i-1][0])+f[s[i]][0];\r\n        }\r\n        f[x][1]=max(g[sz][0],g[sz][1]);\r\n    }\r\n    void dfs(int x)\r\n    {\r\n        f[x][0]=0;\r\n        f[x][1]=(x&lt;=nn);\r\n        for(int i=lj[x];i;i=fro[i])\r\n        {\r\n            dfs(to[i]);\r\n            if(x&lt;=nn)\r\n            {\r\n                f[x][0]+=max(f[to[i]][0],f[to[i]][1]);\r\n                f[x][1]+=f[to[i]][0];\r\n            }\r\n        }\r\n        if(x&gt;nn) sol(x);\r\n    }\r\n    void work()\r\n    {\r\n        dfs(1);\r\n        printf(\"%d\\n\",max(f[1][0],f[1][1]));\r\n    }\r\n}T;\r\n\r\nint lj[N],fro[N],to[N],cnt;\r\nvoid add(int a,int b){fro[++cnt]=lj[a];to[cnt]=b;lj[a]=cnt;}\r\n\r\nvoid tarjan(int x)\r\n{\r\n    dfn[x]=++tim;\r\n    for(int i=lj[x],tx;i;i=fro[i])\r\n    {\r\n        if(to[i]==fa[x]) continue;\r\n        if(!dfn[to[i]]) fa[to[i]]=x,tarjan(to[i]);\r\n        else if(dfn[to[i]]&lt;dfn[x])\r\n        {\r\n            len[++n]=dfn[x]-dfn[to[i]]+1;\r\n            fa[n]=to[i];\r\n            T.add(to[i],n);\r\n            tx=x;\r\n            while(tx^to[i])\r\n            {\r\n                T.add(n,tx);\r\n                vs[tx]=1;\r\n                tx=fa[tx];\r\n            }\r\n        }\r\n    }\r\n    if(!vs[x]) T.add(fa[x],x);\r\n    tim--;\r\n}\r\nint main()\r\n{\r\n    nn=n=rd();m=rd();\r\n    for(int i=1,x,y;i&lt;=m;i++)\r\n    {\r\n        x=rd();y=rd();\r\n        add(x,y);add(y,x);\r\n    }\r\n    tarjan(1);\r\n    T.work();\r\n    return 0;\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; 4316: \u5c0fC\u7684\u72ec\u7acb\u96c6 Time Limit:\u00a010 Sec\u00a0\u00a0Memory Limit:\u00a01 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[5,62],"class_list":["post-544","post","type-post","status-publish","format-standard","hentry","category-bzoj","tag-dp","tag-62"],"_links":{"self":[{"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=\/wp\/v2\/posts\/544","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=544"}],"version-history":[{"count":5,"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=\/wp\/v2\/posts\/544\/revisions"}],"predecessor-version":[{"id":551,"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=\/wp\/v2\/posts\/544\/revisions\/551"}],"wp:attachment":[{"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=544"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=544"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=544"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}