Codeforces 238C World Eater

Source: Internet
Author: User

Test instructions

Enter N, indicating that there are N cities, respectively, 1~n, enter the N-1 group, A, a, indicates that from a can go to B, the minimum number of changes to change the road from the two of the city can go through all the city.

(Http://blog.csdn.net/metalseed/article/details/8045038%20 of something related to achievement)

Problem Solving Ideas:

Create a tree, break an edge, divide the tree into two parts, and calculate the minimum amount of change in both parts.

Then it becomes the smallest change in the subtree.

Dp[i] Indicates the amount of change required for a subtree with I as root

If Root is the starting point, then cost[root] = Dp[root] If you start with any of these subtrees trees

Cost[u] = Dp[root] + u-root need to change amount-root-u need to change the amount of

To make Cost[u] minimum, then (U-root need to change +root-u need to change) the minimum value

1#include <cstdio>2#include <cstring>3#include <iostream>4#include <algorithm>5#include <cmath>6 using namespacestd;7 Const intN = 1e5 +Ten;//1e5 is 10 of the 5-time Square8 structEdge9 {Ten     int  from, to, DIS, NEX; One}edge[n<<1]; A intHead[n], edgenum; - voidAddintUintVintd) - { theEdge E ={u, V, D, Head[u]}; -Edge[edgenum] =E; -Head[u] = edgenum++; - } + intDp[n]; - intn, MX; + voidDfsintUintFaintval) A { atDp[u] =0; -      for(inti = Head[u]; ~i; i = Edge[i].nex)//~i said to take the reverse, in fact, is not 0 -     { -         intv =edge[i].to; -         if(v = = FA)Continue; -DFS (V, u, val-Edge[i].dis);  inDp[u] + = Dp[v] + (edge[i].dis!=1);//if the parentheses are set, add 1, otherwise add 0 . -     } toMX =Max (MX, val); + } - intMain () the { *      while(Cin >>N) $     {Panax Notoginseng         if(n = =1) {puts ("0");Continue; }//puts output and change the costume file Cstdio -Memset (Head,-1,sizeofhead); theEdgenum =0; +          for(inti =1, u, v; I < n; i++) A         { thescanf"%d%d", &u, &v); +Add (U, V,1); -Add (V, U,-1);  $         } $         intAns =1<< -; -          for(inti =0, u, v; i < Edgenum; i + =2) -         { theU = edge[i]. from; v =edge[i].to; -MX =-(1<< -);WuyiDFS (U, V,0); the             intTMP = Dp[u]-MX; -MX =-(1<< -); WuDFS (V, u,0); -TMP + = Dp[v]-MX; AboutAns =min (ans, tmp); $         } -printf"%d\n", ans); -     } -     return 0; A}
View Code

Codeforces 238C World Eater

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.